存类路径到数据库,比如net.csdn.sample.TestClass
以后再用Class.forName("net.csdn.smaple.TestClass");

解决方案 »

  1.   

    使用的ObjectOutputStream的writeobject函数,将对象write到一个文件中(或者流中)
    然后读取这个文件的内容,读取文件的内容,写到数据库中。
    反之读数据库,写文件,ObjectInputStream方法简单而直接,但是耗费比较多。write的对象应该是可序列化的对象,也就是实现了Serializable接口的。
      

  2.   

    to liujiboy(liujiboy) :
    那要写进数据库中的类是不是要求实现Serializable接口啊?
    我现在不能实现Serializable啊~~
      

  3.   

    可以将类的数据写入数据库。(楼上的ObjectInputStream实际也是将类的数据流化),但要将类的定义写入数据库不行的吧。
    只要把你的数据转化为byte就可以了。
    类的定义和数据库表的字段定义要自己设计了。
      

  4.   

    同意楼上的。你可以将实例的数据抽象出来存入数据库,到时候用这些信息重新构造实例。
    要么就是使用Serializable机制直接写入数据库。但这样会非常难看。
      

  5.   

    “我现在不能实现Serializable啊~~”什么意思?
    import java.io.*;
      

  6.   

    因为我用的是j2me~~~!
    没有Serializable接口阿!!!!:(
      

  7.   

    Serializable接口是j2se中的,j2me难道没有吗?j2me不是以j2se为基础吗?
      

  8.   

    楼主的提法本身就令人费解:
    你可以保存的是一个类的实例细节,或者你可以以某种格式保存类的定义,但是怎么可以保存类本身--类是一个抽象的概念.
    不知道楼主想做什么,其实既使你不实现Serializable,也可以自己写方法保存对象的细节或者类的定义什么的.而且j2me没有Serializable,感觉费解.
      

  9.   

    to swinging(山不在高) & 楼主的提法本身就令人费解:其实问题是这样的
    我最近在作j2me,主要是游戏方面的东西,但是用手机从网络上下载挺不方便的,所以我就想能不能写一个比较公共的程序专门用来下载其它的类,把这些类保存在rms中(一个类似数据库的东东),然后再运行,可是j2me中是没有erializable接口的~~~
    所以才有上面的问题
    不知道我说清楚了没~~:)
      

  10.   

    将Serializable接口用j2me实现一次试试,不知道认不认?反正编译通过应该没问题。
    下面是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 {
    }
      

  11.   

    Serializable接口本来就是一个空的。
    只要你implement就ok,虚拟机知道怎么做。
    通常不需要任何实现代码,只是告诉虚拟机,这个类是Serializable的。
    特殊情况需要写点什么,但是我记不得了,你试试吧。
      

  12.   

    Serializable接口是什么都不作的,只是打个标记,JVM会处理。另外你的问题我清楚了,好像和Serializable接口还无关,根本是要传类文件而不是对象吧(如果本地没有类,传递对象过来肯定出错),这个问题我个人觉得在语言本身是没有好的解决方式的,根本是你必须下载所有需要用于运行的文件。以前我做APPLET也有同样的问题,一个简单的解决方法是,开始只载入很小一段代码开始跑,这里跑什么可以商量,比如启动一段动画,或者出来一个简单的功能界面等等,总之是让用户觉得程序开始正常跑了,在这个展示过程中另外一个地方下载真正需要的类,一些不是很重要的也先不下载,而是将主干下完后就开始跑,然后其它地方接着载(这样做好像挺复杂,而且不太好控制)。从远端下载类一般JAVA程序本身是不是好控制我不清楚(根据以往的经验是不行),如果是其它程序需要使用的数据部分的话,一般是在后台开一个线程下载,前台需要什么数据就去获取,等后台载入完成就从下载到本地的数据中取数据。
      

  13.   

    Serializable接口是什么都不作的,===========================
    它仅仅是一个声名,就象throws RemoteException就意味着一个远程调用一样。jvm会根据这个声名假设你的对象可以序列化,如果你的对象不能(或者说先天性的不能,比如Connection),那么你必须自己重载几个方法,主要writeojbect(),resolveOjbect();
      

  14.   

    你是要保存一个类,还是一个实例的某种状态?所以我就想能不能写一个比较公共的程序专门用来下载其它的类这句话比较费解,其他的类指的是什么?别人写好并且打好包的jar文件?还是某个java应用运行到某个时间的所有实例的状态?
      

  15.   

    to liujiboy(liujiboy) &  swinging(山不在高):
    关于Serializable接口我几本明白你们的意思了。但是那不就是说我要实现我自己所有类的串行化?那是个不小的工作量啊。
    要知道j2me中所有的类都没有实现串行化。
      

  16.   

    to swinging(山不在高) :
    其实我现在想要实现的基本上就是你所说的APPLET上的功能。
    因为我不可能把一个泪下载到手机上(我不能控制手机的文件系统)
    唯一的下载并且保存类的方法就是把他存到一个类似数据库的环境当中
    所以这就要求我能够有一种保存类并且以后在执行它的机制~~~
    这就是我现在困惑的to  SAsura(男朋友的号):
    我要保存的是一个类,不是某种实例的状态。
    或者说得更明白点就是我想从网络上下载一段可执行的代码,并且把它存到一个类似数据库的环境中供以后的调用。
    现在怎么越来越感觉到我的要求朝复杂啊?!好像实现不了的感觉啊~~~:(
      

  17.   

    你如果只是想保存一个类,把.jar包或者.class保存起来不就可以了么,需要的时候从数据库读出来,创建实例,运行不就可以了
      

  18.   

    我说了,这个和语言本身无关,因为类必须首先存在你的程序才能正常跑,
    要不然会出异常(类找不到),我不清楚J2ME怎么操作的,简单的方法就是分多次下载,
    第一次一定是下一个很小的能跑的类,以后下主框架类并开始运行,给用户以动态的
    感觉,说白了是给用户一个错觉以为程序正常跑了(这个方法到处用的其实)
      

  19.   

    to SAsura(男朋友的号) :
    对啊!关键就是怎么保存啊?我不是就在问这个呢么~~~:(
      

  20.   

    晕,存成blob类型的,可以放很大的东西,具体存法根据数据库区分
      

  21.   

    哦,如果的确是SAsura(男朋友的号)所描述的那样,那就简单多了这个和将image存入数据库再读出显示是一个道理,可以直接存byte,也可存路径
      

  22.   

    第一个人pleonheart(只睡六小时)存路径的方法很好呀。
      

  23.   

    如果要存类文件,那么建立一个FileInputStream,读取这个类文件的内容,用read,可以一个byte一个的读,也可以一次读多个,然后写到数据库中。数据库用blob字段,说白了就是写byte数组。读取的时候相反。你说无法访问文件系统?什么意思?
      

  24.   

    拜托~~
    我还是不明白存到数据库中以后怎么读出来~上面提到的什么存路径什么的方法都是不可行的阿
    我现在用的是j2me~并且我不可能操作文件系统的。
      

  25.   

    那初始化的时候你怎么装入jar文件,看看ClassLoader,自己做一个ClassLoader,从数据库载入jar文件,不用访问文件系统的,应该不是很难的
      

  26.   

    to SAsura(男朋友的号) :初始化的时候是系统自动装载的jar文件,这个原理和applet差不多
    我查查资料,看看你说得有没有可能实现