程序如下:
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
public class JDBCPoolDemo{
public static void main(String ar[]){
String tablename="student";
String sqlstr;
Connection  con=null;
Statement stmt=null;
ResultSet rs=null;
Context ctx=null;
Hashtable ht=new Hashtable();
try{
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
System.out.println("sdddddd");
ctx=new InitialContext(ht); DataSource ds=(DataSource)ctx.lookup("MssqlDataSource");
System.out.println(ds.getClass().toString());
con=ds.getConnection();
System.out.println(con.getClass().toString());
stmt=con.createStatement();
sqlstr="select * from "+tablename; rs=stmt.executeQuery(sqlstr);
while(rs.next()){
System.out.println(rs.getString("id"));
System.out.println("\t");
System.out.println(rs.getString("name"));
System.out.println("\t");
System.out.println(rs.getString("age"));
System.out.println("\t");
System.out.println("\n");
}
}catch(NamingException e){
System.out.println(e.toString());
System.out.println("驱动程序没有找到");
}catch(SQLException el){
System.out.println(el.toString());
System.out.println("数据库异常");
}
finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}
catch(Exception e3){
System.out.println(e3.toString());
}
}
}
}
                DataSource ds=(DataSource)ctx.lookup("MssqlDataSource");
System.out.println(ds.getClass().toString());//这句可以正确执行
con=ds.getConnection();//这里出错了
System.out.println(con.getClass().toString());这里就报错了
运行结果:
sdddddd
class weblogic.jdbc.common.internal.RmiDataSource_920_WLStub
Exception in thread "main" weblogic.rmi.extensions.RemoteRuntimeException: Unexpected Exception
at weblogic.jdbc.common.internal.RmiDataSource_920_WLStub.getConnection(Unknown Source)
at JDBCPoolDemo.main(JDBCPoolDemo.java:22)
Caused by: weblogic.rjvm.PeerGoneException: ; nested exception is: 
weblogic.utils.NestedException: java.lang.NoClassDefFoundError: com/bea/xml/XmlException
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:195)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:338)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:252)
... 2 more
Caused by: weblogic.utils.NestedException: java.lang.NoClassDefFoundError: com/bea/xml/XmlException
at weblogic.rjvm.RJVMImpl.gotExceptionReceiving(RJVMImpl.java:919)
at weblogic.rjvm.ConnectionManager.gotExceptionReceiving(ConnectionManager.java:1009)
at weblogic.rjvm.MsgAbbrevJVMConnection.gotExceptionReceiving(MsgAbbrevJVMConnection.java:443)
at weblogic.rjvm.t3.MuxableSocketT3.hasException(MuxableSocketT3.java:373)
at weblogic.socket.SocketMuxer.deliverExceptionAndCleanup(SocketMuxer.java:734)
at weblogic.socket.SocketMuxer.deliverHasException(SocketMuxer.java:687)
at weblogic.socket.JavaSocketMuxer.processSockets(JavaSocketMuxer.java:307)
at weblogic.socket.SocketReaderRequest.run(SocketReaderRequest.java:29)
at weblogic.work.ExecuteRequestAdapter.execute(ExecuteRequestAdapter.java:21)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:145)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:117)
Caused by: java.lang.NoClassDefFoundError: com/bea/xml/XmlException
at weblogic.diagnostics.instrumentation.InstrumentationLibrary.loadInstrumentationEngineConfiguration(InstrumentationLibrary.java:119)
at weblogic.diagnostics.instrumentation.InstrumentationLibrary.getInstrumentationLibrary(InstrumentationLibrary.java:96)
at weblogic.diagnostics.instrumentation.InstrumentationManager.getAvailableMonitors(InstrumentationManager.java:248)
at weblogic.diagnostics.instrumentation.InstrumentationManager.<init>(InstrumentationManager.java:105)
at weblogic.diagnostics.instrumentation.InstrumentationManager.getInstrumentationManager(InstrumentationManager.java:121)
at weblogic.diagnostics.instrumentation.InstrumentationSupport.getMonitor(InstrumentationSupport.java:44)
at weblogic.jdbc.rmi.SerialConnection.<clinit>(SerialConnection.java)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getLong(Unknown Source)
at java.io.ObjectStreamClass.getDeclaredSUID(Unknown Source)
at java.io.ObjectStreamClass.access$700(Unknown Source)
at java.io.ObjectStreamClass$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at weblogic.rjvm.ClassTableEntry.readExternal(ClassTableEntry.java:36)
at java.io.ObjectInputStream.readExternalData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at weblogic.rjvm.InboundMsgAbbrev.readObject(InboundMsgAbbrev.java:65)
at weblogic.rjvm.InboundMsgAbbrev.read(InboundMsgAbbrev.java:37)
at weblogic.rjvm.MsgAbbrevJVMConnection.readMsgAbbrevs(MsgAbbrevJVMConnection.java:214)
at weblogic.rjvm.MsgAbbrevInputStream.init(MsgAbbrevInputStream.java:174)
at weblogic.rjvm.MsgAbbrevJVMConnection.dispatch(MsgAbbrevJVMConnection.java:426)
at weblogic.rjvm.t3.MuxableSocketT3.dispatch(MuxableSocketT3.java:368)
at weblogic.socket.AbstractMuxableSocket.dispatch(AbstractMuxableSocket.java:378)
at weblogic.socket.SocketMuxer.readReadySocketOnce(SocketMuxer.java:851)
at weblogic.socket.SocketMuxer.readReadySocket(SocketMuxer.java:787)
at weblogic.socket.JavaSocketMuxer.processSockets(JavaSocketMuxer.java:283)
... 4 more
Caused by: java.lang.ClassNotFoundException: com.bea.xml.XmlException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 43 more

解决方案 »

  1.   

     Unexpected Exception at weblogic.jdbc.common.internal.RmiDataSource_920_WLStub.getConnection(Unknown Source) at DBCPoolDemo.main(JDBCPoolDemo.java:22) 
    Caused by: weblogic.rjvm.PeerGoneException: ; nested exception is: 
    weblogic.utils.NestedException: java.lang.NoClassDefFoundError: com/bea/xml/XmlException 
      

  2.   

    我觉得你在非weblogic环境下远程去取weblogic的数据源应该不会成功,在weblogic的管理下取应该不会有问题。
    远程去的话,connection能被Serializable后在网络传播吗?你试试同样的方法,把程序部署成一个war看看。
      

  3.   

    在weblogic的console里测试一下你配置的jdbc是否成功,然后对照一下你的datasource是否和配置的一致
      

  4.   

    Caused by: weblogic.utils.NestedException: java.lang.NoClassDefFoundError: com/bea/xml/XmlException 缺少一个jar,也许是 weblogic-client.jar
    老紫竹CSDN论坛插件发布-竹签V1.0 
      

  5.   

    在网页上测试连接说是连接成功,但是在控制台里却出现这样的错误:
    <2009-3-8 下午10时20分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>
    <2009-3-8 下午10时22分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>
    <2009-3-8 下午10时24分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>
    <2009-3-8 下午10时26分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>
    <2009-3-8 下午10时28分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>
    <2009-3-8 下午10时30分53秒 CST> <Error> <JDBC> <BEA-001112> <Test "select count(
    *) from select * from orders" set up for pool "MysqlDataSource" failed with exce
    ption: "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error i
    n your SQL syntax; check the manual that corresponds to your MySQL server versio
    n for the right syntax to use near 'select * from orders' at line 1".>好像是每两分钟测试一次,上面写的内容是select * from orders这句话有错,但是没错啊。抓狂了,学习已经止步这里很久了。 
      

  6.   

    oracle 默认的测试sql是select 1 from dual
    mysql的话,你可以用 select version()
      

  7.   

    缺少一个weblogic-client.jar 网页上没问题是因为Web应用本身部署在weblogic上,weblogic-client.jar 会在weblogic启动时加载到classpath
    Java Application测试连接的话一定要把weblogic-client.jar 加到classpath
      

  8.   

    我的weblogic92\server\lib目录下只有一个wlclient.jar,不知道是不是这个,加进去后还是没用啊
      

  9.   

    我是在eclipse里写的程序,把这个jar文件放进构建路径就相当于放进classpath了吧
    不知道是怎么回事,con=ds.getConnection();这句话就出异常了,把con=ds.getConnection()改成改成con=ds.getConnection("weblogic","weblogic")后仍然异常,改成后con=ds.getConnetion("root","1234")说用户root不能被鉴别。
      

  10.   

     缺少一个weblogic-client.jar