[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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.javapublic 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); ArrayList3.编写bruidlist = 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(); } }
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
@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=1234566.编写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()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)