- 论坛徽章:
- 0
|
我用这个类来完成所有的数据库操作,使用这个类的类采用线程序安全,请大家指点不足之处,谢谢:)
//很多功能还未实现,比如事务等等
- /*============================================================
- * 版权: 凌云客栈 版权所有 (c) 2002 - 2003
- * 文件: org.skyinn.sql.DBOperator
- * 功能: 数据库操作类
- * 所含类: DBOperator
- * 修改记录:
- * 日期 作者 内容
- * =============================================================
- * 2003-01-15 walker 创建文件,并实现基本功能
- * 2003-01-19 walker 修改
- * ============================================================*/
- package org.skyinn.sql;
- //sql
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.PreparedStatement;
- import java.sql.CallableStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- //date
- import java.util.Date;
- //日志
- import org.skyinn.util.Logger;
- /**
- * <p>;数据库操作类。</p>;
- * @author walker
- */
- public class DBOperator
- {
- /**日志处理对象*/
- protected static Logger log = Logger.getInstance("DBOperator");
- /**数据库连接对象*/
- protected Connection conn = null;
- /**Statement*/
- protected Statement stmt = null;
- /**PreparedStatement*/
- protected PreparedStatement pstmt = null;
- /**CallableStatement*/
- protected CallableStatement cstmt = null;
- /**数据库连接池管理对象*/
- protected DBConnectionManager dbmanager = null;
- /**
- * 构造方法。
- *
- * 取数据库连接并生产Statement。
- *
- * @throws SQLException
- */
- public DBOperator(){
- try{
- dbmanager = DBConnectionManager.getInstance();
- /** @todo pool name */
- this.conn = dbmanager.getConnection("DB");
- this.stmt = this.conn.createStatement();
- }catch(Exception e){
- log.error(e.getMessage());
- }
- }
- /**
- * 构造方法。
- *
- * 取数据库连接,生成Statement,根据sql参数生成PrepareStatement。
- *
- * @param sql
- * @throws SQLException
- */
- public DBOperator(String sql){
- try{
- dbmanager = DBConnectionManager.getInstance();
- this.conn = dbmanager.getConnection("DB");
- this.stmt = this.conn.createStatement();
- this.pstmt = this.conn.prepareStatement(sql);
- }catch(Exception e){
- log.error(e.getMessage());
- }
- }//end DBOperator()
- /**
- * 设置PrepareStatement,并同时其清空参数列表。
- *
- * @param sql SQL语句
- * @throws SQLException SQL异常
- */
- public void setPrepareStatement(String sql) throws SQLException
- {
- this.clearParameters();
- this.pstmt = this.conn.prepareStatement(sql);
- }
- /**
- * 设置字符串值。
- *
- * @param index 索引
- * @param value 字符串值
- * @throws SQLException SQL异常
- */
- public void setString(int index,String value) throws SQLException
- {
- pstmt.setString(index,value);
- }
- /**
- * 设置整形值。
- *
- * @param index 索引
- * @param value 整形值
- * @throws SQLException SQL异常
- */
- public void setInt(int index,int value) throws SQLException
- {
- pstmt.setInt(index,value);
- }
- /**
- * 设置布尔值。
- *
- * @param index 索引
- * @param value 布尔值
- * @throws SQLException SQL异常
- */
- public void setBoolean(int index,boolean value) throws SQLException
- {
- pstmt.setBoolean(index,value);
- }
- /**
- * 设置时间值。
- *
- * @param index 索引
- * @param value 时间值
- * @throws SQLException SQL异常
- */
- public void setDate(int index,Date value) throws SQLException
- {
- //pstmt.setDate(index,value);
- /** @todo dateformat and insert... */
- pstmt.setString(index,value.toString());
- }
- /**
- * 设置Long值。
- *
- * @param index 索引
- * @param value Long值
- * @throws SQLException SQL异常
- */
- public void setLong(int index,long value) throws SQLException
- {
- pstmt.setLong(index,value);
- }
- /**
- * 设置浮点值。
- *
- * @param index 索引
- * @param value 浮点值
- * @throws SQLException SQL异常
- */
- public void setFloat(int index,float value) throws SQLException
- {
- pstmt.setFloat(index,value);
- }
- /**
- * 清空PrepareStatement中的参数。
- *
- * @throws SQLException SQL异常
- */
- public void clearParameters() throws SQLException
- {
- if(null != this.pstmt){
- pstmt.clearParameters();
- }
- }
- /**
- * 执行查询。
- *
- * 该方法调用Statement的executeQuery(sql)方法并返回ResultSet结果集。
- *
- * @param sql SQL语句
- * @return ResultSet结果集
- * @throws SQLException SQL异常
- */
- public ResultSet executeQuery(String sql) throws SQLException
- {
- if(null != stmt){
- return stmt.executeQuery(sql);
- }
- return null;
- }//end executeQuery()
- /**
- * 执行查询。
- *
- * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
- *
- * @return ResultSet结果集
- * @throws SQLException SQL异常
- */
- public ResultSet executeQuery() throws SQLException
- {
- if(null != pstmt){
- return pstmt.executeQuery();
- }
- return null;
- }//end executeQuery()
- /**
- * 更新数据库。
- *
- * 该方法调用Statement的executeUpdate(sql)方法并返回修改的行数。
- *
- * @param sql SQL语句
- * @return 修改的行数
- * @throws SQLException SQL异常
- */
- public int executeUpdate(String sql) throws SQLException
- {
- if(null != stmt){
- return this.stmt.executeUpdate(sql);
- }
- return -1;
- }
- /**
- * 更新数据库。
- *
- * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
- *
- * @return 修改的行数
- * @throws SQLException SQL异常
- */
- public int executeUpdate() throws SQLException
- {
- if(null != pstmt){
- return this.pstmt.executeUpdate();
- }
- return -1;
- }
- /**
- * 关闭数据库操作,释放数据库连接。
- */
- public void close()
- {
- try{
- if(null != stmt){
- stmt.close();
- stmt = null;
- }
- if(null != pstmt){
- pstmt.close();
- pstmt = null;
- }
- if(null != cstmt){
- cstmt.close();
- cstmt = null;
- }
- }catch(SQLException e){
- log.error("DBOperator close() error: " + e.getMessage());
- }finally{
- try{
- dbmanager.freeConnection(this.conn,"DB");
- }catch(SQLException e){
- log.error("DBOperator close() error: " + e.getMessage());
- }//try...freeConnection...
- }//try...catch...finally...
- }//end close()
- }//end DBOperator class
复制代码
调用这个类的代码:
- ...
- DBOperator dbo = new DBOperator();
- dbo.setPrepareStatement(SQLCollection.INSERT_USERINFO);
- //添加用户信息
- dbo.setInt(1,nextID);
- ...
- //执行
- dbo.executeUpdate();
- ...
复制代码
我关心的一个是效率,一个是安全,不知道这么做是否会有问题,如果有,该怎么做?谢谢指点了:) |
|