I can't use JNDI in tomcat to connect MySQL - java

I did everything in this :
how to connect tomcat 7 and mysql
But it still isn't working..... why? What have I missed?
Enviroment
OS : Win7
Eclipse : J2EE Mars
Tomcat : tomcat 8.0
java : 1.8.0_73
db name : test
username: root
password: 123
controller:
writeDB testDB = new writeDB(tablename);
writeDB.java
....
Class.forName("com.mysql.jdbc.Driver");
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
//-------error-------
dataSource = (DataSource) envContext.lookup("jdbc/test");
//-------error-------
}catch(NamingException ex)
{
throw new RuntimeException(ex);
}
web.xml (in my project)
<!-- MySQL JNDI -->
<resource-ref>
<description>MySQL DB</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml
(in TOMCAT_HOME/conf)
(Also in workspace\Servers\Tomcat v8.0 Server at localhost-config)
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/eatST">
<Resource
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
maxActice="100"
maxIdle="30"
maxWait="10000"
username="root"
password="123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?
useUnicode=true&characterEncoding=UTF8"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
/>
</Context>
Logs
Servlet.service() for servlet [commentCtr] in context
with path [/eatST] threw exception
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.BasicDataSource
cannot be cast to javax.activation.DataSource
at com.joe.db.writeDB.<init>(writeDB.java:58)
at com.joe.servlet.CommentCtr.doPost(CommentCtr.java:38)
at ............

You've imported (and presumable programmed to) an incorrect DataSource. Your exception (java.lang.ClassCastException ... cannot be cast to javax.activation.DataSource) is telling you that you have usedjavax.activation.DataSource, but you wanted javax.sql.DataSource. Modify com.joe.db.writeDB and change
import javax.activation.DataSource;
to
import javax.sql.DataSource;
Also, you don't need Class.forName("com.mysql.jdbc.Driver"); (it doesn't hurt anything, but JDBC drivers register themselves now).

Related

porting web app to Tomcat: javax.naming.NameNotFoundException:

New user of Tomcat (8.5.9) on Linux CentOS 7 with Java SE 8. I must be making a simple mistake. This should be a textbook example how to configure a JDBC connection pool for tomcat.
I have this error:
javax.naming.NameNotFoundException: Name [jdbc/pool1] is not bound in this Context. Unable to find [jdbc]
Any idea what I could doing wrong? Tomcat states It is NOT recommended to place <Context> elements directly in the server.xml file. Thus, my setup:
$CATALINA_HOME/webapps/myapp/META-INF/context.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/pool1"
auth="Container"
type="javax.sql.DataSource"
username="xx"
password="xx"
driverClassName="oracle.jdbc.OracleDriver"
url="xx"
maxTotal="256"
maxIdle="8"
initialSize="4"
removeAbandonedTimeout="7200"
removeAbandonedOnBorrow="true"/>
<Resource name="jdbc/pool2"
auth="Container"
type="javax.sql.DataSource"
username="xx"
password="xx"
driverClassName="oracle.jdbc.OracleDriver"
url="xx"
maxTotal="256"
maxIdle="8"
initialSize="4"
removeAbandonedTimeout="7200"
removeAbandonedOnBorrow="true"/>
<ResourceLink name="jdbc/pool1"
global="jdbc/pool1"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/pool2"
global="jdbc/pool2"
type="javax.sql.DataSource"/>
</Context>
$CATALINA_HOME/webapps/myapp/WEB-INF/web.xml is as follows:
...
<resource-ref>
<description>xxx</description>
<res-ref-name>jdbc/pool1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>xxx</description>
<res-ref-name>jdbc/pool1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
The code causing the exception:
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/pool1");
conn = ds.getConnection();
I have not modified $CATALINA_HOME/conf/server.xml at all. Did I configure something incorrectly, or am I missing setting another file up somewhere?
UPDATE 1
I tried adding the above ResourceLinks to the GlobalNamingResources tag in the $CATALINA_HOME/conf/server.xml file, then stopping/starting Tomcat, but I got the same error.
UPDATE 2
I then added the Resource tags from context.xml above also to the server.xml file (GlobalNamingResources tag), stopping/starting tomcat, and got same error.
UPDATE 3
I got everything working with Andreas' expert help (thanks!) by changing the way java calls the pool:
Context initCtx = new InitialContext();
Context context = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("jdbc/pool1");
conn = ds.getConnection();
Also, the ResourceLinks should not be in server.xml (they simply generate a warning in tomcat log).
Your $CATALINA_BASE/conf/server.xml file should contain the full <Resource> element. Remember to also add the JDBC driver jar file to Tomcat's $CATALINA_BASE/lib folder, since it is Tomcat, not your webapp, that needs it when the <Resource> is defined in server.xml.
Next, the META-INF/context.xml is a template that is used the first time your webapp is deployed. It gets copied to $CATALINA_BASE/conf/Catalina/localhost/myapp.xml, and is likely not updated/refreshed if you change META-INF/context.xml.
The .../Catalina/localhost/myapp.xml file should contain the <ResourceLink> element, mapping the name used by the webapp to the name used in server.xml. Keeping those two names the same is easiest, but not required.
Tomcat works fine without the <resource-ref> elements in WEB-INF/web.xml, but it's better if they are there, for compatibility with other Servlet containers.
Note: $CATALINA_BASE is usually the same as $CATALINA_HOME, i.e. the folder where Tomcat is installed, unless you explicitly configure it otherwise.
So, $CATALINA_BASE/conf/server.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server ...>
...
<GlobalNamingResources>
...
<Resource name="jdbc/pool1" auth="Container" type="javax.sql.DataSource" ... />
<Resource name="jdbc/pool2" auth="Container" type="javax.sql.DataSource" ... />
...
</GlobalNamingResources>
...
</Server>
and $CATALINA_BASE/conf/Catalina/localhost/myapp.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink name="jdbc/pool1" global="jdbc/pool1" type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/pool2" global="jdbc/pool2" type="javax.sql.DataSource"/>
</Context>
and place ojdbcXXX.jar in $CATALINA_BASE/lib.

tomcat.dbcp... Cannot create JDBC driver of class '' for connect URL 'null'

Lots of answers to this question already but none of them are working for me. I feel like I've tried everything. Anyone else have any ideas?
code:
private DataSource ds1;
private Connection dc1 = null;
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
ds1 = (DataSource)envContext.lookup("jdbc/CMS1-DEV");
dc1 = ds1.getConnection();
WEB-INF/web.xml:
<resource-ref>
<description>CMS Data</description>
<res-ref-name>jdbc/CMS1-DEV</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
META-INF/context.xml
<context>
<resource
name="jdbc/CMS1-DEV"
auth="Container"
type="jaxax.sql.DataSource"
username="******"
password="******"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bitnami_wordpress"
maxActive="10" maxIdle="4"/>
I have the mysql-connector-java-5.1.17.jar in the META-INF/lib directory
stack trace:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at us.deans.parrot.ParrotController.doPost(ParrotController.java:77)
There is a typo here in your configuration - "jaxa" instead of "java".
Change following from:
type="jaxax.sql.DataSource"
To
type="javax.sql.DataSource"

javax.naming.NameNotFoundException: Name [jdbc/eswastha] is not bound in this Context. Unable to find [jdbc]

I shall describe the environment first
environment: : Netbeans 7.2 and Tomcat 7.0.27.0 is configured with Netbeans ID
when i take the build separately and put it in webapps folder and run than there is no issue but when i run the application in the IDE itself i get javax.naming.NameNotFoundException: Name [jdbc/eswastha] is not bound in this Context. Unable to find [jdbc]. this exception.
conf/context.xml
<Resource name="jdbc/eswastha" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/eswastha"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="r14#17*" />
and web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jjdbc/eswastha</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and the java class :
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class JDBCManager {
public Connection mysqlConnection() {
Connection dbConnection = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/eswastha");
dbConnection = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
}
Please help me to identify the issue.
Regards
Is it missing?
Check
<res-ref-name>jjdbc/eswastha</res-ref-name>
and
<Resource name="jdbc/eswastha"..../>
Based on your comment :
Cannot create JDBC driver of class '' for connect URL 'null'
Make sure your JDBC Driver and copy driver jar file under tomcat-home/lib.
It will be reference for your. here
You may want to try to access the data source in full JNDI name, e.g.: "java:comp/env/jdbc/test"
Source: http://www.coderanch.com/t/442367/Tomcat/jdbc-bound-Context
On second note, I noticed that you've defined jjdbc, instead of jdbc in web.xml:
<res-ref-name>jjdbc/eswastha</res-ref-name>

Tomcat, Java & Oracle9. org.apache.naming.NamingContext lookup

I am using Tomcat and Java (through Eclipse) and Oracle Database 9.2.1
I am getting
org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException: oracle.jdbc.OracleDriver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver
(PooledConnection.java:243)
My code is
context.xml
<Resource type="javax.sql.DataSource" auth="Container"
name="jdbc/charmDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#localhost:1521:db"
username="db" password="db" maxActive="20" maxIdle="10"
/>
<Resource name="jdbc/charmDB" auth="Container"
type="javax.sql.DataSource"
description="My Database"/>
Java code
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/charmDB");
Connection con = ds.getConnection();
Does anyone know what's wrong?
Thanks!
Your context.xml appears to have two resources with the same name: jdbc/charmDB.
Try resolving this issue first.
EDIT: Make sure your Oracle JDBC driver jar is in the Tomcat lib directory.

tomcat oracle datasource

I have apache tomcat 5.5.28 on my windows box and I am trying to deploy a web application (WAR) which works fine on other servers.
However I am having trouble creating a datasource. I am not sure of the format. The db is oracle.
Here is what I have in server.xml.
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
name="tomdb11"
type="oracle.jdbc.pool.OracleDataSource"
maxActive="4"
maxIdle="2"
username="tomdb11"
maxWait="5000"
driverClassName="oracle.jdbc.driver.OracleDriver"
validationQuery="select * from dual"
password="remotedb11"
url="jdbc:oracle:thin:#dbserver:1521:orcl"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
How do I access this in the web.xml where usually what I have which works in other servers is
<context-param>
<param-name>databaseUser</param-name>
<param-value>tomdb11</param-value>
</context-param>
<context-param>
<param-name>databasePassword</param-name>
<param-value>tomdb11</param-value>
</context-param>
<context-param>
<param-name>databaseSchema</param-name>
<param-value>TOMDBADMINN11</param-value>
</context-param>
Also am I missing something?
Edit: I get the following exception:
javax.naming.NameNotFoundException: Name tomdb11 is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.taw.database.DatabaseService.<init>(DatabaseService.java:19)
at com.taw.database.DatabaseServices.init(DatabaseServices.java:40)
If exception tells that it cannot find jdbc in the JNDI context, then it roughly means that you tried to obtain the DataSource as follows
dataSource = new InitialContext().lookup("jdbc/tomdb11");
while your server.xml file tells the following:
<Resource
name="tomdb11"
>
Those names are not the same. In fact, you should have been used:
dataSource = new InitialContext().lookup("tomdb11");
In Tomcat, however, the InitialContext doesn't directly point to java:comp/env/, so you'll need to replace it by:
dataSource = new InitialContext().lookup("java:comp/env/tomdb11");
The normal practice, however, is that you specify JDBC datasources with the jdbc prefix. So I would rename the resource as
<Resource
name="jdbc/tomdb11"
>
and access it by
dataSource = new InitialContext().lookup("java:comp/env/jdbc/tomdb11");
In the webapp's web.xml you should however also have the following resource declaration:
<resource-env-ref>
<resource-env-ref-name>jdbc/tomdb11</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
For more details about Tomcat JNDI check this HOWTO: http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html. Hope this helps.
First... Make sure you have an Oracle JDBC Jar in your $TOMCAT_HOME/common/lib.
Second... Make sure your web.xml also contains a block like this...
<resource-ref>
<description>Oracle Datasource</description>
<res-ref-name>tomdb11</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
As for your <context-param>, I'm not sure that is doing anything as you already have those things defined in your <Resource>.

Categories

Resources