JDBC秒变C3P0连接池——再加连接解耦

  • 2019 年 10 月 3 日
  • 笔记

?JDBC???C3P0??????

?Java??????JDBC?????????????

  ??????????????(Class.forName(“??????”);)
  ??????????(Connection con  = DriverManager.getConnection();)
  ??????????(PreparedStatement stat = con.prepareStatement(sql);stat.executeQuery();)
  ???????????????(con.close();)
???????????????????????????????????(?????????????????????????????)?????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????
      ?????????????????????????
 
???????   
?? ?????????????????????????????????????????????????????????????jdbc???(jdbc??:????????????????????????)????????????????????????????????????????????????????????????jdbc????????????????????????????????
 
???
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????     ???????????????????????“???”?????????????????????????????????“???”?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????:

  1. ?????:??????????????,????????????????????,????????????????.
  2. ?????:?????????????,?????????????,????????????????????,????????????
  3. ?????????????????:????????????,?????????????????????????????.??,??????????????????????????,?????????????????????????

????c3p0?????????????

?1?  ???????????
?2? ?????????????
?3? ??????????????
?4? ??????????????????

 

????????IDEA,????Maven??????c3p0?????????????

 

<!-- mysql??-->      <dependency>        <groupId>mysql</groupId>        <artifactId>mysql-connector-java</artifactId>        <version>5.0.5</version>      </dependency>      <!-- c3p0 -->      <dependency>        <groupId>com.mchange</groupId>        <artifactId>c3p0</artifactId>        <version>0.9.5.2</version>      </dependency>  

??

db.properties  ??????? ????src?  ???????key?????

 

 

 ??????????????

package jdbc;      import com.mchange.v2.c3p0.ComboPooledDataSource;    import java.beans.PropertyVetoException;  import java.io.FileInputStream;  import java.io.IOException;  import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.ResultSet;  import java.sql.SQLException;  import java.util.Properties;    public class JDBCUtils {      public void setConnection() throws SQLException, PropertyVetoException, IOException {          //?????          ComboPooledDataSource dataSource = new ComboPooledDataSource();          //??????          Properties properties = new Properties();          properties.load(new FileInputStream("src/main/java/db.properties"));            String driver = properties.getProperty("driver");          String url = properties.getProperty("url");          String username = properties.getProperty("username");          String password = properties.getProperty("password");            //????          Connection conn = null;          PreparedStatement ps = null;          ResultSet res = null;              dataSource.setDriverClass(driver);          dataSource.setJdbcUrl(url);          dataSource.setUser(username);          dataSource.setPassword(password);            conn = dataSource.getConnection();          String sql = "select * from user";          ps = conn.prepareStatement(sql);            res = ps.executeQuery();          while (res.next()){              System.out.println(res.getInt("id")+" "+res.getString("username")+" "+res.getString("password"));          }        }    }    class text{      public static void main(String[] args) throws PropertyVetoException, SQLException, IOException {          JDBCUtils jdbcUtils = new JDBCUtils();          jdbcUtils.setConnection();      }  }  

??