DefaultDS Not Found
qinding Jul 10, 2003 9:22 PMAfter installation of JBoss, I made no change. Start JBoss. Go to jmx-console, Click Service=JNDIView. Click Invoke button under java.lang.Strng list(). I see Java:Namespace. Under it, I see +-DefaultDS. So, DefaultDS is bound to the jndi tree.
Now, I write a small java program to access to the database using DefaultDS.
public static void main(String[] args) {
DataSource ds = null;
try{
Properties prop = new Properties();
prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
prop.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
prop.setProperty("java.naming.provider.url", "localhost");
Context jndiCntx = new InitialContext(prop);
//ds = (DataSource)jndiCntx.lookup("DefaultDS");
ds = (DataSource)jndiCntx.lookup("java:/DefaultDS");
}catch(javax.naming.NamingException ne){
ne.printStackTrace();
}
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try{
conn = ds.getConnection();
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery("Select * from Department");
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
//}
} catch(Exception e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
ds = null;
}
}
}
Execute this program: I got this error:
javax.naming.NameNotFoundException: DefaultDS not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:495)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:503)
at org.jnp.server.NamingServer.getObject(NamingServer.java:509)
at org.jnp.server.NamingServer.lookup(NamingServer.java:282)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:536)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:492)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:471)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.qd.dbConnectionConfig.Qding.main(Qding.java:34)
java.lang.NullPointerException
at com.qd.dbConnectionConfig.Qding.main(Qding.java:45)
Did I do something wrong? Is this the correct way to use jndi datasource? Please advise. Thank you very much.
Qin