package JDBC;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/*
* 改进DBUtil,使得connection不需要传参
* */
public class DBUtil2 {
//数据库驱动
private static String driver;
//连接数据库的路径
private static String url;
//连接数据库的用户名
private static String user;
//连接数据库的密码
private static String pwd;
//用于管理不同线程所获取的连接
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
//静态块:类被虚拟机加载时执行一次
static {
try {
//读取配置文件
Properties properties = new Properties();
//更加推荐的相对路径写法
InputStream inputStream = JDBCDemon4.class.getClassLoader()
.getResourceAsStream("JDBC/config.properties");
properties.load(inputStream);
driver = properties.getProperty("driver").trim();
url = properties.getProperty("url").trim();
pwd = properties.getProperty("pwd").trim();
user = properties.getProperty("user").trim();
// System.out.println("Driver:" + driver);
// System.out.println("user:" + user);
// System.out.println("url:" + url);
// System.out.println("pwd:" + pwd);
inputStream.close();
//获取驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取一个连接
*/
public static Connection getConnection() throws Exception{
try {
/*
* 通过DriverManager创建一个数据库连接并返回
* */
Connection connection = DriverManager.getConnection(url,user,pwd);
/*
* ThreadLocal的set方法会将当前线程作为key
* 并将给定的值作为value存入内部的map中保存
* */
threadLocal.set(connection);
return connection;
} catch (Exception e) {
e.printStackTrace();
//通知调用者,创建连接出错
throw e;
}
}
/**
* 关闭给定的连接
*/
public static void closeConnection(){
try{
//在ThreadLocal中获取connection即(value值)
Connection connection = threadLocal.get();
if(connection != null){
connection.close();
//删除value值,防止污染
threadLocal.remove();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
package JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
/**
* Statment主要用于执行静态的sql语句,,内容不变的sql,每执行一次都要对sql语句进行编译
*
* sql语句中只是其中参数有所不同,则适用于preparedStament,sql语句提前编译
* 继承与Statment,其中execute,executeQuery,executeUpdate被更改,不需要参数
* */
//使用预编译sql提高执行效率
public class JDBCPreSta1 {
public static void main(String[] args) {
try{
//建立连接
Connection connection = DBUtil2.getConnection();
/*
* 使用preparedAtatment,预编译sql
* */
// String sql = "insert into user_demon values (?,?,'123456',?,?)";
// //System.out.println(sql);
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// //记录开始时间
// long start = System.currentTimeMillis();
// for(int i = 2000;i < 2100;i++){
// preparedStatement.setInt(1,i);
// preparedStatement.setString(2,"test"+i);
// preparedStatement.setInt(3,5000);
// preparedStatement.setString(4,"test"+i+"qq.com");
// preparedStatement.executeUpdate();
// }
// //结束时间
// long end = System.currentTimeMillis();
// //输出插入完毕总耗时
// System.out.println("执行完毕,耗时:"+(end - start) +"秒");
/*
* 使用Statment未经过预编译的sql
* */
//获取Statment
Statement statement = connection.createStatement();
long start = System.currentTimeMillis();
for(int i = 3000;i <3100;i++){
//String sql = "insert into user_demon values(1,'test"+i+"','12345',5000,'test"+i+"@qq.com')";
String sql ="insert into user_demon " +
"values " +
"("+i+","+
"'test"+i+"',"+
"'12345',"+
"5000,"+
"'test"+i+"@qq.com'" +
")";
statement.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.println("执行完毕,耗时:"+(end - start) +"秒");
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil2.closeConnection();
}
}
}