Mysql 存储 javabean

yulu0214 2009-11-26 06:29:41
业务需要,将javabean 序列化后存储到mysql中,字段设置为blob
序列话:

PipedInputStream is = new PipedInputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(new PipedOutputStream(is));
oos.writeObject(object);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}

插入数据库:

pstmt.setBinaryStream(10, is, is.available());

这一步是成功的
取数据:

InputStream is = rs.getBinaryStream("bean");
反序列化:
Object object = null;
try {
if(is.available() >0){
ObjectInputStream ois = new ObjectInputStream(is);
object = ois.readObject();
ois.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}

object一直为空,查看了下is里面是有值的
网上这方面的资料太少了,忘高人指点,分不够可以再加。
谢谢哈
...全文
142 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wifewifewife 2009-11-27
  • 打赏
  • 举报
回复
不知道.友情棒顶.
Busing 2009-11-26
  • 打赏
  • 举报
回复
呵呵 以前记得写过的 忘记了 就又找些资料写出来了

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package objectintodb;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author root
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
// TODO code application logic here
Connection conn=getConn();
//java.sql.PreparedStatement pstmt=conn.prepareStatement("insert into user values (?)");

//saveObject(new User("ty", "男", 20), pstmt, 1);
//pstmt.execute();


ResultSet rs=conn.createStatement().executeQuery("select user from user");
User user=null;
while(rs.next())
{
user= (User)(read(rs, "user"));
}
rs.close();

System.out.println(user.getUname());


}


static void saveObject(User user,PreparedStatement pstmt,int paramterIndex)
{
PipedInputStream is=new PipedInputStream();
try {
ObjectOutputStream oout = new ObjectOutputStream(new PipedOutputStream(is));
oout.writeObject(user);
oout.close();
pstmt.setBinaryStream(1, is, is.available());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException sx)
{
sx.printStackTrace();
}



}




static Object read(ResultSet rs,String column) throws ClassNotFoundException, IOException, SQLException
{
byte[] buf=rs.getBytes(column);
if(buf!=null)
{
ObjectInputStream objIn=new ObjectInputStream(new ByteArrayInputStream(buf));
return objIn.readObject();
}
return null;
}


static Connection getConn()
{
Connection conn=null;
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/user";
String username = "ty";
String pwd = "ty";
Class.forName(driver);
conn = DriverManager.getConnection(url, username, pwd);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


return conn;

}

}



跟你的没多大的不同,主要就是你的 InputStream is = rs.getBinaryStream("bean");

和我的
byte[] buf=rs.getBytes(column);



我的测试已经成功了


ty
BUILD SUCCESSFUL (total time: 0 seconds)

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧