首页 > 开发 > JSP > 正文

JSP中小型网站适用的一个JDBC数据库联接类

2020-02-05 13:37:46
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

这个类我本是在参加学校网页设计大赛时写的.现在稍加修改借以讨论有关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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表