1. 程式人生 > >java的jdbc連接輔助類

java的jdbc連接輔助類

其中 jdbc exec bstr service cep get array ava

 1     @Override
 2     public void insert(String name, String type) {
 3         final String sql = "insert into service_data(serviceName,serviceType,status,serviceTime) values(?,?,?,?)";
 4         final Object[] objArr = {name,type,"新創建",null};
 5         ConnectionUtil.startAction(new ConnectionUtil() {
6 @Override 7 public void resetAction() throws SQLException { 8 preparedStatement(BaseConnection.getConnection(), sql, objArr).executeUpdate(); 9 } 10 }); 11 }

其中BaseConnection是獲取到的數據庫連接.

以上是屬於dao層的插入代碼,來插入數據庫,方法裏有自動關閉連接的方法.預編譯的位數就對應傳入的Object數組.

 1     @SuppressWarnings("unchecked")
 2     public List<ServiceData> listSelect(String serviceName, int pageCount,
 3             int pagingCout) {
 4         final String sql = "select * from service_data  where serviceName like  ? limit ?,?";
 5         final Object[] objArr = { "%" + serviceName + "%",
6 Integer.valueOf(pagingCout), Integer.valueOf(pagingCout) }; 7 return ConnectionUtil.startAction(new ConnectionUtil() { 8 @Override 9 public void resetAction() throws SQLException { 10 preparedStatement(BaseConnection.getConnection(), sql, objArr) 11 .executeQuery();// 預編譯,並設置值,並且開始查詢 12 ResultSet resultSet = getResultSet(); // 拿到結果集合 13 setReturnArray(new ArrayList<ServiceData>()); 14 while (resultSet.next()) { 15 add(new ServiceData(getInt(1), getInt(2), getString(3), 16 getString(4), getString(5), getTime(6), getInt(7))); 17 } 18 } 19 }); 20 }

以上則是封裝到Array中再關閉.

不足之處,請自行改正,封裝的本質,等同於偷懶.

基礎代碼

  1 package cn.gzsxt.service.util;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Time;
  8 import java.util.ArrayList;
  9 
 10 import cn.gzsxt.util.BaseConnection;
 11 
 12 public abstract class ConnectionUtil {
 13 
 14     public static ArrayList startAction(ConnectionUtil c) {
 15         try {
 16             c.resetAction();
 17         } catch (SQLException e) {
 18             e.printStackTrace();
 19         } finally {
 20             c.close();
 21         }
 22         return c.returnArray;
 23     }
 24     private Connection connection = BaseConnection.getConnection();
 25     private PreparedStatement prepareStatement = null;
 26     private ResultSet resultSet = null;
 27 
 28     public ArrayList returnArray = null;
 29 
 30     public void add(Object obj){
 31         returnArray.add(obj);
 32     }
 33 
 34     private void close() {
 35         if (resultSet != null)
 36             try {
 37                 resultSet.close();
 38             } catch (SQLException e) {
 39                 e.printStackTrace();
 40             }
 41         if (prepareStatement != null)
 42             try {
 43                 prepareStatement.close();
 44             } catch (SQLException e) {
 45                 e.printStackTrace();
 46             }
 47         if (connection != null)
 48             try {
 49                 connection.close();
 50             } catch (SQLException e) {
 51                 e.printStackTrace();
 52             }
 53     }
 54 
 55     public void executeQuery() throws SQLException {
 56         resultSet = prepareStatement.executeQuery();
 57     }
 58 
 59     public int executeUpdate() throws SQLException {
 60         return prepareStatement.executeUpdate();
 61     }
 62 
 63     public int getInt(int num) throws SQLException {
 64         return resultSet != null ? resultSet.getInt(num) : -1;
 65     }
 66 
 67     public Object getObject(int num) throws SQLException {
 68         return resultSet != null ? resultSet.getObject(num) : null;
 69     }
 70 
 71     public ResultSet getResultSet() {
 72         return resultSet;
 73     }
 74 
 75     public ArrayList getReturnArray() {
 76         return returnArray;
 77     }
 78 
 79     public String getString(int num) throws SQLException {
 80         return resultSet != null ? resultSet.getString(num) : null;
 81     }
 82 
 83     public Time getTime(int num) throws SQLException {
 84         return resultSet != null ? resultSet.getTime(num) : null;
 85     }
 86 
 87     
 88     public ConnectionUtil preparedStatement(Connection connection, String sql,
 89             Object[] objArr) {
 90         if (connection != null) {
 91             this.connection = connection;
 92             try {
 93                 this.prepareStatement = connection.prepareStatement(sql);
 94                 if (objArr != null) {
 95                     for (int i = 0; i < objArr.length; i++) {
 96                         prepareStatement.setObject(i + 1, objArr[i]);
 97                     }
 98                 }
 99             } catch (SQLException e) {
100                 throw new AssertionError("預編譯失敗,設置值錯誤-->" + e);
101             }
102 
103         } else {
104             throw new AssertionError("傳遞的數據庫連接為空");
105         }
106         return this;
107     }
108     public abstract void resetAction() throws SQLException;
109 
110     public void setReturnArray(ArrayList returnArray) {
111         this.returnArray = returnArray;
112     }
113 
114 }

java的jdbc連接輔助類