JDBC工具类:
在上次我们使用的工具类中,我们只能使用mysql的数据库获取连接,如果要做修改则会很麻烦,这就是我们将代码写的“太死了“,我们可以通过properties集合的方式,将代码写入文件中,最后将文件中内容读取出来,这样在以后的修改中,只需要修改文件内容就好了。
package com.jdbc.util;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
private static String url = null;
private static String user = null;
private static String password = null;
private static String className = null;
static{
//随着类的加载而夹在
try {
//使用properties这个类,将配置文件中的键值对加载到集合中
Properties prop = new Properties();
prop.load(new FileInputStream("db.properties"));
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
className = prop.getProperty("className");
Class.forName(className);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public static Connection getConn(){
//注册驱动
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException();
}
}
//释放资源
public static void close(Connection conn,Statement stmt,ResultSet rs){
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
最后在我们的文件中,添加进去相关的数据库连接
url=jdbc:mysql://localhost:3306/day20
user=root
password=root
className=com.mysql.jdbc.Driver
使用JDBC进行批处理
在我们之前,我们一次操作只能发送一句sql语句,如果我们要插入2000条数据的话,用IO流的话,效率也不是很高,我们可以利用sql缓冲区,一次发送多条语句到数据库服务端来执行,这个就称之为批处理。
Statement : addBatch(String sql) 添加到sql缓冲区,暂时不发送,
int exxcuteBatch() 发送批次处理命令,发送所有的缓冲区内容,
void clearBatch() 清空所有的缓冲区内容
PreparedStatement() : void addBatch() 添加到sql缓冲区
int[] executeBatch() 执行缓冲区的所有sql语句
void clearBatch() 清空缓冲区
package com.jdbc.a_statement_preparedstatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import com.jdbc.util.JDBCUtil;
public class Test {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
//testStatement();//不使用批处理的情况下,给学生表中插入2000条数据,6230
//testStatementBatch();//使用批处理的情况下,给学生表中插入2000条数据,6170
//testPreparedStatement();//6240
testPreparedStatementBatch();//6210
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
private static void testPreparedStatementBatch() {
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = JDBCUtil.getConn();
//创建一个动态的sql语句
String sql = "insert into stu values(?,?);";
stmt = conn.prepareStatement(sql);
//型数据库插入2000条数据,一次插入一条
for (int i = 1; i <=2000; i++) {
stmt.setInt(1, i);
stmt.setString(2, "刘德华");
stmt.addBatch();
//判断每20条发送一次
if (i%20==0) {
stmt.executeBatch();
//清空缓存区
stmt.clearBatch();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
JDBCUtil.close(conn, stmt, null);
}
}
private static void testPreparedStatement() {
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = JDBCUtil.getConn();
//创建一个动态的sql语句
String sql = "insert into stu values(?,?);";
stmt = conn.prepareStatement(sql);
//型数据库插入2000条数据,一次插入一条
for (int i = 1; i <=2000; i++) {
stmt.setInt(1, i);
stmt.setString(2, "刘德华");
//发送参数
stmt.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
JDBCUtil.close(conn, stmt, null);
}
}
private static void testStatementBatch() {
Connection conn =null;
Statement stmt = null;
try{
//获取连接
conn = JDBCUtil.getConn();
//获取statement对象
stmt = conn.createStatement();
//每次给stmt这个命令行中添加20条sql语句,一次性进行发送
for (int i = 1; i <=2000; i++) {
String sql = "insert into stu values("+i+",'刘德华');";
//将这个sql先暂时添加在stmt的命令行中
stmt.addBatch(sql);
if (i%20==0) {
//批量发送sql
stmt.executeBatch();
//清空stmt的命令行列表
stmt.clearBatch();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
JDBCUtil.close(conn, stmt, null);
}
}
private static void testStatement() {
Connection conn =null;
Statement stmt = null;
try{
//获取连接
conn = JDBCUtil.getConn();
//获取statement对象
stmt = conn.createStatement();
//给数据库发送2000次sql语句,插入数据
for (int i = 1; i <=2000; i++) {
String sql = "insert into stu values("+i+",'刘德华');";
//给数据库发送sql
stmt.executeUpdate(sql);
}
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
JDBCUtil.close(conn, stmt, null);
}
}
}
JDBC获取自增长值
比如有俩张表,一张员工表,一张是部门表,在部门表中出入一个新的部门后,要在员工表中也插入一个新的员工,要求同时实现,这就要用到自增长值。
package com.jdbc.c_autoincrement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.jdbc.util.JDBCUtil;
public class IncrementDemo {
public static void main(String[] args) {
//需求:在给部门表插入一条数据的同时,再给员工表中刚刚插入的该部门插入一条员工数据
/**
* 1.给部门表中插入一个新的部门“财务部”
* 2.获取财务部对应的id
* 3.给员工表中插入一条员工数据,对应的部门就是财务部对应的id */
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn =JDBCUtil.getConn();
String sql1 = "insert into dept(deptName) values(?)";
String sql2 = "insert into employee(name,deptId) values(?,?);";
//先给部门表中插入一条数据
//PreparedStatement prepareStatement(String sql,int autoGeneratedKeys)
stmt = conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, "财务部");
stmt.executeUpdate();
//获取自增长的键值
//ResultSet getGeneratedKeys()
rs = stmt.getGeneratedKeys();//rs就是一个自增站的键的一个结果集
int deptId = 0;
while (rs.next()) {
deptId = rs.getInt(1);
}
//给员工表中插入一条数据
stmt = conn.prepareStatement(sql2);
stmt.setString(1, "岳云鹏");
stmt.setInt(2, deptId);
//发送参数并执行sql
stmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
}
}
}