一、Druid的简单使用
1 1 try{ 2 2 //1.创建Druid数据源对象 3 3 DruidDataSource dataSource = new DruidDataSource(); 4 4 5 5 //2.设置数据库连接信息 6 6 dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 7 7 dataSource.setUrl("jdbc:mysql://localhost:3306/dbtao?useSSL=false"); 8 8 dataSource.setUsername("root"); 9 9 dataSource.setPassword(""); 1010 //设置连接池初始化的连接个数 1111 dataSource.setInitialSize(5); 1212 1313 //3.从数据库连接池获取数据库连接 1414 // 首次从连接池获取连接时,会初始化话连接池中的连接数 1515 Connection conn = dataSource.getConnection(); 1616 PreparedStatement st = conn.prepareStatement("select * from emp"); 1717 ResultSet rs = st.executeQuery(); 1818 while(rs.next()){ 1919 System.out.println(rs.getString("ename")); 2020 } 2121 rs.close(); 2222 st.close(); 2323 //4.释放数据库连接,不是关闭数据库连接,而是放回了数据库连接池中 2424 conn.close(); 2525 //5.关闭数据源 2626 dataSource.close(); 2727 }catch(Exception e){ 2828 e.getStackTrace(); 2929 }
二、从工厂获取数据源
1 1 try{ 2 2 //1.设置数据库连接信息 3 3 /** Map 构造 4 4 Map<String,String> map =new HashMap<>(); 5 5 map.put("driverClassName", "com.mysql.jdbc.Driver"); 6 6 map.put("url", "jdbc:mysql://localhost:3306/dbtao?useSSL=false"); 7 7 map.put("username", "root"); 8 8 map.put("password", ""); 9 9 DataSource dataSource = DruidDataSourceFactory.createDataSource(map); 1010 */ 1111 1212 Properties prop = new Properties(); 1313 /**Properties 逐个设置 1414 prop.setProperty("driverClassName", "com.mysql.jdbc.Driver"); 1515 prop.setProperty("url", "jdbc:mysql://127.0.0.1:3306/00?useSSL=false"); 1616 prop.setProperty("username", "root"); 1717 prop.setProperty("password", "123456"); 1818 */ 1919 2020 // 加载 Properties配置文件的内容到对象中 2121 InputStream in = new FileInputStream("jdbc.properties"); 2222 prop.load(in); 2323 2424 //2.初始化Druid数据源对象 2525 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); 2626 2727 Connection conn = dataSource.getConnection(); 2828 PreparedStatement st = conn.prepareStatement("select * from emp"); 2929 ResultSet rs = st.executeQuery(); 3030 while(rs.next()){ 3131 System.out.println(rs.getString("ename")); 3232 } 3333 3434 rs.close(); 3535 st.close(); 3636 //4.释放数据库连接,不是关闭数据库连接,而是放回了数据库连接池中 3737 conn.close(); 3838 3939 }catch(Exception e){ 4040 e.getStackTrace(); 4141 }
三、封装 DBUtil
1 1 import java.io.*; 2 2 import java.sql.*; 3 3 import java.util.*; 4 4 import javax.sql.*; 5 5 import com.alibaba.druid.pool.DruidDataSourceFactory; 6 6 7 7 public class DBUtil { 8 8 static DataSource dataSource; 9 9 static { 1010 try { 1111 Properties prop = new Properties(); 1212 // 1.创建一个字节输入流的对象 1313 InputStream in = new FileInputStream("jdbc.properties"); 1414 prop.load(in); 1515 // 2.初始化数据源 1616 dataSource = DruidDataSourceFactory.createDataSource(prop); 1717 System.out.println("初始化数据源信息成功...."); 1818 } catch (Exception e) { 1919 System.out.println("初始化数据源信息异常...."); 2020 e.printStackTrace(); 2121 } 2222 } 2323 /** 2424 * 获取数据库连接池 2525 */ 2626 public static DataSource getDataSource() { 2727 return dataSource; 2828 } 2929 /** 3030 * 获取数据库连接 3131 */ 3232 public static Connection getConnection() { 3333 Connection conn = null; 3434 try { 3535 conn = dataSource.getConnection(); 3636 } catch (SQLException e) { 3737 e.printStackTrace(); 3838 } 3939 return conn; 4040 } 4141 /** 4242 * 释放数据库连接 4343 */ 4444 public static void close(Connection conn,Statement st,ResultSet rs) { 4545 if(rs!=null) { 4646 try { 4747 rs.close(); 4848 } catch (SQLException e) { 4949 e.printStackTrace(); 5050 } 5151 } 5252 close(conn,st); 5353 } 5454 public static void close(Connection conn,Statement st) { 5555 if(st!=null) { 5656 try { 5757 st.close(); 5858 } catch (SQLException e) { 5959 e.printStackTrace(); 6060 } 6161 } 6262 close(conn); 6363 } 6464 public static void close(Connection conn) { 6565 if(conn!=null) { 6666 try { 6767 conn.close(); 6868 } catch (SQLException e) { 6969 e.printStackTrace(); 7070 } 7171 } 7272 } 7373 public static void commit(Connection conn){ 7474 if(conn!=null) { 7575 try { 7676 conn.commit(); 7777 } catch (SQLException e) { 7878 e.printStackTrace(); 7979 } 8080 } 8181 } 8282 public static void rollback(Connection conn){ 8383 if(conn!=null) { 8484 try { 8585 conn.rollback(); 8686 } catch (SQLException e) { 8787 e.printStackTrace(); 8888 } 8989 } 9090 } 9191 }
jdbc.properties:
1# database config 2driverClassName = com.mysql.jdbc.Driver 3url = jdbc:mysql://localhost:3306/dbtao?useSSL=false 4username = root 5password = 123456