1、JDBC链接数据库Demo

相关Jar包:mysql-connector-java-8.0.23.jar

  1. public static void main(String[] args) {
  2. Statement stmt = null;
  3. Connection conn = null;
  4. try {
  5. // 1.注册驱动
  6. Class.forName("com.mysql.jdbc.Driver");
  7. // 2.定义SQL
  8. String sql = "insert into account values(null,'王五',300)";
  9. // 3.获取Connection对象
  10. conn = DriverManager.getConnection("jdbc:mysql://10.16.163.76:3306/dbtest","hhyu","Yhh920205");
  11. // 4.获取SQL执行对象 Statement
  12. stmt = conn.createStatement();
  13. // 5.执行
  14. int count = stmt.executeUpdate(sql);
  15. // 6.结果
  16. System.out.println(count);
  17. if(count > 0){
  18. System.out.println("添加成功!");
  19. }
  20. else{
  21. System.out.println("添加失败!");
  22. }
  23. } catch (ClassNotFoundException | SQLException e) {
  24. e.printStackTrace();
  25. }
  26. finally {
  27. if(conn !=null){
  28. try {
  29. // 7.释放
  30. conn.close();
  31. } catch (SQLException throwables) {
  32. throwables.printStackTrace();
  33. }
  34. }
  35. if(stmt !=null){
  36. try {
  37. stmt.close();
  38. } catch (SQLException throwables) {
  39. throwables.printStackTrace();
  40. }
  41. }
  42. }
  43. }

2、JDBC 使用PreparedStatement获取数据

  1. /**
  2. * 使用 PreparedStatement 防止SQL注入
  3. * @param username
  4. * @param password
  5. * @return
  6. */
  7. public boolean loginByPrepare(String username, String password){
  8. if(username == null || password == null){
  9. return false;
  10. }
  11. Connection conn = null;
  12. PreparedStatement pstmt = null;
  13. ResultSet rs = null;
  14. // 变量形式生成Query
  15. String sql = String.format("select * from user where username=? and password=?",username,password);
  16. try {
  17. conn = JDBCUtil.getConnection();
  18. pstmt = conn.prepareStatement(sql); //Prepared
  19. pstmt.setString(1,username); //补充参数
  20. pstmt.setString(2,password);
  21. rs = pstmt.executeQuery();
  22. return rs.next();
  23. } catch (SQLException throwables) {
  24. throwables.printStackTrace();
  25. }
  26. finally {
  27. JDBCUtil.close(pstmt,conn,rs);
  28. }
  29. return false;
  30. }

3、JDBC 提交事务

  1. public static void main(String[] args) {
  2. Connection conn = null;
  3. PreparedStatement pstmt1 = null;
  4. PreparedStatement pstmt2 = null;
  5. String sql1 = "update user set username='NewJerry' where id = ?";
  6. String sql2 = "update user set username='NewTom' where id = ?";
  7. try {
  8. conn = JDBCUtil.getConnection();
  9. conn.setAutoCommit(false);//开启事务
  10. pstmt1 = conn.prepareStatement(sql1);
  11. pstmt2 = conn.prepareStatement(sql2);
  12. pstmt1.setInt(1,1);
  13. pstmt2.setInt(1,2);
  14. pstmt1.executeUpdate();
  15. pstmt2.executeUpdate();
  16. conn.commit();//提交事务
  17. } catch (SQLException throwables) {
  18. try {
  19. if(conn != null)
  20. conn.rollback();//回滚事务
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. throwables.printStackTrace();
  25. }
  26. finally {
  27. JDBCUtil.close(pstmt1,conn);
  28. JDBCUtil.close(pstmt2,null);
  29. }
  30. }

4、数据库链接池(C3P0)

1. 引入jar 包 c3p0-0.9.5.5.bin.zip

2. 在根目录下创建c3p0.properties 或 c3p0-config.xml 文件

  1. <c3p0-config>
  2. <default-config>
  3. <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
  4. <property name="jdbcUrl">jdbc:mysql://10.16.163.76:3306/dbtest</property>
  5. <property name="user">hhyu</property>
  6. <property name="password">Yhh920205</property>
  7. <property name="initialPoolSize">5</property>
  8. <property name="maxPoolSize">10</property>
  9. <property name="checkoutTimeout">3000</property>
  10. </default-config>
  11. </c3p0-config>

3. 执行(获取Connection对象)

  1. DataSource ds = new ComboPooledDataSource();
  2. Connection conn = ds.getConnection();

5、数据库连接池(Druid)

1. 引入jar 包 druid-1.2.5.jar

2. 创建properties文件

  1. driverClassName=com.mysql.cj.jdbc.Driver
  2. url=jdbc:mysql://10.16.163.76:3306/dbtest
  3. username=hhyu
  4. password=Yhh920205
  5. initialSize=5
  6. maxActive=10
  7. maxWait=3000

3. 获取Connection对象

  1. var pro = new Properties();
  2. pro.load(DruidConnectionPool.class.getClassLoader().getResourceAsStream("druid.properties"););
  3. DataSource ds = DruidDataSourceFactory.createDataSource(pro);
  4. Connection conn = ds.getConnection();