Environment:Tomcat 8.0.32
Windows 10
Eclipse Mars 2 Release 4.5.2
MySQL-connector-java-5.1.39-bin.jar
I'm try to set up a jndi for a MySQL database connection in eclipse using
Tomcat 8.0.32 . I keep receiving the following message when I try to run code
example.
javax.naming.NameNotFoundException: Name [308tubeOracle] is not bound in this
Context. Unable to find [308tubeOracle].
I believe I have set everything up correctly and would really appreciate some
direction.
I have added the following two entries into the context.xml under the Tomcat
Server config in Eclipse.
<Resource name="jdbc/308tubeOracle" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdatabase" username="mysqladmin"
password="mypassword" />
<ResourceLink name="jdbc/308tubeOracle"
global="jdbc/308tubeOracle"
type="javax.sql.DataSource"/>
I have added the following to the application web.xml in the WEB-INF folder of
my application.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>com.youtube.rest</display-name>
<welcome-file-list>
<welcome-file>readme.html</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description>
<!-- <res-ref-name>jdbc/tubeOracle</res-ref-name> -->
<res-ref-name>jdbc/308tubeOracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I have copied the MySQL-connector-java-5.1.39-bin.jar to the WEB-INF \lib in
my application .
Here is the code I call the context with to try and get the DataSource. I added
the while loop to try to
figure out what was going on. It does print the name 308tubeOracle at that
point. But I still get theName [308tubeOracle] is not bound in this Context.
Unable to find [308tubeOracle].
import javax.naming.*;import javax.sql.*; public class get {private static
DataSource Oracle308tube = null;
private static Context context = null;
public static DataSource Oracle308tubeConn() throws Exception {if(Oracle308tube
!= null){
return Oracle308tube;
}try { if (context == null){
context = new InitialContext();
} NamingEnumeration<NameClassPair> list =
context.list("java:comp/env/jdbc"); while
(list.hasMore()) {
System.out.println(list.next().getName());}Oracle308tube = (DataSource)
context.lookup("308tubeOracle");}catch (Exception e){
e.printStackTrace(); }return Oracle308tube;}}