数据库名:db_file_test
表名:obj
实体类:
package com.bean;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
数据库连接的工具类:
package com.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
static String url = "jdbc:mysql://localhost:3306/db_file_test?useUnicode=true&characterEncoding=UTF-8";
static String db_user = "root";
static String db_password = "root";
static Connection conn = null;
static {
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, db_user, db_password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
}
存取对象信息的核心工具类:
package com.utils;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.bean.User;
public class DBUtils {
public int addObject(User user) {
String name = user.getName();
String sql = "insert into obj(name,value)values(?,?)";
Connection conn = DBConnection.getConnection();
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setObject(2, user);
int result = ps.executeUpdate();
return result;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public User getObj(String name) {
Connection conn = DBConnection.getConnection();
String sql = "select * from obj where name=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
ObjectInputStream ois = new ObjectInputStream(
rs.getBinaryStream("value"));
User user = (User) ois.readObject();
System.out.println("getObj-->" + user.toString());
return user;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
测试类:
将对象添加进数据库:
package com.test;
import com.bean.User;
import com.utils.DBUtils;
public class Test1 {
public static void main(String[] args) {
DBUtils utils = new DBUtils();
User user = new User("小明", "123456");
if (utils.addObject(user) != 0) {
System.out.println("添加成功!");
}
}
}
从数据库中读取出对象信息:
package com.test;
import com.bean.User;
import com.utils.DBUtils;
public class Test1 {
public static void main(String[] args) {
DBUtils utils = new DBUtils();
User user = utils.getObj("小明");
System.out.println(user.toString());
}
}