目錄:
java
package com.it.common; import com.it.utils.Account; import com.itheima.uitl.JDBCUtil02; import org.junit.Test; import javax.sql.rowset.JdbcRowSet; import java.sql.*; public class CommonCRUDUtil { class A implements ResultHandler<Account>{ @Override public Account handler(ResultSet resultSet) { try { while (resultSet.next()){ Account account = new Account(); String name = resultSet.getString("name"); int money = resultSet.getInt("money"); account.setMoney(money); account.setName(name); return account; } }catch (SQLException e){ e.printStackTrace(); } return null; } } //通用的查方法 @Test public void testFindall(){ Account account = findall("select * from account where id=?",new A(),6); System.out.println(account.toString()); } public <T> T findall(String sql, ResultHandler<T> handler, Object...args){ Connection conn = null; PreparedStatement ps = null; try { conn = JDBCUtil02.getConn(); ps = conn.prepareStatement(sql); ParameterMetaData metaData = ps.getParameterMetaData(); int count = metaData.getParameterCount(); for (int i = 0; i < count; i++) { ps.setObject(i+1, args[i]); } // 查詢獲得結果集 ResultSet resultSet = ps.executeQuery(); // 把獲得的結果集交給調用者,讓它去封裝數據 T t = (T) handler.handler(resultSet); return t; } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtil02.release(conn, ps); } return null; } // 通用的增刪改方法 @Test public void testUpdate(){ updata02("insert into account values(?,?,?)",8,6000,"womengl"); } public void update(String sql, Object...args){ Connection conn = null; PreparedStatement ps = null; try { conn = JDBCUtil02.getConn(); ps = conn.prepareStatement(sql); for (int i = 0; i < args.length; i++) { ps.setObject(i+1, args[i]); } ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtil02.release(conn, ps); } } public void updata02(String sql, Object ... args){ Connection conn = null; PreparedStatement ps = null; try { conn = JDBCUtil02.getConn(); ps = conn.prepareStatement(sql); // 獲取參數源數據對象 ParameterMetaData metaData = ps.getParameterMetaData(); // 獲取參數的總數 int count = metaData.getParameterCount(); for (int i = 0; i < count; i++) { ps.setObject(i+1, args[i]); } ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtil02.release(conn, ps); } } }
package com.it.common; import java.sql.ResultSet; public interface ResultHandler<T> { // 定義了一個數據封裝的規則,或者是規範 T handler(ResultSet resultSet); }
package com.itheima.uitl; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JDBCUtil02 { static ComboPooledDataSource dataSource = null; static{ dataSource = new ComboPooledDataSource(); } // public static DataSource getDataSource(){ // return dataSource; // } /** * 獲取鏈接對象 * @return * @throws SQLException */ public static Connection getConn() throws SQLException{ return dataSource.getConnection(); } /** * 釋放資源 * @param conn * @param st * @param rs */ public static void release(Connection conn , Statement st , ResultSet rs){ closeRs(rs); closeSt(st); closeConn(conn); } public static void release(Connection conn , Statement st){ closeSt(st); closeConn(conn); } private static void closeRs(ResultSet rs){ try { if(rs != null){ rs.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ rs = null; } } private static void closeSt(Statement st){ try { if(st != null){ st.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ st = null; } } private static void closeConn(Connection conn){ try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ conn = null; } } }
jdbc配置mysql
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost/wzdata name=root password=root
c3p0-configsql
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- This is default config! --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/wzdata</property> <property name="user">root</property> <property name="password">123456</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="minPoolSize">30</property> <property name="maxPoolSize">100</property> </default-config> </c3p0-config>