小弟用序列化操作数据库的时候,想到一个问题,是序列化直接以binary流操作起来快,还是传统的jdbc快呢?而且用hibernate的时候,entity都是先了serializable,但是H好像没用序列化来操作啊。
一下是自己写的简单的案例:
PersonDetails:
package com.sean.dao;
import java.io.Serializable;
public class PersonDetails implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String sex;
public PersonDetails(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
PersonPersist:
package com.sean.dao;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PersonPersist {
static String userid = "zndt", password = "123456";
static String url = "jdbc:sqlserver://localhost:1433; DatabaseName=learning";
static int count = 0;
static Connection con = null;
public static void main(String[] args) {
Connection con = getOracleJDBCConnection();
PersonDetails person1 = new PersonDetails("hemanth", 10, "Male");
PersonDetails person2 = new PersonDetails("bob", 12, "Male");
PersonDetails person3 = new PersonDetails("Richa", 10, "Female");
PreparedStatement ps;
try {
ps = con
.prepareStatement("INSERT INTO SerialTest VALUES (?, ?)");
write(person1, ps);
ps.execute();
write(person2, ps);
ps.execute();
write(person3, ps);
ps.execute();
ps.close();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM SerialTest");
while (rs.next()) {
Object obj = read(rs, "person");
PersonDetails p = (PersonDetails) obj;
System.out.println(p.getName() + "\t" + p.getAge() + "\t"
+ p.getSex());
}
rs.close();
st.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void write(Object obj, PreparedStatement ps)
throws SQLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(baos);