序列化失败(100分)

  • 主题发起人 主题发起人 wyfhn
  • 开始时间 开始时间
W

wyfhn

Unregistered / Unconfirmed
GUEST, unregistred user!
用application序列化成功,但换成applet失败,不知何故。
------------Data.java-------------
import java.io.*;
import java.util.zip.*;
import java.util.Vector;
import java.sql.*;
import java.lang.*;
class Data implements Serializable
{
String tt;
Vector vect=new Vector();
Vector v=new Vector();
public Data(String tt)
{
this.tt=tt;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection cn = DriverManager.getConnection("jdbc:oracle:thin:@213.168.0.1:1521:ora1","scott","tiger");
Statement stmt=cn.createStatement();
ResultSet rs=stmt.executeQuery(tt);
v.removeAllElements();
String pk;
while(rs.next())
{
pk = rs.getString(2);
v.addElement(pk);
}
vect.addElement(v);
}
catch (Exception e)
{
System.out.println(e.getMessage () + "/n");
}
}
}
----------------compressObject.java-------------
import java.util.zip.*;
import java.io.*;
public class compressObject
{
public static byte[] writeCompressObject(Data object_)
{
byte[] data_=null;
try
{
ByteArrayOutputStream o = new ByteArrayOutputStream();
GZIPOutputStream gzout=new GZIPOutputStream(o);
ObjectOutputStream out = new ObjectOutputStream(gzout);
out.writeObject(object_);
out.flush();
out.close();
gzout.close();
data_=o.toByteArray();
o.close();
}catch(IOException e)
{
System.out.println(e);
}
return(data_);
}
public static Data readCompressObject(byte[] data_)
{
Data object_=null;
try
{
ByteArrayInputStream i = new ByteArrayInputStream(data_);
GZIPInputStream gzin=new GZIPInputStream(i);
ObjectInputStream in = new ObjectInputStream(gzin);
object_=(Data)in.readObject();
i.close();
gzin.close();
in.close();
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
return(object_);
}
}
----------------------------test.java------------------
import java.io.*;
import java.util.zip.*;
public class test
{
public static void main(String[] args)
{
Data testData_=new Data("select * from dept where rownum<10");
byte[] i_=compressObject.writeCompressObject(testData_);
Data o_=compressObject.readCompressObject(i_);
System.out.println("name="+o_.vect);
}
}
-------------------testzip.java-------------------
import java.util.zip.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class testzip extends Applet
{
public TextArea chat_txt;
public void init()
{
chat_txt= new TextArea();
Panel pp= new Panel();
pp.setLayout(new FlowLayout());
add("North",pp);
add("Center",chat_txt);
Data testData_=new Data("select * from dept where rownum<10");
byte[] i_=compressObject.writeCompressObject(testData_);
Data o_=compressObject.readCompressObject(i_);
System.out.println("name="+o_.vect);
}
}
------------------------testzip.html-------------
<html>
<body>
<hr>
<applet
codebase="."
code=testzip.class
width = 400
height = 400
>
</applet>
<hr>
</body>
</html>
----------------------end------------------------
 
是不是jdk的问题,你的applet是在浏览器里面看结果还是在appletviewer里面?
-----
http://www.8421.org
 
applet无法直接操作数据库(这受applet本身的特点和安全性限制)
以至于你的 Data在applet中构造失败。
 
不会吧,那要操作数据库只有写应用?
 
后退
顶部