存类路径到数据库,比如net.csdn.sample.TestClass
以后再用Class.forName("net.csdn.smaple.TestClass");
以后再用Class.forName("net.csdn.smaple.TestClass");
解决方案 »
- 求教java中的drawString()方法
- 关于为组件添加事件,高手们看看这是为什么?
- 请问Java容器里可以套容器吗?
- Java中得自己写实现delete功能的代码吗???
- 在局域网中如果捕获已经发送出去的邮件
- 请高手解释一下
- 求张孝祥JAVA视频教学 高级篇 下载地址 给个下载地址就结帖
- [偏要较真];请问servlet的正确读音?
- 分享《Applying UML and Patterns -- An introduction to OOA/D and the UP》英文版....
- 怎样实现一个弹出式的日历框!!! 急
- 提供资料的给分
- 关于 throws java.io.IOException
然后读取这个文件的内容,读取文件的内容,写到数据库中。
反之读数据库,写文件,ObjectInputStream方法简单而直接,但是耗费比较多。write的对象应该是可序列化的对象,也就是实现了Serializable接口的。
那要写进数据库中的类是不是要求实现Serializable接口啊?
我现在不能实现Serializable啊~~
只要把你的数据转化为byte就可以了。
类的定义和数据库表的字段定义要自己设计了。
要么就是使用Serializable机制直接写入数据库。但这样会非常难看。
import java.io.*;
没有Serializable接口阿!!!!:(
你可以保存的是一个类的实例细节,或者你可以以某种格式保存类的定义,但是怎么可以保存类本身--类是一个抽象的概念.
不知道楼主想做什么,其实既使你不实现Serializable,也可以自己写方法保存对象的细节或者类的定义什么的.而且j2me没有Serializable,感觉费解.
我最近在作j2me,主要是游戏方面的东西,但是用手机从网络上下载挺不方便的,所以我就想能不能写一个比较公共的程序专门用来下载其它的类,把这些类保存在rms中(一个类似数据库的东东),然后再运行,可是j2me中是没有erializable接口的~~~
所以才有上面的问题
不知道我说清楚了没~~:)
下面是sun的Serializable源代码。
/*
* @(#)Serializable.java 1.20 03/01/23
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/package java.io;/**
* Serializability of a class is enabled by the class implementing the
* java.io.Serializable interface. Classes that do not implement this
* interface will not have any of their state serialized or
* deserialized. All subtypes of a serializable class are themselves
* serializable. The serialization interface has no methods or fields
* and serves only to identify the semantics of being serializable. <p>
*
* To allow subtypes of non-serializable classes to be serialized, the
* subtype may assume responsibility for saving and restoring the
* state of the supertype's public, protected, and (if accessible)
* package fields. The subtype may assume this responsibility only if
* the class it extends has an accessible no-arg constructor to
* initialize the class's state. It is an error to declare a class
* Serializable if this is not the case. The error will be detected at runtime. <p>
*
* During deserialization, the fields of non-serializable classes will
* be initialized using the public or protected no-arg constructor of
* the class. A no-arg constructor must be accessible to the subclass
* that is serializable. The fields of serializable subclasses will
* be restored from the stream. <p>
*
* When traversing a graph, an object may be encountered that does not
* support the Serializable interface. In this case the
* NotSerializableException will be thrown and will identify the class
* of the non-serializable object. <p>
*
* Classes that require special handling during the serialization and
* deserialization process must implement special methods with these exact
* signatures: <p>
*
* <PRE>
* private void writeObject(java.io.ObjectOutputStream out)
* throws IOException
* private void readObject(java.io.ObjectInputStream in)
* throws IOException, ClassNotFoundException;
* </PRE><p>
*
* The writeObject method is responsible for writing the state of the
* object for its particular class so that the corresponding
* readObject method can restore it. The default mechanism for saving
* the Object's fields can be invoked by calling
* out.defaultWriteObject. The method does not need to concern
* itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput. <p>
*
* The readObject method is responsible for reading from the stream and
* restoring the classes fields. It may call in.defaultReadObject to invoke
* the default mechanism for restoring the object's non-static and non-transient
* fields. The defaultReadObject method uses information in the stream to
* assign the fields of the object saved in the stream with the correspondingly
* named fields in the current object. This handles the case when the class
* has evolved to add new fields. The method does not need to concern
* itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput. <p>
*
* Serializable classes that need to designate an alternative object to be
* used when writing an object to the stream should implement this
* special method with the exact signature: <p>
*
* <PRE>
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
* </PRE><p>
*
* This writeReplace method is invoked by serialization if the method
* exists and it would be accessible from a method defined within the
* class of the object being serialized. Thus, the method can have private,
* protected and package-private access. Subclass access to this method
* follows java accessibility rules. <p>
*
* Classes that need to designate a replacement when an instance of it
* is read from the stream should implement this special method with the
* exact signature.<p>
*
* <PRE>
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
* </PRE><p>
*
* This readResolve method follows the same invocation rules and
* accessibility rules as writeReplace.
*
* @author unascribed
* @version 1.20, 01/23/03
* @see java.io.ObjectOutputStream
* @see java.io.ObjectInputStream
* @see java.io.ObjectOutput
* @see java.io.ObjectInput
* @see java.io.Externalizable
* @since JDK1.1
*/
public interface Serializable {
}
只要你implement就ok,虚拟机知道怎么做。
通常不需要任何实现代码,只是告诉虚拟机,这个类是Serializable的。
特殊情况需要写点什么,但是我记不得了,你试试吧。
它仅仅是一个声名,就象throws RemoteException就意味着一个远程调用一样。jvm会根据这个声名假设你的对象可以序列化,如果你的对象不能(或者说先天性的不能,比如Connection),那么你必须自己重载几个方法,主要writeojbect(),resolveOjbect();
关于Serializable接口我几本明白你们的意思了。但是那不就是说我要实现我自己所有类的串行化?那是个不小的工作量啊。
要知道j2me中所有的类都没有实现串行化。
其实我现在想要实现的基本上就是你所说的APPLET上的功能。
因为我不可能把一个泪下载到手机上(我不能控制手机的文件系统)
唯一的下载并且保存类的方法就是把他存到一个类似数据库的环境当中
所以这就要求我能够有一种保存类并且以后在执行它的机制~~~
这就是我现在困惑的to SAsura(男朋友的号):
我要保存的是一个类,不是某种实例的状态。
或者说得更明白点就是我想从网络上下载一段可执行的代码,并且把它存到一个类似数据库的环境中供以后的调用。
现在怎么越来越感觉到我的要求朝复杂啊?!好像实现不了的感觉啊~~~:(
要不然会出异常(类找不到),我不清楚J2ME怎么操作的,简单的方法就是分多次下载,
第一次一定是下一个很小的能跑的类,以后下主框架类并开始运行,给用户以动态的
感觉,说白了是给用户一个错觉以为程序正常跑了(这个方法到处用的其实)
对啊!关键就是怎么保存啊?我不是就在问这个呢么~~~:(
我还是不明白存到数据库中以后怎么读出来~上面提到的什么存路径什么的方法都是不可行的阿
我现在用的是j2me~并且我不可能操作文件系统的。
我查查资料,看看你说得有没有可能实现