实现序列化接口的Student类
import java.io.Serializable;
import java.util.Date;
/*
* 实现可序列化接口
*/
public class Student implements Serializable{
private Integer id;
private String name;
private int age;
private int gender;
private String dept;
private String tel;
private String email;
private String hobby;
private String school;
private double wallet;
private int zodiac;
private String address;
public Student() {
}
public Student(Integer id, String name, int age, int gender, String dept, String tel, String email, String hobby, String school, double wallet, int zodiac, String address) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.dept = dept;
this.tel = tel;
this.email = email;
this.hobby = hobby;
this.school = school;
this.wallet = wallet;
this.zodiac = zodiac;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public double getWallet() {
return wallet;
}
public void setWallet(double wallet) {
this.wallet = wallet;
}
public int getZodiac() {
return zodiac;
}
public void setZodiac(int zodiac) {
this.zodiac = zodiac;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", dept='" + dept + '\'' +
", tel='" + tel + '\'' +
", email='" + email + '\'' +
", hobby='" + hobby + '\'' +
", school='" + school + '\'' +
", wallet=" + wallet +
", zodiac=" + zodiac +
", address='" + address + '\'' +
'}';
}
}
Student表
存储序列化Objtest表
DBHelper类
数据库连接池配置
public class DBHelper {
private static Connection conn;
private static PreparedStatement pres;
private static ResultSet rs;
private static ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
static {
try {
Class.forName(bundle.getString("Driver"));
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("数据库连接成功");
} else System.out.println("数据库连接失败");
} catch (Exception e) {
e.printStackTrace();
}
}
数据库连接池关闭
public static void free(Connection conn, PreparedStatement pres, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (pres != null) {
try {
pres.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
清空数据库表
public static void cleanTable() {
String sql1 = "truncate table student";
String sql2 = "truncate table objtest";
try {
pres = conn.prepareStatement(sql1);
pres.executeUpdate();
pres = conn.prepareStatement(sql2);
pres.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
free(conn, pres, null);
}
}
对象序列化存储
public static void save(List<Student> students){
long startTime = System.currentTimeMillis();
String sql = "insert into objtest(obj) values(?)";
try {
pres=conn.prepareStatement(sql);
for(int i = 0; i < students.size(); i ++){
pres.setObject(1, students.get(i));
pres.addBatch();
}
pres.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
free(conn, pres, null);
}
long endTime = System.currentTimeMillis();
System.out.println("序列化存储运行时间:" + (endTime - startTime));
}
对象反序列化读取
public static List<Student> read(){
long startTime = System.currentTimeMillis();
List<Student> list = new ArrayList<Student>();
String sql="select obj from objtest";
try {
pres = conn.prepareStatement(sql);
rs = pres.executeQuery();
while(rs.next()){
Blob blob = rs.getBlob(1);
InputStream is = blob.getBinaryStream();
BufferedInputStream buffer = new BufferedInputStream(is);
byte[] buff = new byte[(int) blob.length()];
while((buffer.read(buff, 0, buff.length)) != -1) {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buff));
Student student = (Student) in.readObject();
list.add(student);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
free(conn, pres, rs);
}
long endTime = System.currentTimeMillis();
System.out.println("序列化读取运行时间:" + (endTime - startTime));
return list;
}
对象原生存储
public static void saveStudent(List<Student> students) {
long startTime = System.currentTimeMillis();
String sql = "insert into student(id, name, age, gender, dept, tel, email, birth, school, wallet, zodiac, address) values (?,?,?,?,?,?,?,?,?,?,?,?)";
try {
pres=conn.prepareStatement(sql);
for (Student student : students) {
pres.setInt(1, student.getId());
pres.setString(2, student.getName());
pres.setInt(3, student.getAge());
pres.setInt(4, student.getGender());
pres.setString(5, student.getDept());
pres.setString(6, student.getTel());
pres.setString(7, student.getEmail());
pres.setDate(8, (Date) student.getBirth());
pres.setString(9, student.getSchool());
pres.setDouble(10, student.getWallet());
pres.setInt(11, student.getZodiac());
pres.setString(12, student.getAddress());
pres.addBatch();
}
pres.executeBatch();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
free(conn, pres, null);
}
long endTime = System.currentTimeMillis();
System.out.println("直接存储运行时间:" + (endTime - startTime));
}
对象原生读取
public static List<Student> readStudent() {
long startTime = System.currentTimeMillis();
List<Student> list = new ArrayList<Student>();
String sql="select * from student";
try {
pres = conn.prepareStatement(sql);
rs = pres.executeQuery();
while(rs.next()){
Integer id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
int gender = rs.getInt("gender");
String dept = rs.getString("dept");
String tel = rs.getString("tel");
String email = rs.getString("email");
Date birth = rs.getDate("birth");
String school = rs.getString("school");
double wallet = rs.getDouble("wallet");
int zodiac = rs.getInt("zodiac");
String address = rs.getString("address");
Student student = new Student(id, name, age, gender, dept, tel, email, birth, school, wallet, zodiac, address);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
free(conn, pres, rs);
}
long endTime = System.currentTimeMillis();
System.out.println("直接读取运行时间:" + (endTime - startTime));
return list;
}
计算记录所用内存空间
public static void getMemory(String tableName) {
double memory = 0;
int rows = 0;
String sql = "SELECT TABLE_NAME,DATA_LENGTH+INDEX_LENGTH memory,TABLE_ROWS FROM TABLES WHERE TABLE_SCHEMA='jdbc' AND TABLE_NAME=?";
try {
pres = conn.prepareStatement(sql);
pres.setString(1, tableName);
rs = pres.executeQuery();
if (rs.next()) {
memory = rs.getDouble("memory");
rows = rs.getInt("TABLE_ROWS");
}
System.out.println("表中记录数: " + rows + " 占用内存空间:" + memory / 1024 + "KB");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
记录过程:
记录数为1时:
原生存储重复五次,平均运行时间为10.8ms,原生读取重复五次,平均运行时间4.6ms,占用内存16kb;序列化存储重复五次,平均运行时间为27.2ms,序列化读取重复五次,平均运行时间24.0ms,占用内存16kb。
Test类
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
DBHelper.cleanTable();
Student student = new Student(1, "zhangsan", 18, 1, "计算机系", "10086", "123@163.com", "basketball", "温州大学", 3.8, 5, "温州大学计算机与人工智能学院");
list.add(student);
System.out.println("记录数为1条时:");
DBHelper.saveStudent(list);
}
}
数据
Test类
public class Test {
public static void main(String[] args) {
System.out.println("记录数为1条时:");
DBHelper.readStudent();
}
}
数据
Test类
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
DBHelper.cleanTable();
Student student = new Student(1, "zhangsan", 18, 1, "计算机系", "10086", "123@163.com", "basketball", "温州大学", 3.8, 5, "温州大学计算机与人工智能学院");
list.add(student);
System.out.println("记录数为1条时:");
DBHelper.save(list);
}
}
记录
Test类
public class Test {
public static void main(String[] args) {
System.out.println("记录数为1条时:");
DBHelper.read();
}
}
记录
记录数为200时:
原生存储重复五次,平均运行时间为666.4ms,原生读取重复五次,平均运行时间48.2ms,占用内存64kb;序列化存储重复五次,平均运行时间为734.2ms,序列化读取重复五次,平均运行时间95.0ms,占用内存64kb。
Test类
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
DBHelper.cleanTable();
for (int i = 1; i <= 200; i++) {
list.add(new Student(i, "zhangsan", 18, 1, "计算机系", "10086", "123@163.com", "basketball", "温州大学", 3.8, 5, "温州大学计算机与人工智能学院"));
}
System.out.println("记录数为200条时:");
DBHelper.saveStudent(list);
}
}
记录
Test类
public class Test {
public static void main(String[] args) {
System.out.println("记录数为200条时:");
Memory.getMemory("student");
DBHelper.readStudent();
}
}
Test类
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
DBHelper.cleanTable();
for (int i = 1; i <= 200; i++) {
list.add(new Student(i, "zhangsan", 18, 1, "计算机系", "10086", "123@163.com", "basketball", "温州大学", 3.8, 5, "温州大学计算机与人工智能学院"));
}
System.out.println("记录数为200条时:");
DBHelper.save(list);
}
}
记录
Test类
public class Test {
public static void main(String[] args) {
System.out.println("记录数为200条时:");
Memory.getMemory("student");
DBHelper.read();
}
}
记录
记录数为1000时:
原生存储重复五次,平均运行时间为3038.4ms,原生读取重复五次,平均运行时间120.2ms,占用内存192kb;序列化存储重复五次,平均运行时间为3211.8ms,序列化读取重复五次,平均运行时间195.6ms,占用内存496kb。
Test类同上
记录
记录
Test类同上
记录
记录
记录数为5000时:
原生存储重复五次,平均运行时间为14080.8ms,原生读取重复五次,平均运行时间226.0ms,占用内存1552kb;序列化存储重复五次,平均运行时间为14394.2ms,序列化读取重复五次,平均运行时间440.6ms,占用内存2576kb。
Test类同上
记录
记录
Test类同上
记录
记录
记录数为5000时:
原生存储重复五次,平均运行时间为27909.4ms,原生读取重复五次,平均运行时间320.4ms,占用内存2576kb;序列化存储重复五次,平均运行时间为28020.4ms,序列化读取重复五次,平均运行时间759.4ms,占用内存5648kb。
Test类同上
记录
记录
Test类同上
记录
记录
参考文章
(45条消息) MySQL8 版本后 系统表 information_schema.tables 行数与实际数据表行数不准确 处理_AllenLeungX的博客-CSDN博客
public void savePerson(List<Person> persons){
String sql="insert into objtest(obj) values(?)";
try {
pres=conn.prepareStatement(sql);
for(int i=0;i<persons.size();i++){
pres.setObject(1, persons.get(i));
pres.addBatch(); //实现批量插入
}
pres.executeBatch(); //批量插入到数据库中
if(pres!=null)
pres.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}