首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 其他服务器 >

Myeclipse+Jboss+mysql JNDI调用出错,该怎么解决

2012-03-04 
Myeclipse+Jboss+mysql JNDI调用出错package com.jms.jmsServiceimport java.sql.ResultSetimport java.

Myeclipse+Jboss+mysql JNDI调用出错
package com.jms.jmsService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Properties;

import org.jnp.interfaces.TimedSocketFactory;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnJms {

/**
* 连接store空间(发送方)
* @param args
*/
public static void main(String[] args) {
connJboss();//1
jbossClient();//2
}

//JBOSS JNDI连接方式

public static Context getJbossContext(){

final String INIT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
final String SERVER_URL = "jnp://127.0.0.1:8080";
Context ctx = null;
try{

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INIT_FACTORY);
env.put(Context.PROVIDER_URL,SERVER_URL);
ctx = new InitialContext(env);

}catch(Exception e){

e.printStackTrace();
}
return ctx;

}



public static void jbossClient(){

final String INIT_FACTORY="org.jnp.interfaces.NamingContextFactory";
final String SERVER_URL="jnp://127.0.0.1:8080";
Hashtable map = new Hashtable();
map.put(Context.INITIAL_CONTEXT_FACTORY,INIT_FACTORY);
map.put(Context.PROVIDER_URL, SERVER_URL);

Properties properties = new Properties();  
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");  
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");  
properties.setProperty(Context.PROVIDER_URL,"localhost:1099");  
try {
Context ctx = new InitialContext(properties);
DataSource ds = (DataSource)ctx.lookup("java:/DefaultDS");
java.sql.Connection con = ds.getConnection();
Statement stat = con.createStatement();
ResultSet qu = stat.executeQuery("select * from mysql.user");
while(qu.next()){

System.out.println("------:"+qu.getString("User"));
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


1:调用jbossClient()方法时报错:log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.
javax.naming.NameNotFoundException: DefaultDS not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
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:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)


at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.jms.jmsService.ConnJms.jbossClient(ConnJms.java:159)
at com.jms.jmsService.ConnJms.main(ConnJms.java:65)

我用的是 Myeclipse8.6,jboss6.0Final,mysql5.1
数据库的jar包:mysql-connector-java-5.1.10-bin.jar
不知道是哪里错了???

[解决办法]
试试下面jndi的写法
DataSource ds = (DataSource)ctx.lookup("java:comp/env/DefaultDS");

[解决办法]

探讨

试试下面jndi的写法
DataSource ds = (DataSource)ctx.lookup("java:comp/env/DefaultDS");

热点排行