DBUtil数据库工具封装

  • package org.idcn.util   
  •   
  • import java.io.InputStream;   
  • import java.sql.Connection;   
  • import java.sql.Date;   
  • import java.sql.PreparedStatement;   
  • import java.sql.ResultSet;   
  • import java.sql.SQLException;   
  • import java.sql.Time;   
  • import java.sql.Timestamp;   
  • import javax.sql.DataSource;   
  •   
  • /**  
  •  * This class encapsulation the Connection and PreparedStatement  
  •  * deal with database  
  •  *   
  •  * It be designed as a singleton class  
  •  *   
  •  * @author zdxue  
  •  *  
  •  */  
  • public class DBUtil {   
  •     private Connection conn = null;        //Connection object   
  •     private PreparedStatement prepStmt = null;    //PreparedStatement object   
  •   
  •     /**  
  •      * create DBUtil with Connection and sql  
  •      *   
  •      * @param conn Connection  
  •      * @param sql sql statement  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     private DBUtil(Connection conn, String sql) throws SQLException  {   
  •         this.conn = conn;   
  •         prepStmt = conn.prepareStatement(sql,   
  •                 ResultSet.TYPE_SCROLL_INSENSITIVE,   
  •                 ResultSet.CONCUR_READ_ONLY);   
  •     }   
  •   
  •     /**  
  •      * create DBUtil with dataSource and sql   
  •      *   
  •      * @param ds DataSource   
  •      * @param sql sql statement  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     private DBUtil(DataSource ds, String sql) throws SQLException  {   
  •         conn = ds.getConnection();   
  •         prepStmt = conn.prepareStatement(sql,   
  •                 ResultSet.TYPE_SCROLL_INSENSITIVE,   
  •                 ResultSet.CONCUR_READ_ONLY);   
  •     }   
  •   
  •     /**  
  •      * the static method to get DBUtil instance   
  •      *   
  •      * @param connection java.sql.Connection  
  •      * @param sql sql statement  
  •      * @return DBUtil DBUtil instance  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public static DBUtil getInstance(Connection connection, String sql) throws SQLException {   
  •         return new DBUtil(connection, sql);   
  •     }   
  •   
  •     /**  
  •      * static method to get DBUtil instance  
  •      *   
  •      * @param dataSource dataSource  
  •      * @param sql sql statement  
  •      * @return DBUtil DBUtil instance  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public static DBUtil getInstance(DataSource dataSource, String sql) throws SQLException {   
  •         return new DBUtil(dataSource, sql);   
  •     }   
  •   
  •     /**  
  •      * get Connection   
  •      *   
  •      * @return connection java.sql.Conncetion instance  
  •      */  
  •     public Connection getConnection() {   
  •         return conn;   
  •     }   
  •   
  •     /**  
  •      * get preparedStatement  
  •      *   
  •      * @return preparedStatement java.sql.preparedStatement instance  
  •      */  
  •     public PreparedStatement getPreparedStatement() {   
  •         return prepStmt;   
  •     }   
  •   
  •     /**  
  •      * execute Query from database  
  •      *   
  •      * @return ResultSet  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public ResultSet executeQuery() throws SQLException {           
  •         return prepStmt.executeQuery();   
  •     }   
  •   
  •     /**  
  •      * execute update to the database  
  •      *   
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public int executeUpdate() throws SQLException {   
  •         if(prepStmt == null)   
  •             return 0;   
  •         return prepStmt.executeUpdate();   
  •     }   
  •   
  •     /**  
  •      * close the connection and preparedStatment  
  •      */  
  •     public void close() {   
  •         try {   
  •             if (prepStmt != null) {   
  •                 prepStmt.close();   
  •                 prepStmt = null;   
  •             }   
  •             if(conn != null) {   
  •                 conn.close();   
  •                 conn = null;   
  •             }   
  •         } catch (Exception e) {               
  •         }   
  •     }   
  •   
  •     /**  
  •      * set the String value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value String parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setString(int index,String value) throws SQLException {   
  •         prepStmt.setString(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the int value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value int parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setInt(int index,int value) throws SQLException {   
  •         prepStmt.setInt(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the Double value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value Double parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setDouble(int index,Double value) throws SQLException {   
  •         prepStmt.setDouble(index, value);   
  •     }   
  •   
  •     /**  
  •      * set the boolean value for the preparedStatement  
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value boolean parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setBoolean(int index,boolean value) throws SQLException {   
  •         prepStmt.setBoolean(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the Date value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value Date parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setDate(int index,Date value) throws SQLException {   
  •         prepStmt.setDate(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the Time value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value Time parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setTime(int index,Time value) throws SQLException {   
  •         prepStmt.setTime(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the TimeStampe value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value java.sql.Timestamp parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setTimestamp(int index,Timestamp value) throws SQLException {   
  •         prepStmt.setTimestamp(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the long value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value long parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setLong(int index,long value) throws SQLException {   
  •         prepStmt.setLong(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the float value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param value float parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setFloat(int index,float value) throws SQLException {   
  •         prepStmt.setFloat(index,value);   
  •     }   
  •   
  •     /**  
  •      * set the Object value for the preparedStatement  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param obj Object parameter value  
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setObject(int index, Object obj) throws SQLException {   
  •         prepStmt.setObject(index, obj);   
  •     }   
  •   
  •     /**  
  •      * set binaryStream  
  •      *   
  •      * @param index the first parameter is 1, seconed is 2, and so on.  
  •      * @param in binayStream  
  •      * @param length the byte length of the stream   
  •      * @throws SQLException if occur database access wrong  
  •      */  
  •     public void setBinaryStream(int index,InputStream in,int length) throws SQLException {   
  •         prepStmt.setBinaryStream(index,in,length);   
  •     }   
  •   
  •     /**  
  •      * transaction commit  
  •      */  
  •     public void commit() {   
  •         try {   
  •             conn.commit();   
  •         } catch(Exception e) {   
  •             e.printStackTrace();   
  •         }   
  •     }   
  •   
  •     /**  
  •      * transaction rollback  
  •      */  
  •     public void rollback() {   
  •         try {   
  •             conn.rollback();   
  •         }    
  •         catch(Exception e) {   
  •             e.printStackTrace();   
  •         }   
  •     }   
  • }  
  • posted @ 2009-01-09 20:32  猪鼻驴耳  阅读(135)  评论(0)    收藏  举报