关于数据库保存流java流对象
答案:2 悬赏:40 手机版
解决时间 2021-11-11 22:10
- 提问者网友:捧腹剧
- 2021-11-11 04:21
关于数据库保存流java流对象
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-11-11 04:41
什么》? 哎呀没必要。。
不过可实现的:如下为一个通用的例子:
假设有这么个对象:
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;
}
}
全部回答
- 1楼网友:冷風如刀
- 2021-11-11 05:02
你是WEB程序啊。还是应用程序啊?
WEB程序可以用SESSION来保存。
应用程序用不用保存哦。spring 也有类似的功能啊。
WEB程序可以用SESSION来保存。
应用程序用不用保存哦。spring 也有类似的功能啊。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯