⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconn.java

📁 这个是我们做土地局的一个项目。感觉不错。欢迎下载
💻 JAVA
字号:
package huayi.gg.cao;

import java.sql.*;
import java.io.*;
import java.util.*;
import javax.naming.*;

/**
* 从连接池中取得一个空闲的数据库连接
* @return Connection
* @exception Exception
*/
public class dbConn{

Connection conn = null; 
CallableStatement stmtpro = null;
Statement stmt=null;
ResultSet rs = null;


PreparedStatement pre;
Calendar calendar=Calendar.getInstance();
String houzhui=null;
File f1;
RandomAccessFile random;
FileOutputStream o;
String secondLine;
int second=1;
int position;
String fileName;
byte b[]=new byte[10000];
int n;


public dbConn(){
try {
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=HYOA;user=center;password=6651002");
} catch(java.lang.SecurityException se) {
throw se;
} catch(java.lang.Exception e) {
e.printStackTrace();
}
}


//数据处理
public ResultSet executeQuery(String sql){
stmt=null;
rs = null;
try {        
     stmt=conn.createStatement();                                                 
     rs=stmt.executeQuery(sql);
     } catch(SQLException ex){
       System.err.println("sql_data.executeUpdate:"+ex.getMessage());
     //显示数据库连接错误或者查询错误 
                             }   
     return rs;                          
    }

public ResultSet executeQueryb(String sql){
stmt=null;
rs = null;
try {   
     stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);                                                               
     rs=stmt.executeQuery(sql);
     } catch(SQLException ex){
       System.err.println("sql_data.executeUpdate:"+ex.getMessage());
     //显示数据库连接错误或者查询错误 
                             }   
     return rs;                          
    }
//数据更新
public int executeUpdate(String sql){
stmt=null;	
int number = 0;
try {
       stmt=conn.createStatement();    
       stmt.executeUpdate(sql);
       if (conn==null) { return -1; }
       if (stmt == null) { return -1; }
        } catch(SQLException ex){
       System.err.println("sql_data.executeUpdate:"+ex.getMessage());
       return -1;
        }
       return number;
    }
//取后缀名	
public String getName(InputStream in,String tempFileName)
{

try
{
  f1=new File("c:/",tempFileName);
  o=new FileOutputStream(f1);
  while((n=in.read(b))!=-1){o.write(b,0,n);}
  o.close();
  in.close();
  random=new RandomAccessFile(f1,"r");
  secondLine=null;
  while(second<=2)
  {secondLine=random.readLine();second++;}
  position=secondLine.lastIndexOf('\\');
  fileName=secondLine.substring(position+1,(secondLine.length()-1));
  int position0=fileName.lastIndexOf('.');
  houzhui=fileName.substring((position0+1),fileName.length()); 
 
}
catch(IOException e){}
  return houzhui;  
}
//文件上传
public void transfer(String sql)
{
try
{
  random.seek(0);
  long forthEndPosition=0;
  int forth=1;
  while((n=random.readByte())!=-1&&(forth<=4))
   {
    if(n=='\n')
    {
	    forthEndPosition=random.getFilePointer();
        forth++;
	}
   }
  File f2=new File("c:/tomcat/webapps/HUAYI/XT/Image",fileName);
  RandomAccessFile random2=new RandomAccessFile(f2,"rw");
  random.seek(random.length());
  long endPosition=random.getFilePointer();
  long mark=endPosition;
  int j=1;
  while((mark>=0)&&(j<=6))
 {
   mark--;
   random.seek(mark);
   n=random.readByte();
   if(n=='\n')
   {endPosition=random.getFilePointer();j++;}
 }
  random.seek(forthEndPosition);
  long startPoint=random.getFilePointer();
  while(startPoint<(endPosition-1))
 {
   n=random.readByte();
   random2.write(n);
   startPoint=random.getFilePointer();
 }
 FileInputStream inn=new FileInputStream(f2);
 try
{
 pre=conn.prepareStatement(sql);
 pre.setBinaryStream(1,inn,inn.available());
 int number=pre.executeUpdate();
}
catch(SQLException ex)
{
System.err.println("sql_data.executeUpdate:"+ex.getMessage());
}
 random2.close();
 random.close();
f1.delete();
//f2.delete();
   
}
catch(IOException e){}
}
//获取日期
public void calendar()
{
	calendar.setTime(new java.util.Date());
}
public int getYear()
{
	int year=calendar.get(Calendar.YEAR);
	return year;
}
public int getMonth()
{
	int month=calendar.get(Calendar.MONTH)+1;
	return month;
}
public int getDate()
{
	int date=calendar.get(Calendar.DAY_OF_MONTH);
	return date;
}
//关闭数据源,断开连接
public boolean close(){
try{
  if(this.rs!=null)
  {
   this.rs.close();
  } 
  if(stmt!=null)
  {
   this.stmt.close();
  }
  if(stmtpro!=null)
  {
   this.stmtpro.close();
  }
  if(conn!=null)
  {
   this.conn.close();
  }
  return true;
}
catch(Exception err)
{
return false;
}
}
    
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -