用JavaBean封装数据库操作(数据库连接用连接池)- -

来源:互联网 发布:获取淘宝视频真实地址 编辑:程序博客网 时间:2024/05/09 18:23
 

package school.pub;

import java.sql.*;
import school.connectionpool.*;   //引用精华帖中我发的连接池,注意路径

/**
* Description:数据库的封装操作
* @author:shiyq
* @version 1.0
*/

public class DBExecute {
private Connection conn = null;
private Statement stmt = null;  //有回滚的
private PreparedStatement pstmt = null;  //@param查询
private PoolManager dcm=null;  //连接池管理

   Utility ut=new Utility();  //声明一个汉字内码转换的类,自已写的,你可以不用它

  public void init() {
       dcm = PoolManager.getInstance();  //连接池实例化
       conn = dcm.getConnection("school"); //取得连接池
   }

  /*** 构造数据库的连接和访问类*/
   public DBExecute() throws Exception {
   init();
}
   
 //有回滚功能的生成器
   public void setStmt() throws Exception {
 this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 }

 /***返回状态* @return Statement 状态 */
public Statement getStmt() throws Exception {
return stmt;
}
   
/**  * 可回滚的预编译SQL语句   * @param sql SQL语句  */
public void setPstmt(String sql) throws Exception {
 this.pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
 
/** * 返回预设状态 */
public PreparedStatement getPstmt() throws Exception{
return pstmt;
}

public void clearParameters() throws SQLException    {
       pstmt.clearParameters();
   pstmt=null;
   }

/** * 返回连接 * @return Connection 连接 */
public Connection getConnection() {
return conn;
}


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

 
/** * 执行一般的SQL语句返回字段集 */
public ResultSet ExeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
 
   //执行有预处理功能的查询
public ResultSet ExeQuery() throws SQLException {
if (pstmt != null) {
return pstmt.executeQuery();
}
else return null;
}
 
  /*
//** * 执行一般的更新SQL语句 *
public void ExeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}

//执行有预处理功能的更新操作
public void ExeUpdate() throws SQLException {
if (pstmt != null)
pstmt.executeUpdate();
}
  */

  public boolean ExeUpdate(String sql) throws SQLException {
     boolean flat=false;
     if (stmt != null)
   if (stmt.executeUpdate(sql)>0) {
 flat=true; }
 return flat;
}

public boolean ExeUpdate() throws SQLException {
     boolean flat=false;
if (pstmt != null)
 if (pstmt.executeUpdate()>0){
   flat=true; }
 return flat;
}


/** * 关闭连接 */
public void close() throws Exception {
if (stmt != null)  {
stmt.close();
stmt = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (conn!=null)
{

dcm.freeConnection("school",conn);

}

}

 public static  void main(String[] args){
 
//不代参数的查询
try{
String sql="select * from SysVar";
DBExecute dbc=new DBExecute();
    dbc.setStmt();
ResultSet rs=dbc.ExeQuery(sql);
while (rs.next())
{
System.out.println("UsersID:"+rs.getString(1));
        System.out.println("UsersID:"+rs.getString(2));
}
dbc.close();
    System.out.println("aaa");
  }catch(Exception ex){System.out.println(ex.getMessage());}
 
   
/*
//代参数查询
try{
     String sql="select * from Users where UserID=?";
     DBExecute dbc=new DBExecute();
 dbc.setPstmt(sql);
 dbc.setInt(1,6);
 ResultSet rs=dbc.ExeQuery();
     while (rs.next()) {
System.out.println("UsersID:"+rs.getString(1));
        System.out.println("UsersID:"+rs.getString(2));
 }
 dbc.close();
     System.out.println("aaa");
    }catch(Exception ex){System.out.println(ex.getMessage());}
   */

 }

}

 
原创粉丝点击