這個類我本是在參加學校網頁設計大賽時寫的.現在稍加修改借以討論有關jsp數據庫訪問優化的問題.類的內容如下,這個類在不修改任何代碼的情況下適用于mssql/mysql/access數據庫的聯接和基本操作.
package mxzc.web.dbctrl;
import java.sql.*;
public final class sqldbctrl
{/*********************************************
public sqldbctrl(string usr,string pwd,string cnstr,string derv)
public synchronized resultset selects(string sql)
public synchronized void updates(string sql)
public synchronized void adddels(string sql)
數據庫操作的函數.
**********************************************/
private string usr;
private string pwd;
private string cnstr;
private string derv;
private bool canuse;
public sqldbctrl(string usr,string pwd,string cnstr,string derv)throws sqlexception
{
this.usr=usr;
this.pwd=pwd;
this.cnstr=cnstr;
this.derv=derv;
this.canuse=true;
try
{
class.forname(derv);
}
catch(classnotfoundexception e)
{
this.canuse=false;
system.out.println("請確定"+derv+"類所對應的包已包含進程序的環境變量內.");
e.tostring();
}
}
public sqldbctrl()throws sqlexception
{
//這里是一個空的構造,這個構造可能產生問題....
this.canuse=false;
}
public string getusr(){return this.usr;}
public string getpwd(){return this.pwd;}
public string getcnstr(){return this.cnstr;}
public string getderv(){return this.derv;}
public void setusr(string usr){this.usr=usr;}
public void setpwd(string pwd){this.pwd=pwd;}
public void setcnstr(string cnstr){this.cnstr=cnstr;}
public void setderv(string derv)
{//屬性derv對本類實例的可用性有重要意義.
/*
只有derv被賦值后,這個實例才可操作.因為這里需要加載數據庫的驅動.
為了防止一個沒有加載數據庫驅動的實例被使用,我設置了一個canuse屬性.
只有該屬性為真時,這個實例才真正的可操作.
*/
this.canuse=true;
this.derv=derv;
try
{
class.forname(derv);
}
catch(classnotfoundexception e)
{
this.canuse=false;
system.out.println("請確定"+derv+"類所對應的包已包含進程序的環境變量內.");
e.tostring();
}
}
public bool getcanuse()
{//是否可進行數據庫操作?在操作的數據庫操作之前最好是執行一下.以防出錯.
return canuse;
}
public synchronized resultset selects(string sql)throws exception
{
connection conn=null;
statement stmt=null;
resultset rs=null;
conn=drivermanager.getconnection(cnstr,usr,pwd);
stmt=conn.createstatement();
rs=stmt.executequery(sql);
return rs;
}
public synchronized void updates(string sql)throws exception
{
connection conn=null;
statement stmt=null;
conn=drivermanager.getconnection(cnstr,usr,pwd);
stmt=conn.createstatement();
stmt.executeupdate(sql);
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
stmt=null;
conn=null;
}
public synchronized void adddels(string sql)throws exception
{
connection conn=null;
statement stmt=null;
conn=drivermanager.getconnection(cnstr,usr,pwd);
stmt=conn.createstatement();
stmt.execute(sql);
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
stmt=null;
conn=null;
}
public static void main(string args[])throws exception
{
system.out.println("");
system.out.println("************************************************");
system.out.println(" 包名: mxzc.web.dbctrl");
system.out.println(" 類名: sqldbctrl");
system.out.println(" 特性: 最終類,線程安全");
system.out.println(" 沒有默認構造函數只能有參構造");
system.out.println(" 作者: 夢醒之初/可心");
system.out.println(" 版本: 1.10(update)");
system.out.println("************************************************");
//內部測試用sqldbctrl a=new sqldbctrl("","","jdbc:odbc:jspbbs","sun.jdbc.odbc.jdbcodbcdriver");
}
}
在jsp網站中的期望使用方式:
<jsp:usebean id="conn" scope="session" class="mxzc.web.dbctrl.sqldbctrl" >
<jsp:setproperty name="conn" property="usr" value="" />
<jsp:setproperty name="conn" property="pwd" value="" />
<jsp:setproperty name="conn" property="cnstr" value="jdbc:mysql://localhost:3306/kexintemp?useunicode=true&characterencoding=gbk" />
<jsp:setproperty name="conn" property="derv" value="org.gjt.mm.mysql.driver" />
</jsp:usebean>
新聞熱點
疑難解答