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里面是有值的
网上这方面的资料太少了,忘高人指点,分不够可以再加。
谢谢哈
...全文
115 2 打赏 收藏 转发到动态 举报
写回复
用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)

基于jsp+javabean+mysql三层结构的动态购物网站。网站用户接口(即界面)由jsp完成,数据和逻辑处理由beans完成,数据储存由mysql完成。因为beans独立负责处理整个网站的全部数据逻辑运算,所以整个网站的负载量和速度都将大大提高,所以基于这种语言和结构开发的购物系统的优势是其它语言没法比尔的。更重要的是,jsp+bean能够在Apache环境下顺畅地运行,这也是其最大优点之一。这就更进一步保证了网站的稳定性和安全性,而这些,对于一个购物网站来说是非常重要的!这个版本让大家期待已久了,主要做了以下一些修正:1.新增产品站内搜索引擎。2.新增人气商品排行。3.新增热评商品排行。4.新增重点推荐产品。5.删除不太需要的新闻频道。6.修正后台管理的一些Nullpointexception错误。7.新增顾客资料修改。8.新增顾客订单查询。9.新增后台超级用户和订单管理员管理,方便物流人员独立处理订单。10.完善了一些购物流程。本程序由“城市WebClub”独立自主开发,你可以对整个商城的版面甚至版权作任意修改,但传播本程序时,请保留本站的说明文件。因为开发一个系统真的很痛苦!由于时间和水平关系,本系统还有很多不足和错误的地方,欢迎大家试用并提出意见。安装====1。把myshop直接上传到你的jsp主机。2。把class里的文件上传到你的jsp主机的class目录。3。分别修改myshop下的config.jsp、class/myshop下的config.java文件。4。把所有mysqldata文件倒入你的数据库。

50,541

社区成员

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

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