基于tomcat连接池设计的JavaBean【访问数据库】(

 

在上一篇已经说明了所有关于tomcat6.0+Mysql+jdbc建立连接池

参考网上写了个JavaBean用于数据库连接的所有操作

package database;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
//import database.DBConnectionManager;

public class DBConnect
{
      Connection conn=null; //数据库连接变量
      Statement stmt=null;//statement语句变量
      private PreparedStatement prepstmt=null;//预定义statement 变量
     // private DBConnectionManager dcm=null;//连接池管理变量
       DataSource ds = null;
     
      void init() throws Exception{
        InitialContext ctx=new InitialContext();     
        ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); //连接池名,具体看上一篇  
        conn = ds.getConnection();    
      }
    //构造数据库的连接和访问类
      public DBConnect() throws Exception{
          init();
          stmt=conn.createStatement();
      }
      public DBConnect(int resultSetType,int resultSetConcurrency) throws Exception{
          init();
          stmt=conn.createStatement(resultSetType,resultSetConcurrency);
      }
      //执行sql语句,返回结果集
      public ResultSet executeQuery(String sql) throws SQLException{
         if(stmt!=null){
             return stmt.executeQuery(sql);
          }
          else return null;
      }
      public ResultSet executeQuery() throws SQLException{
          if(prepstmt!=null){
             return prepstmt.executeQuery();
          }
          else return null;
      }
      public int executeUpdate(String sql) throws SQLException{
          if(stmt!=null){
             return stmt.executeUpdate(sql);  
          }
          else return -1;
      }
      public int executeUpdate() throws SQLException{
          if(prepstmt!=null){
             return prepstmt.executeUpdate();
          }
          else return -1;
      }


      //够找数据库的连接和访问类
      //预编译SQL语句
      public DBConnect(String sql) throws Exception{
          init();
          this.prepareStatement(sql);
      }
      public DBConnect(String sql,int resultSetType,int resultSetConcurrency) throws Exception{
          init();
          this.prepareStatement(sql,resultSetType,resultSetConcurrency);
      }

     
      //返回连接
      public Connection getConnection(){
          return conn;
      }
    
      //关闭连接
      public void close() throws Exception{
          if(stmt!=null){
             stmt.close();
             stmt=null;
           }
          if(prepstmt!=null){
             prepstmt.close();
             prepstmt=null;
           }
         if(conn!=null){
            conn.close();
           }
      }  
     
     //preparedStatement 创建一个对象
     public void prepareStatement(String sql) throws SQLException{
          prepstmt=conn.prepareStatement(sql);
        
     }
     public void prepareStatement(String sql,int resultSetType,int resultSetConcurrency) throws SQLException{
          prepstmt=conn.prepareStatement(sql,resultSetType,resultSetConcurrency);
     }
  


     //设置对应的值
     //index参数索引value对应值
     public void setString(int index,String value) throws SQLException{
         prepstmt.setString(index,value);
     }
     public void setInt(int index,int value) throws SQLException{
         prepstmt.setInt(index,value);
     }
     public void setBoolean(int index,boolean value) throws SQLException{
         prepstmt.setBoolean(index,value);
     }
     public void setDate(int index,Date value) throws SQLException{
         prepstmt.setDate(index,value);
     }
     public void setLong(int index,long value) throws SQLException{
         prepstmt.setLong(index,value);
     }
     public void setFloat(int index,float value) throws SQLException{
         prepstmt.setFloat(index,value);
     }
     public void setBytes(int index,byte[] value) throws SQLException{
         prepstmt.setBytes(index,value);
     }

     //清除参数及返回状态
     public void clearParameters() throws SQLException{
         prepstmt.clearParameters();
         prepstmt=null;
     }
     public PreparedStatement getPreparedStatement(){
         return prepstmt;
     }
     public Statement getStatement(){
         return stmt;
     }

   

  
}

 

 

 

具体使用有两种:

1.选择性,操作成功返回1,失败返回0

int flag=0;
String result=null;//如何比对用户名密码而不出错,的SQL语句~~~~就不用在异常里捕捉了,我晕
String strSql = "select userPwd from users where userName='"+userName+"'";  
  try{
  DBConnect dbc=new DBConnect();
  ResultSet rs=dbc.executeQuery(strSql); 
  while(rs.next()){      
        result=rs.getString(1);                     
    }
  if(result.equals(password))
   flag=1;      
  rs.close();
  dbc.close();
  }catch(Exception e)
  {
   return flag;
  }

 

2.更新性质的:更新成功1

     int flag=0;
     //存在自动id增长的SQL语句还没找到,我晕
     String strSql = "insert into users(userName,userPwd) values(?,?)";    
     try{
     DBConnect dbc=new DBConnect(strSql);
        dbc.setString(1,userName);
        dbc.setString(2,password);
        flag = dbc.executeUpdate();
      
        dbc.close();
        }catch(Exception e)
        {
         flag=0;
         return flag;
        }
     return flag;

 

基本就这两种了,用起来蛮方便的

自己读读代码咯

不多说,有问题发信http://blog.sina.com.cn/programbus

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值