资源池的创建过程,三种方式 mysql、java、idea、properties、druid

资源池的创建过程,三种方式 mysql、java、idea、properties、druid,第1张

资源池的创建过程,三种方式 mysql、java、idea、properties、druid 1.连接池

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qbDlgLSp-1637726169320)(C:Users忠你AppDataRoamingTyporatypora-user-imagesimage-20211124110429026.png)]

1.导入架包

druid-1.0.9.jar

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTcxcaJt-1637726169323)(C:Users忠你AppDataRoamingTyporatypora-user-imagesimage-20211124114920308.png)]

2.编写jdbcUtils.java
public static void main(String[] args) throws Exception {
queryMore("select * from t_emps");
    }

    public static void queryMore(String sql) throws Exception {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban", "root", "123456");

        Statement stat = conn.createStatement();

        ResultSet query = stat.executeQuery(sql);

        ArrayList list = new ArrayList<>();

        while (query.next()){
            String id = query.getString("id");
            String name = query.getString("name");
            String sex = query.getString("sex");
            Date birth = query.getDate("birth");
            double salary = query.getDouble("salary");
            int deptId = query.getInt("deptId");

            Emps emps = new Emps(id, name, sex, birth, salary, deptId);

            list.add(emps);
            System.out.println(list);
        }
        list.stream().forEach(System.out::println);
        System.out.println(query.next());
        stat.close();
        conn.close();
    }

}
3.编写bruid
public class Test1 {

    public static void main(String[] args) throws Exception {
        
        DruidDataSource ds= new DruidDataSource();
        
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        
        ds.setUrl("jdbc:mysql://localhost:3306/bailiban");
        
        ds.setUsername("root");
        
        ds.setPassword("123456");
        

        
        for (int i = 0; i < 10; i++) {
            DruidPooledConnection con = ds.getConnection();
            System.out.println(con);
            if (i<5){
                con.close();
            }
        }



    }
}
4.创建资源池的三种方式
public static void main(String[] args) throws Exception {
    
    DruidDataSource ds= new DruidDataSource();
    
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    
    ds.setUrl("jdbc:mysql://localhost:3306/bailiban");
    
    ds.setUsername("root");
    
    ds.setPassword("123456");

    
    for (int i = 0; i < 10; i++) {
        DruidPooledConnection con = ds.getConnection();
        System.out.println(con);
        if (i<5){
            con.close();
        }
    }



}
@Test
public void test1() throws Exception {
    
    HashMap map = new HashMap<>();
    
    map.put("driverClassName","com.mysql.jdbc.Driver");
      
    map.put("url","jdbc:mysql://localhost:3306/bailiban");
    
    map.put("username","root");
    
    map.put("password","123456");
    DataSource ds = DruidDataSourceFactory.createDataSource(map);

 
    for (int i = 0; i <10 ; i++) {
        Connection con = ds.getConnection();
        System.out.println(con);

        if (i<5){
            con.close();
        }

    }


}
@Test
 public void test2() throws Exception {
     
    Properties prop = new Properties();
    prop.setProperty("driverClassName","com.mysql.jdbc.Driver");
    prop.setProperty("url","jdbc:mysql://localhost:3306/bailiban");
    prop.setProperty("username","root");
    prop.setProperty("password","123456");
    DataSource ds = DruidDataSourceFactory.createDataSource(prop);
     for (int i = 0; i <11 ; i++) {
         Connection con = ds.getConnection();
         System.out.println(con);

         if (i<5){
             con.close();
         }

     }


 }
5.编写db.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bailiban
username=root
password=123456
6.编写druidUtils.java文件
public class druidUtils {

    
   private static DataSource ds;

   
   static {

       try {
           Properties prop = new Properties();


           

           InputStream is = druidUtils.class.getClassLoader().getResourceAsStream("druid.properties");

           prop.load(is);

           ds = DruidDataSourceFactory.createDataSource(prop);

       } catch (Exception e) {
           e.printStackTrace();
       }

   }

   
    public static DataSource getDs(){
       return ds;

    }

    public static void main(String[] args) throws SQLException {

        System.out.println(getDs());
        System.out.println(getDs().getConnection());
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5596293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存