導入Java . SQL . *;
公共類數據庫訪問{
string sdb driver = " Oracle . JDBC . driver . Oracle driver ";
string URL = " JDBC:Oracle:thin:@ localhost:1521:xdf ";
String sHint =
結果集rs = null
連接連接=空;
語句stmt = null
String user = " scott
String pwd = " tiger
公共字符串getSHint(){
返回sHint
}
public void setSDBDriver(String dbDriver){
sDBDriver = dbDriver
}
公共字符串getSDBDriver(){
返回sDBDriver
}
公共字符串getUrl(){
返回url
}
公共void setUrl(字符串){
URL = s;
}
公共結果集getResultSet(){
返回RS;
}
//初始化
public boolean initialize(String db drive,String dbConn,String user,String pwd){
sDBDriver = dbDrive
url = dbConn
用戶=用戶;
pwd = pwd
返回initialize();
}
//初始化
公共布爾initialize(){
嘗試{
class . for name(sdb driver);
sHint = "初始化成功";
返回true
} catch(ClassNotFoundException e){
sHint = "初始化異常:"+e . getmessage();
返回false
}
}
//執行查詢語句
公共結果集executeQuery(字符串sql){
rs = null
嘗試{
conn = driver manager . getconnection(URL,user,pwd);
stmt = conn . create statement();
RS = stmt . execute query(SQL);
}catch(SQLException ex){
sHint = "查詢異常:"+ex . getmessage();
}
返回RS;
}
//執行更新語句
public boolean executeUpdate(字符串sql){
嘗試{
conn = driver manager . getconnection(URL,user,pwd);
stmt = conn . create statement();
stmt . execute update(SQL);
返回true
}catch(SQLException ex){
sHint = "更新異常:"+ex . getmessage();
返回false
}
}
//關閉連接、結果集和語句,釋放資源。
public boolean close(){
嘗試{
如果(rs!= null)RS . close();
如果(stmt!= null)stmt . close();
如果(conn!= null)conn . close();
返回true
}catch(SQLException ex){
sHint = " Close Exception:"+ex . getmessage();
返回false
}
}
}