Druid连接池和springJDbc框架-Java(新手)

 


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 }

 

posted @ 2019-04-03 20:43  浪子。  阅读(883)  评论(0编辑  收藏  举报