如何把java对象存入数据库
2个回答
推荐于2018-03-20
展开全部
假设纤陵有这么个对象:
import java.io.Serializable;
public class MyObject implements Serializable {
private static final long serialVersionUID = 1L;
private int i;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
//测试 的方法如下
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;
public class test {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
MyObject obj = new MyObject();
obj.setI(4567);
write(Object2Bytes(obj));
// read();
}
public static void write(byte[] b) throws ClassNotFoundException {
System.out.println(b.length);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection con = DriverManager.getConnection("jdbc:odbc:temp");
String sql = "insert into tab values(?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setBytes(1, b);
pstmt.execute();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read() throws ClassNotFoundException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection con = DriverManager.getConnection("jdbc:odbc:temp");
String sql = "select * from tab";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet res = pstmt.executeQuery();
while (res != null && res.next()) {
byte[] b = res.getBytes("key");
System.out.println(b.length);
MyObject obj = (MyObject) Bytes2Object(b);
System.out.println(obj.getI());
}
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 将对象转换枝竖敏成字节猛枝数组
public static byte[] Object2Bytes(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
}
// 将字节数组转换成为对象
public static Object Bytes2Object(byte[] b) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return obj;
}
}
import java.io.Serializable;
public class MyObject implements Serializable {
private static final long serialVersionUID = 1L;
private int i;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
//测试 的方法如下
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;
public class test {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
MyObject obj = new MyObject();
obj.setI(4567);
write(Object2Bytes(obj));
// read();
}
public static void write(byte[] b) throws ClassNotFoundException {
System.out.println(b.length);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection con = DriverManager.getConnection("jdbc:odbc:temp");
String sql = "insert into tab values(?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setBytes(1, b);
pstmt.execute();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read() throws ClassNotFoundException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection con = DriverManager.getConnection("jdbc:odbc:temp");
String sql = "select * from tab";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet res = pstmt.executeQuery();
while (res != null && res.next()) {
byte[] b = res.getBytes("key");
System.out.println(b.length);
MyObject obj = (MyObject) Bytes2Object(b);
System.out.println(obj.getI());
}
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 将对象转换枝竖敏成字节猛枝数组
public static byte[] Object2Bytes(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
}
// 将字节数组转换成为对象
public static Object Bytes2Object(byte[] b) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return obj;
}
}
2016-06-18
展开全部
使用李好棚 java.sql.Clob或 Blob 写 入。。。哪则。。。。。。对象先做序列化、转袜陵成byte[]或流。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询