Druid連接池:
1 Druid 由阿里提供 2 3 安裝步驟: 4 1 導包 durid1.0.9 jar包 5 6 2 定義配置文件 properties文件 7 8 名字任意位置也任意 加載文件 9 10 3 獲得數據庫連接池對象 通過DuridDataSourceFactory獲得 11 12 4 獲取鏈接
SpringJDBC :jdbcTemplate:
1 SpringJDBC :jdbcTemplate 2 3 定義: 4 5 Spring框架對jdbc進行了封裝 提供的一個JDBCTemplated對象簡化jdbc開發 6 7 如何用: 8 9 1 導包 libs-->add 10 11 2 創建JDBCTemplate對象,依賴於DataSource 12 13 3 調用JDBCTemplate方法 CRUD 14 15 3.1 insert增 delete刪 update改 DML語句 16 17 3.2 queryForMap() 查詢結果封裝為map 集合 將列名key value 18 19 3.3 queryForList() 查詢結果封裝List 集合 20 21 3.4 query() 查詢結果 JavaBean對象 22 23 3.5 queryForObject() 將結果封裝成對象
實例:
1 package cn.Wuchuang.JDBCTemplate; 2 3 import org.junit.Test; 4 import org.springframework.jdbc.core.BeanPropertyRowMapper; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 7 import java.util.List; 8 import java.util.Map; 9 10 public class Demo1JDBCTemplate { 11 //創建JdbcTemplate對象。 12 JdbcTemplate tem= new JdbcTemplate(JdbcUtils.JDBCUtils.getDataSource()); 13 14 //指定行添加。 15 @Test 16 public void fun1(){ 17 18 String sql = "insert into tablename1 values (3,?,?)"; 19 int i = tem.update(sql,"吳十一",521314); 20 System.out.println(i); 21 } 22 23 //添加數據。 24 @Test 25 public void fun2(){ 26 String sql = "insert into tablename1(money,uname) values(?,?)"; 27 int i = tem.update(sql,4000,"吳墨酬"); 28 System.out.println(i); 29 } 30 31 //查詢所有 32 @Test 33 public void fun3(){ 34 String sql = "select * from tablename1"; 35 List<Map<String,Object>>maps = tem.queryForList(sql); 36 for (Map<String,Object>Som:maps){ 37 System.out.println(Som); 38 } 39 } 40 41 //查詢所有記錄,將其封裝為Emp對象的List集合。 42 @Test 43 public void fun4(){ 44 String sql = "select * from emp"; 45 List<Demo1Emps> list = tem.query(sql,new BeanPropertyRowMapper<Demo1Emps>(Demo1Emps.class)); 46 for (Demo1Emps demp : list){ 47 System.out.println(demp); 48 } 49 } 50 }
Emp表的實體化對象:
1 package cn.Wuchuang.JDBCTemplate; 2 3 public class Demo1Emps { 4 private int uid; 5 private String uname; 6 private int Job_id; 7 private int mgr; 8 private String state; 9 private Double salary; 10 private Double bonus; 11 private int Dect_id; 12 13 public Demo1Emps() { 14 } 15 16 public Demo1Emps(int uid, String uname, int job_id, int mgr, String state, Double salary, Double bonus, int dect_id) { 17 this.uid = uid; 18 this.uname = uname; 19 Job_id = job_id; 20 this.mgr = mgr; 21 this.state = state; 22 this.salary = salary; 23 this.bonus = bonus; 24 Dect_id = dect_id; 25 } 26 27 public int getUid() { 28 return uid; 29 } 30 31 public void setUid(int uid) { 32 this.uid = uid; 33 } 34 35 public String getUname() { 36 return uname; 37 } 38 39 public void setUname(String uname) { 40 this.uname = uname; 41 } 42 43 public int getJob_id() { 44 return Job_id; 45 } 46 47 public void setJob_id(int job_id) { 48 Job_id = job_id; 49 } 50 51 public int getMgr() { 52 return mgr; 53 } 54 55 public void setMgr(int mgr) { 56 this.mgr = mgr; 57 } 58 59 public String getState() { 60 return state; 61 } 62 63 public void setState(String state) { 64 this.state = state; 65 } 66 67 public Double getSalary() { 68 return salary; 69 } 70 71 public void setSalary(Double salary) { 72 this.salary = salary; 73 } 74 75 public Double getBonus() { 76 return bonus; 77 } 78 79 public void setBonus(Double bonus) { 80 this.bonus = bonus; 81 } 82 83 public int getDect_id() { 84 return Dect_id; 85 } 86 87 public void setDect_id(int dect_id) { 88 Dect_id = dect_id; 89 } 90 91 @Override 92 public String toString() { 93 return "Demo1Emps{" + 94 "uid=" + uid + 95 ", uname='" + uname + '\'' + 96 ", Job_id=" + Job_id + 97 ", mgr=" + mgr + 98 ", state='" + state + '\'' + 99 ", salary=" + salary + 100 ", bonus=" + bonus + 101 ", Dect_id=" + Dect_id + 102 '}'; 103 } 104 }
JDBC工具類:
1 package JdbcUtils; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import java.io.IOException; 7 import java.sql.Connection; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.util.Properties; 12 13 /** 14 *Druid連接池工具類 15 * */ 16 public class JDBCUtils { 17 //1 定義成員變量 DataSource 18 private static DataSource ds; 19 20 static{ 21 //2 加載配置文件 獲得連接池 22 try { 23 Properties pro= new Properties(); 24 pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); 25 ds = DruidDataSourceFactory.createDataSource(pro); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 // 獲得鏈接 31 public static Connection getConnection() throws Exception { 32 return ds.getConnection(); 33 } 34 // 釋放資源 35 public static void close(Statement stmt,Connection conn){ 36 if(stmt!=null){ 37 try { 38 stmt.close(); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 } 43 if(conn!=null){ 44 try { 45 conn.close(); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } 49 } 50 } 51 public static void close(ResultSet rs,Statement stmt, Connection conn){ 52 if(rs!=null){ 53 try { 54 rs.close(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 } 59 if(stmt!=null){ 60 try { 61 stmt.close(); 62 } catch (SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 if(conn!=null){ 67 try { 68 conn.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 //獲得連接池 75 public static DataSource getDataSource(){ 76 return ds; 77 } 78 }