不知道直接写应该怎么做。不过可以用objectoutputstream把对象实例写到文件。然后就可以写到数据库了--使用blob数据库简单操作File file=new File("f:\\object.dat");
out.println(file.length());
java.io.FileInputStream filein=new FileInputStream(file);
PreparedStatement st1=conn.prepareStatement("update table set ob=?");
st1.setBinaryStream(1,fileint,2000);
st1.executeUpdate();
filein.close();
out.println(file.length());
java.io.FileInputStream filein=new FileInputStream(file);
PreparedStatement st1=conn.prepareStatement("update table set ob=?");
st1.setBinaryStream(1,fileint,2000);
st1.executeUpdate();
filein.close();
解决方案 »
- java新人求解
- 如何将jsp中设置在xml固定调用两个页面
- 如下图所示的N*N数组,用Java编写代码,要以N为入口???
- 问一个转码的问题?
- 大量oreilly电子书免费下载,msn机器人提供 [email protected]
- 寻找java小程序
- 如何用java实现独占打开文件?
- 求swt 遍历 Tree的例子
- 看看这段程序
- 怎样用java实现操作系统中类似信号量的类?
- Applet上开三个线程,编译通过,但是运行时出错,提示Exception occurred during event dispatching:java.lang.NullPointerExceprion。
- 有关JComboBox的ActionListener的问题
Object Stream包装一个ByteArray Stream就可以了。
import java.io.*;
import java.sql.*;public class Object2Blob { public static void main(String[] args) {
try{
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url ="jdbc:informix-sqli://10.0.0.1:1234/itas:INFORMIXSERVER=link;user=link;password=test";
Connection conn= DriverManager.getConnection(url); Map map = new HashMap();
map.put("1","a"); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(map); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); //PreparedStatement pstate = conn.prepareStatement("insert into table2 (item1) values(?)");
PreparedStatement pstate = conn.prepareStatement("update table2 set item1=?"); pstate.setBinaryStream(1,bis,bis.available());
pstate.execute(); Statement state = conn.createStatement();
ResultSet rs = state.executeQuery("select item1 from table2");
rs.next();
Map map2 = new HashMap(); Blob blob = rs.getBlob("item1"); ByteArrayInputStream bais = new ByteArrayInputStream(blob.getBytes(1,(int)blob.length()));
ObjectInputStream obis = new ObjectInputStream(bais);
map2 = (HashMap)obis.readObject(); System.out.println("^^^" + map2.containsKey("1"));
System.out.println("***" + map2.get("1")); conn.close(); }catch (Exception e){
e.printStackTrace();
} }
}------------------
table2->item1 为blob