Je travaille sur serveur jboss et en exécutant le code dans une class main le tout en local :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
 
 
public class TestJndi {
 
	/**
         * @param args
         */
	public static void main(String[] args) {
		Context context = null;
		Connection cn = null;
		Properties pop = new Properties();
		pop.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		pop.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
		pop.put("java.naming.provider.url", "jnp://localhost:1099");
 
 
		try {
			context = new InitialContext(pop);
			DataSource	dataSource= (DataSource)context.lookup("java:MyDS");
			cn = dataSource.getConnection();
			Statement st= cn.createStatement();
			String sql = "select * from mytable ";
		    ResultSet rs = st.executeQuery(sql);
		    while (rs.next()) 
		        {
		            System.out.printf("%-20s | %-20s | %3d\n", // 
		            		rs.getString(1), rs.getString(2));
		        }
		cn.close();
 
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
 
		// TODO Auto-generated method stub
 
	}
 
}

j'obtiens l'erreur suivante:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
 
javax.naming.NameNotFoundException: MyDs 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:396)
	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:303)
	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:726)
	at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at TestJndi.main(TestJndi.java:29)