JDBC资源包
链接: https://pan.baidu.com/s/1JaZ3...
提取码:858d
public class SqlUtils { private static String driver = "com.mysql.jdbc.Driver"; private static String url = ""; private static String username = ""; private static String password = ""; static { //加载驱动 try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //只产生一个连接 public static Connection getConnection() throws SQLException { System.out.println("连接数据库..."); return DriverManager.getConnection(url, username, password); } public static void release(Connection connection, Statement statement, ResultSet resultSet) { 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(); } } } }
public class test { public static void main(String[] args) throws SQLException { //加载驱动,创建连接 Connection connection = SqlUtils.getConnection(); //SQL语句 String insert = "INSERT INTO `user` (id,username,password) VALUE (?,?,?);"; String select = "SELECT * FROM `user`;"; //将事务设置为手动提交 connection.setAutoCommit(false); PreparedStatement statement1; PreparedStatement statement2; try{ //根据sql语句,得到预编译语句对象 statement1 = connection.prepareStatement(insert); statement2 = connection.prepareStatement(select); for(int i=1;i<=10;i++){ //按占位符设置参数值 statement1.setInt(1,i); statement1.setString(2,"user_"+i); statement1.setString(3,"password"+i); //放入批处理队列 statement1.addBatch(); } //执行插入语句,批量插入,事务 statement1.executeBatch(); //执行查询语句,得到结果集 ResultSet result = statement2.executeQuery(); //遍历、打印结果 while (result.next()){ System.out.println("username:"+result.getObject("username")+";password:"+result.getObject("password")); } //提交事务 connection.commit(); //关闭连接,释放资源 SqlUtils.release(connection,statement1,result); SqlUtils.release(connection,statement2,result); }catch (Exception e){ e.printStackTrace(); } } }
PreparedStatement继承Statement,PreparedStatement包含已编译的SQL语句,其执行速度比Statement要快。Statement会频繁编译SQL。PreparedStatement可对SQL进行预编译,提高效率,预编译的SQL存储在PreparedStatement对象中。
在JDBC中,任何时候都不要用Statement,因为:
准备:建立一个数据库缓冲池
最小连接数:连接池一直保持的连接数
最大连接数:连接池可以申请最大的连接数,如果超过,后面的数据连接请求进入等待队列
连接池一直保持着不少于最小连接数的数量,当数量不够时,数据库会创建新的连接,直到达到最大的连接数,之后数据库会一直等待
常见开源的连接池:DBCP、C3P0、Druid