?如何将一个序列化的对象存入数据库? 你可以先将这个类文件存进去,用的时候取出来存成文件,然后再Class.ForName()不久行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么把类文件存进去?比如Class1 class1sql = "insert into table1 (class1_intable) values('" + class1 + "')";state.execute(sql);取的时候取class1_intable,再Class.ForName就成了? File file=new File("yourclass.class"); java.io.FileInputStream fileint=new FileInputStream(file); PreparedStatement st1=conn.prepareStatement("update classtable set class=?");st1.setBinaryStream(1,fileint,2000);st1.executeUpdate();fileint.close();//别的还用我多说吗? 再问一句,数据库那个字段的类型是什么?是char还是blob?如果不让用文件流,还有什么方法。谢谢。 问题解决了,我把程序放上来,再留一天讨论时间,明日17:00前揭帖。import java.util.*;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(); } }} 请问如何显示图片? JDK 在windowXP 要不要配置环境变量???? JCreator中有没有格式重排的功能?快捷键是什么? 高分求教,客户要求UTF-8编码的文件,我该怎么做呢? java 小问题 一个数组分割问题 求救求救求救 jbuilder中,菜单中字母下划线该怎么弄呢? 请大家推荐几个好的中文JAVA站点吧。 刚学JAVA本想按《程序员大本营java专刊》练几招,结果~~~ 菜鸟学飞,低高度问题,谢谢 谁有JBUILDER的序列号?给我一个
比如Class1 class1sql = "insert into table1 (class1_intable) values('" + class1 + "')";
state.execute(sql);取的时候取class1_intable,再Class.ForName就成了?
File file=new File("yourclass.class");
java.io.FileInputStream fileint=new FileInputStream(file);
PreparedStatement st1=conn.prepareStatement("update classtable set class=?");
st1.setBinaryStream(1,fileint,2000);
st1.executeUpdate();
fileint.close();
//
别的还用我多说吗?
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();
} }
}