?? dbconn.java
字號:
package huayi.gg.jd;
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=tempFileName;
int position0=fileName.lastIndexOf('.');
houzhui=fileName.substring((position0+1),fileName.length());
}
catch(IOException e){}
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("../webapps/HUAYI/NB/EB/face",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);
random2.close();
random.close();
f1.delete();
//f2.delete();
}
catch(IOException e){}
return houzhui;
}
//獲取日期
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 + -