MySQL学习笔记(狂神说Java) 狂神说B站视频:https://www.bilibili.com/video/BV1NJ411J79W?p=1
MySQL官网:https://www.mysql.com/
MySQL教程:https://www.runoob.com/mysql/mysql-tutorial.html
一、数据库驱动 驱动:声卡、显卡、数据库
我们的程序会通过数据库驱动和数据库打交道!
二、JDBC Maven下载链接
MySQL官网下载JDBC
三、第一个JDBC程序
1.创建测试数据库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 -- JDBC CREATE DATABASE jdbcStudy CHARACTER SET utf8 COLLATE utf8_general_ci; USE jdbcStudy; CREATE TABLE `users`( id INT PRIMARY KEY, NAME VARCHAR(40), PASSWORD VARCHAR(40), email VARCHAR(60), birthday DATE ); INSERT INTO `users`(id,NAME,PASSWORD,email,birthday) VALUES(1,'zhansan','123456','zs@sina.com','1980-12-04'), (2,'lisi','123456','lisi@sina.com','1981-12-04'), (3,'wangwu','123456','wangwu@sina.com','1979-12-04')
2.导入数据库驱动
3.编写测试代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package JDBC;import java.sql.*;public class Demo01 { public static void main (String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost|:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true" ; String username = "root" ; String password = "123456" ; Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); String sql = "select * from users" ; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()){ System.out.println(resultSet.getObject("id" )); System.out.println(resultSet.getObject("NAME" )); System.out.println(resultSet.getObject("PASSWORD" )); System.out.println(resultSet.getObject("email" )); System.out.println(resultSet.getObject("birthday" )); System.out.println("=====================================" ); } statement.close(); connection.close(); connection.close(); } }
步骤总结 : 加载驱动 - 连接数据库(DriverManager) - 获取执行对象(Statement) - 获得返回结果集 - 释放连接
4.常用对象分析 DriverManager 1 2 3 4 5 6 7 8 Class.forName("com.mysql.jdbc.Driver" ); Connection connection = DriverManager.getConnection(url, username, password); connection.commit(); connection.rollback(); connection.setAutoCommit();
URL 1 2 3 4 5 6 7 8 9 10 String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true" ; jdbc:mysql
Statement 1 2 3 4 5 6 7 8 9 10 PreparedStatement Statement statement = connection.createStatement();String sql = "select * from users" ; statement.executeQuery(); statement.executeUpdate(); statement.execute(); statement.clearBatch();
ResultSet
获得指定数据类型
1 2 3 4 5 6 7 resultSet.getObject(); resultSet.getDouble(); resultSet.getString(); resultSet.getFloat(); resultSet.getInt(); .....
遍历(指针)
1 2 3 4 5 resultSet.beforeFirst(); resultSet.afterLast(); resultSet.next(); resultSet.previous(); resultSet.absolute(row);
释放资源
1 2 3 statement.close(); connection.close(); connection.close();
四、statement对象 JDBC中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可。
Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sql语句,executeUpdate执行完后,将会返回一个整数(即增删改语句导致了数据库几行数据发生了变化)。
Statement.executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。
1.db.properties 1 2 3 4 driver=com.mysql.jdbc.Driver url=jdbc:mysql: username=root password=123456
2.jdbcUtils 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package com.baixf.utils;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.Properties;public class jdbcUtils { private static String driver = null ; private static String url = null ; private static String username = null ; private static String password = null ; static { try { InputStream resourceAsStream = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties" ); Properties properties = new Properties (); properties.load(resourceAsStream); driver = properties.getProperty("driver" ); url = properties.getProperty("url" ); username = properties.getProperty("username" ); password = properties.getProperty("password" ); Class.forName(driver); }catch (IOException | ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection () throws SQLException { return DriverManager.getConnection(url, username, password); } public static void release (Connection connection, Statement statement, ResultSet resultSet) throws SQLException { if (resultSet!=null ) { try { resultSet.close(); }catch (SQLException e){ e.printStackTrace(); } } if (statement!=null ){ try { statement.close(); }catch (SQLException e){ e.printStackTrace(); } } if (connection!=null ){ try { connection.close(); }catch (SQLException e){ e.printStackTrace(); } } } }
3.CRUD操作 Create
使用executeUpdate(String sql)方法完成数据添加操作,示例操作:
1 2 3 4 5 6 statement st = conn.createstatement(;string sq1 = "insert into user(... . ) values .... . ) " ;int num = st.executeupdate(sq1);if (num>0 ){ system.out.println("插入成功!!! " ); }
Read
使用executeQuery(String sql)方法完成数据查询操作,示例操作:
1 2 3 4 5 statement st = conn.createstatement(;string sql = "select t from user where id=1" ;Resu7tset rs = st.executeupdate(sq1);while (rs.next({ }
Update
使用executeUpdate(String sql)方法完成数据修改操作,示例操作:
1 2 3 4 5 statement st = conn. createstatementO;string sql = "update user set name='" where name='" ";int num = st.executeupdate(sq1); if(num>0){ system.out.print1n(“修改成功!!! "); }
Delete
使用executeUpdate(String sql)方法完成数据删除操作,示例操作:
1 2 3 4 5 6 statement st = conn.createstatementO;string sql = "delete from user where id=1" ;int num = st.executeupdate(sq1);if (num>0 ){ system.out. println(“删除成功!!! "); }
4.编写增删改查操作 添加数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.baixf.lesson02;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Demo01 { public static void main (String[] args) throws SQLException ,NullPointerException{ Connection connection=null ; Statement statement=null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); statement = connection.createStatement(); String sql = "INSERT INTO `users`(id,NAME,PASSWORD,email,birthday)" + "VALUES(6,'wangwu','password','1813252727@qq.com','2020-12-25')" ; int i = statement.executeUpdate(sql); if (i>0 ){ System.out.println("插入成功!" ); } }catch (SQLException e){ e.printStackTrace(); }finally { assert statement != null ; jdbcUtils.release(connection,statement,resultSet); } } }
删除数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.baixf.lesson02;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Demo02 { public static void main (String[] args) throws SQLException { Connection connection=null ; Statement statement=null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); statement = connection.createStatement(); String str = "DELETE FROM users WHERE `ID`=6;" ; int i = statement.executeUpdate(str); if (i>0 ){ System.out.println("删除成功!" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,statement,resultSet); } } }
更新数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.baixf.lesson02;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Demo03 { public static void main (String[] args) throws SQLException { Connection connection=null ; Statement statement=null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); statement = connection.createStatement(); String str = "UPDATE users SET `NAME`='yanghongli',`email`='707401057@qq.com' WHERE `id`=5" ; int i = statement.executeUpdate(str); if (i>0 ){ System.out.println("修改成功!" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,statement,resultSet); } } }
查询数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.baixf.lesson02;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Demo04 { public static void main (String[] args) throws SQLException { Connection connection=null ; Statement statement=null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); statement = connection.createStatement(); String str = "SELECT * FROM users" ; resultSet = statement.executeQuery(str); while (resultSet.next()){ System.out.println(resultSet.getObject("id" )); System.out.println(resultSet.getObject("NAME" )); System.out.println(resultSet.getObject("PASSWORD" )); System.out.println(resultSet.getObject("email" )); System.out.println(resultSet.getObject("birthday" )); System.out.println("=====================================" ); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcUtils.release(connection,statement,resultSet); } } }
SQL注入问题
SQL存在漏洞,会被攻击导致数据泄露,SQL会被拼接。
SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编写时的疏忽,通过SQL语句,实现无账号登录,甚至篡改数据库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.baixf.lesson02;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Demo05 { public static void main (String[] args) throws SQLException { login("baixf" ,"password" ); login("" ,"password" ); login("'or '1=1" ,"123456" ); } public static void login (String username, String password) throws SQLException { Connection connection = null ; Statement statement = null ; ResultSet resultSet = null ; try { connection = jdbcUtils.getConnection(); statement = connection.createStatement(); String str = "SELECT * FROM `users` WHERE `NAME`='" + username + "' && `PASSWORD`='" + password + "'" ; resultSet = statement.executeQuery(str); while (resultSet.next()) { System.out.println(resultSet.getObject("NAME" )); System.out.println(resultSet.getObject("PASSWORD" )); System.out.println("===================================" ); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcUtils.release(connection, statement, resultSet); } } }
五、PreparedStatement 对象
PreparedStatement 对象可以防止SQL注入,而且效率更快。
1.db.properties 1 2 3 4 driver=com.mysql.jdbc.Driver url=jdbc:mysql: username=root password=123456
2.jdbcUtils 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package com.baixf.utils;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.Properties;public class jdbcUtils { private static String driver = null ; private static String url = null ; private static String username = null ; private static String password = null ; static { try { InputStream resourceAsStream = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties" ); Properties properties = new Properties (); properties.load(resourceAsStream); driver = properties.getProperty("driver" ); url = properties.getProperty("url" ); username = properties.getProperty("username" ); password = properties.getProperty("password" ); Class.forName(driver); }catch (IOException | ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection () throws SQLException { return DriverManager.getConnection(url, username, password); } public static void release (Connection connection, Statement statement, ResultSet resultSet) throws SQLException { if (resultSet!=null ) { try { resultSet.close(); }catch (SQLException e){ e.printStackTrace(); } } if (statement!=null ){ try { statement.close(); }catch (SQLException e){ e.printStackTrace(); } } if (connection!=null ){ try { connection.close(); }catch (SQLException e){ e.printStackTrace(); } } } }
3.CRUD操作 增加数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.baixf.lesson03;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.*;public class Demo01 { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; try { connection = jdbcUtils.getConnection(); String str = "INSERT INTO `users`(id,NAME,PASSWORD,email,birthday) VALUES(?,?,?,?,?)" ; preparedStatement = connection.prepareStatement(str); preparedStatement.setInt(1 ,6 ); preparedStatement.setString(2 ,"王冰冰" ); preparedStatement.setString(3 ,"bxfwlg" ); preparedStatement.setString(4 ,"1813252727@qq.com" ); preparedStatement.setDate(5 , new Date (2000 )); int i = preparedStatement.executeUpdate(); if (i>0 ){ System.out.println("插入成功!" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,null ); } } }
删除数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.baixf.lesson03;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.SQLException;public class Demo02 { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; try { connection = jdbcUtils.getConnection(); String str = "delete from users where name = ?" ; preparedStatement = connection.prepareStatement(str); preparedStatement.setString(1 ,"baixf" ); int i = preparedStatement.executeUpdate(); if (i>0 ){ System.out.println("删除成功!" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,null ); } } }
更新数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.baixf.lesson03;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class Demo03 { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; try { connection = jdbcUtils.getConnection(); String str = "update users set birthday = ? where name = ?" ; preparedStatement = connection.prepareStatement(str); preparedStatement.setString(1 ,"2020-12-25" ); preparedStatement.setString(2 ,"王冰冰" ); int i = preparedStatement.executeUpdate(); if (i>0 ){ System.out.println("更新成功!" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,null ); } } }
查询数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.baixf.lesson03;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Demo06 { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; ResultSet resultSet = null ; try { connection = jdbcUtils.getConnection(); String str = "select * from users where name = ?" ; preparedStatement = connection.prepareStatement(str); preparedStatement.setString(1 ,"王冰冰" ); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println("查询成功!" ); System.out.println(resultSet.getObject("id" )); System.out.println(resultSet.getObject("NAME" )); System.out.println(resultSet.getObject("PASSWORD" )); System.out.println(resultSet.getObject("email" )); System.out.println(resultSet.getObject("birthday" )); System.out.println("=====================================" ); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,null ); } } }
防止注入问题
PreparedStatement 对象防止SQL注入的本质:把传递的参数当作字符
假设存在转义字符,将直接被忽略 ,例如 ‘ (引号)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.baixf.lesson03;import com.baixf.lesson02.utils.jdbcUtils;import java.sql.*;public class Demo07 { public static void main (String[] args) throws SQLException { login("zhansan" ,"123456" ); login("'or '1=1" ,"'or '1=1" ); } public static void login (String username, String password) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; ResultSet resultSet = null ; try { connection = jdbcUtils.getConnection(); String str = "SELECT * FROM `users` WHERE `NAME`=? && `PASSWORD`=?" ; preparedStatement = connection.prepareStatement(str); preparedStatement.setString(1 ,username); preparedStatement.setString(2 ,password); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getObject("NAME" )); System.out.println(resultSet.getObject("PASSWORD" )); System.out.println("===================================" ); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcUtils.release(connection, preparedStatement, resultSet); } } }
六、使用IDEA连接数据库 1.建立连接
2.连接成功后选择数据库
3.查看数据库中的内容
4.CRU操作
5.其他操作
七、事务
要么都成功,要么都失败!
1.ACID原则
原子性:要么全部完成,要么不完成
一致性:总数不变
隔离性:多个进程互不干扰
持久性:一旦提交不可逆
2.代码实现 1.开启事务。
2.一组业务执行完毕,提交事务。
3.可以在catch语句中显示的定义 回滚语句,但默认失败就会回滚。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.baixf.lesson04;import com.baixf.utils.jdbcUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Transaction { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); connection.setAutoCommit(false ); String sql1 = "UPDATE ACCOUNT SET `money` = `money` - 100 WHERE NAME='A'" ; preparedStatement = connection.prepareStatement(sql1); preparedStatement.executeUpdate(); String sql2 = "UPDATE ACCOUNT SET `money` = `money` + 100 WHERE NAME='B'" ; preparedStatement = connection.prepareStatement(sql2); preparedStatement.executeUpdate(); connection.commit(); System.out.println("成功!" ); } catch (SQLException throwables) { connection.rollback(); throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,resultSet); } } }
失败案例
回滚!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.baixf.lesson04;import com.baixf.utils.jdbcUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Demo01 { public static void main (String[] args) throws SQLException { Connection connection = null ; PreparedStatement preparedStatement = null ; ResultSet resultSet=null ; try { connection = jdbcUtils.getConnection(); connection.setAutoCommit(false ); String sql1 = "UPDATE ACCOUNT SET `money` = `money` - 100 WHERE NAME='A'" ; preparedStatement = connection.prepareStatement(sql1); preparedStatement.executeUpdate(); int x=1 /0 ; String sql2 = "UPDATE ACCOUNT SET `money` = `money` + 100 WHERE NAME='B'" ; preparedStatement = connection.prepareStatement(sql2); preparedStatement.executeUpdate(); connection.commit(); System.out.println("成功!" ); } catch (SQLException throwables) { connection.rollback(); throwables.printStackTrace(); }finally { jdbcUtils.release(connection,preparedStatement,resultSet); } } }
八、数据库连接池 数据库连接–执行完毕–释放
连接 —》释放十分浪费资源
池化技术:准备一些预先的资源,过来就连接预先准备好的
常用连接数:10
最小连接数:10
最大连接数:100
编写连接池,实现一个接口:Datasource
1.开源数据源实现
使用了这些数据库连接池,项目中就不需要编写连接数据库的代码!
2.jdbcUtils_DBCP.properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #连接设置 DBCP数据源定义! driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql: username=root password=123456 #初始化连接 initialSize=10 #最大连接数量 maxActive=50 #最大空闲连接 maxIdle=20 #最小空闲连接 minIdle=5 #超时等待时间 maxWait=60000 #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] #注意:“user” 与 “password” 两个属性会被明确地传递,因此这里不需要包含他们。 connectionProperties=useUnicode=true ;characterEncoding=UTF8 #指定由连接池所创建的连接的自动提交(auto-commit)状态。 defaultAutoCommit=true #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。 #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE defaultTransactionIsolation=READ_UNCOMMITTED
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.baixf.utils;import org.apache.commons.dbcp2.BasicDataSource;import org.apache.commons.dbcp2.BasicDataSourceFactory;import javax.sql.DataSource;import java.io.InputStream;import java.sql.*;import java.util.Properties;public class jdbcUtils_DBCP { private static DataSource dataSource = null ; static { try { InputStream in = jdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties" ); Properties properties = new Properties (); properties.load(in); dataSource = BasicDataSourceFactory.createDataSource(properties); }catch (Exception e){ e.printStackTrace(); } } public static Connection getConnection () throws SQLException { return dataSource.getConnection(); } public static void release (Connection connection, Statement statement, ResultSet resultSet) throws SQLException { if (resultSet!=null ) { try { resultSet.close(); }catch (SQLException e){ e.printStackTrace(); } } if (statement!=null ){ try { statement.close(); }catch (SQLException e){ e.printStackTrace(); } } if (connection!=null ){ try { connection.close(); }catch (SQLException e){ e.printStackTrace(); } } } }
3.c3p0-config.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default -config> <!-- 连接参数 --> <property name="driverClass" >com.mysql.jdbc.Driver</property> <property name="jdbcUrl" >jdbc:mysql: <property name="user" >root</property> <property name="password" >root</property> <!-- 连接池参数 --> <!--初始化申请的连接数量--> <property name="initialPoolSize" >5 </property> <!--最大的连接数量--> <property name="maxPoolSize" >10 </property> <!--超时时间--> <property name="checkoutTimeout" >3000 </property> </default -config> <named-config name="otherc3p0" > <!-- 连接参数 --> <property name="driverClass" >com.mysql.jdbc.Driver</property> <property name="jdbcUrl" >jdbc:mysql: <property name="user" >root</property> <property name="password" >root</property> <!-- 连接池参数 --> <property name="initialPoolSize" >5 </property> <property name="maxPoolSize" >8 </property> <property name="checkoutTimeout" >1000 </property> </named-config> </c3p0-config>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 package com.itheima.utils;import java.io.IOException;import java.sql.*;import java.util.Properties;public class JDBCUtils2 { private JDBCUtils2 () {} private static String driverClass; private static String url; private static String username; private static String password; public static void loadProperties () { Properties pp = new Properties (); try { pp.load(JDBCUtils2.class.getClassLoader().getResourceAsStream("config.properties" )); } catch (IOException e) { e.printStackTrace(); } driverClass = pp.getProperty("driverClass" ); url = pp.getProperty("url" ); username = pp.getProperty("username" ); password = pp.getProperty("password" ); } static { try { loadProperties(); Class.forName(driverClass); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection () { try { return DriverManager.getConnection(url, username,password); } catch (SQLException e) { e.printStackTrace(); } return null ; } public static void release (Connection conn, Statement stat, ResultSet rs) { try { if (rs != null ) { rs.close(); rs = null ; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stat != null ) { stat.close(); stat = null ; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null ) { conn.close(); conn = null ; } } catch (SQLException e) { e.printStackTrace(); } } } } public static void release (Connection conn, Statement stat) { try { if (stat != null ) { stat.close(); stat = null ; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null ) { conn.close(); conn = null ; } } catch (SQLException e) { e.printStackTrace(); } } } }
参考文章