1.JDBC鏈接池mysql
public class JdbcTemplateDemo2 { //Junit單元測試,可讓方法獨立執行 //1. 獲取JDBCTemplate對象 private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); /** * 1. 修改1號數據的 salary 爲 10000 */ @Test public void test1(){ //2. 定義sql String sql = "update emp set salary = 10000 where id = 1001"; //3. 執行sql int count = template.update(sql); System.out.println(count); } /** * 2. 添加一條記錄 */ @Test public void test2(){ String sql = "insert into emp(id,ename,dept_id) values(?,?,?)"; int count = template.update(sql, 1015, "郭靖", 10); System.out.println(count); } /** * 3.刪除剛纔添加的記錄 */ @Test public void test3(){ String sql = "delete from emp where id = ?"; int count = template.update(sql, 1015); System.out.println(count); } /** * 4.查詢id爲1001的記錄,將其封裝爲Map集合 * 注意:這個方法查詢的結果集長度只能是1 */ @Test public void test4(){ String sql = "select * from emp where id = ? or id = ?"; Map<String, Object> map = template.queryForMap(sql, 1001,1002); System.out.println(map); //{id=1001, ename=孫悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20} } /** * 5. 查詢全部記錄,將其封裝爲List */ @Test public void test5(){ String sql = "select * from emp"; List<Map<String, Object>> list = template.queryForList(sql); for (Map<String, Object> stringObjectMap : list) { System.out.println(stringObjectMap); } } /** * 6. 查詢全部記錄,將其封裝爲Emp對象的List集合 */ @Test public void test6(){ String sql = "select * from emp"; List<Emp> list = template.query(sql, new RowMapper<Emp>() { @Override public Emp mapRow(ResultSet rs, int i) throws SQLException { Emp emp = new Emp(); int id = rs.getInt("id"); String ename = rs.getString("ename"); int job_id = rs.getInt("job_id"); int mgr = rs.getInt("mgr"); Date joindate = rs.getDate("joindate"); double salary = rs.getDouble("salary"); double bonus = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); emp.setId(id); emp.setEname(ename); emp.setJob_id(job_id); emp.setMgr(mgr); emp.setJoindate(joindate); emp.setSalary(salary); emp.setBonus(bonus); emp.setDept_id(dept_id); return emp; } }); for (Emp emp : list) { System.out.println(emp); } } /** * 6. 查詢全部記錄,將其封裝爲Emp對象的List集合 */ @Test public void test6_2(){ String sql = "select * from emp"; List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class)); for (Emp emp : list) { System.out.println(emp); } } /** * 7. 查詢總記錄數 */ @Test public void test7(){ String sql = "select count(id) from emp"; Long total = template.queryForObject(sql, Long.class); System.out.println(total); } }
/** * Druid鏈接池的工具類 */ public class JDBCUtils { //1.定義成員變量 DataSource private static DataSource ds ; static{ try { //1.加載配置文件 Properties pro = new Properties(); pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); //2.獲取DataSource ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取鏈接 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } /** * 釋放資源 */ public static void close(Statement stmt,Connection conn){ /* if(stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close();//歸還鏈接 } catch (SQLException e) { e.printStackTrace(); } }*/ close(null,stmt,conn); } public static void close(ResultSet rs , Statement stmt, Connection conn){ if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close();//歸還鏈接 } catch (SQLException e) { e.printStackTrace(); } } } /** * 獲取鏈接池方法 */ public static DataSource getDataSource(){ return ds; } }
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///db3 username=root password=root # 初始化鏈接數量 initialSize=5 # 最大鏈接數 maxActive=10 # 最大等待時間 maxWait=3000
2.JDBC druid鏈接池sql
/** * 使用新的工具類 */ public class DruidDemo2 { public static void main(String[] args) { /* * 完成添加操做:給account表添加一條記錄 */ Connection conn = null; PreparedStatement pstmt = null; try { //1.獲取鏈接 conn = JDBCUtils.getConnection(); //2.定義sql String sql = "insert into account values(null,?,?)"; //3.獲取pstmt對象 pstmt = conn.prepareStatement(sql); //4.給?賦值 pstmt.setString(1,"王五"); pstmt.setDouble(2,3000); //5.執行sql int count = pstmt.executeUpdate(); System.out.println(count); } catch (SQLException e) { e.printStackTrace(); }finally { //6. 釋放資源 JDBCUtils.close(pstmt,conn); } } }
/** * Druid演示 */ public class DruidDemo { public static void main(String[] args) throws Exception { //1.導入jar包 //2.定義配置文件 //3.加載配置文件 Properties pro = new Properties(); InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); //4.獲取鏈接池對象 DataSource ds = DruidDataSourceFactory.createDataSource(pro); //5.獲取鏈接 Connection conn = ds.getConnection(); System.out.println(conn); } }
3.JDBC c3po鏈接池數據庫
/** * c3p0的演示 */ public class C3P0Demo1 { public static void main(String[] args) throws SQLException { //1.建立數據庫鏈接池對象 DataSource ds = new ComboPooledDataSource(); //2. 獲取鏈接對象 Connection conn = ds.getConnection(); //3. 打印 System.out.println(conn); } }
/** * c3p0的演示 */ public class C3P0Demo1 { public static void main(String[] args) throws SQLException { //1.建立數據庫鏈接池對象 DataSource ds = new ComboPooledDataSource(); //2. 獲取鏈接對象 Connection conn = ds.getConnection(); //3. 打印 System.out.println(conn); } }
<c3p0-config> <!-- 使用默認的配置讀取鏈接池對象 --> <default-config> <!-- 鏈接參數 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</property> <property name="user">root</property> <property name="password">root</property> <!-- 鏈接池參數 --> <!--初始化申請的鏈接數量--> <property name="initialPoolSize">5</property> <!--最大的鏈接數量--> <property name="maxPoolSize">10</property> <!--超時時間--> <property name="checkoutTimeout">3000</property> </default-config> <named-config name="otherc3p0"> <!-- 鏈接參數 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db3</property> <property name="user">root</property> <property name="password">root</property> <!-- 鏈接池參數 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>