JDBC概述

JDBC复习回顾

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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
* @author zss
*/
public class GetInfor {
public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
Properties properties=new Properties();
properties.load(new FileReader("src\\Information.properties"));
String jdbc= properties.getProperty("jdbcJar");
String url= properties.getProperty("url");
String password= properties.getProperty("password");
String user= properties.getProperty("user");
Class.forName(jdbc);
Connection connection= DriverManager.getConnection(url,user,password);
String selectSql="select * from user where name= ? ";
PreparedStatement preparedStatement= connection.prepareStatement(selectSql);
preparedStatement.setString(1,"郭富城");
ResultSet resultSet= preparedStatement.executeQuery();
if (resultSet.next()){
System.out.println(resultSet.getString("name"));
}
}
}

image-20220329235249837

练习

添加custom

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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

/**
* @author zss
*/
public class CustomerAdd {
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
CustomerAdd customerAdd=new CustomerAdd();
customerAdd.addPerson();
}
public void addPerson() throws SQLException, IOException, ClassNotFoundException {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入添加的用户名");
String name=scanner.next();
System.out.println("请输入添加邮箱");
String email=scanner.next();
System.out.println("请输入添加的生日");
String birthday=scanner.next();
String mysqlInsert= "insert into customers (name,email,birth) values(?,?,?)";
dateAdd(mysqlInsert,name,email,birthday);

}
public void dateAdd(String sql,Object ...obj) throws IOException, ClassNotFoundException, SQLException {
Properties properties=new Properties();
properties.load(new FileReader("src\\Information.properties"));
String jdbc= properties.getProperty("jdbcJar");
String url= properties.getProperty("url");
String password= properties.getProperty("password");
String user= properties.getProperty("user");
Class.forName(jdbc);
Connection connection= DriverManager.getConnection(url,user,password);
PreparedStatement preparedStatement= connection.prepareStatement(sql);
for (int i=0;i<obj.length;i++){
preparedStatement.setObject(i+1,obj[i]);
}
int count=preparedStatement.executeUpdate();
if (count==1){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}

preparedStatement.close();
connection.close();

}

}

存储数据库的图片与视频

1
preparedStatement.setBlob(4,new BufferedInputStream(new FileInputStream(new File("D:\\壁纸\\女孩子.png"))));

注意,有时候我们使用了mediumBlog,但是还是插入不进去,这是因为mysql安装目录下,my.ini文件缺少相关发配置,加上max_allowed_packet=16M

批量插入

批量插入 中使用statement必须每一次都重新编译sql语句,但是preparementStament仅仅编译一次,其余使用占位符即可,使用for循环进行插入即可,只不过每一次的占位符不同

1
2
3
4
5
6
Connection connection= DriverManager.getConnection(url,user,password);
PreparedStatement preparedStatement= connection.prepareStatement("insert into goods (name) VALUES (?)");
for (int i=0;i<200;i++){
preparedStatement.setObject(1,"goods"+i);
int count=preparedStatement.executeUpdate();
}

优化

addBatch:积攒sql语句

excuteBatch:执行sql语句

clearBatch:清空sql语句

默认情况下,mysql关闭批处理,所以我们需要一个参数开启

1
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
1
2
3
4
5
6
7
8
for (int i=500;i<=1000;i++){
preparedStatement.setString(1,"goods"+i);
preparedStatement.addBatch();
if (i%500==0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}

这样会优化我们插入的效率

优化2

关闭自动提交

1
2
3
4
5
6
7
8
9
10
11
12
Connection connection= DriverManager.getConnection(url,user,password);
connection.setAutoCommit(false);
PreparedStatement preparedStatement= connection.prepareStatement("insert into goods (name) VALUES (?)");
for (int i=500;i<=1000;i++){
preparedStatement.setString(1,"goods"+i);
preparedStatement.addBatch();
if (i%500==0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
connection.commit();