我无法在 tomcat 中使用 JNDI 连接 MySQL
I can't use JNDI in tomcat to connect MySQL
我做了这一切:
how to connect tomcat 7 and mysql
但它仍然无法正常工作......为什么?我错过了什么?
环境
OS : Win7
日食:J2EE 火星
Tomcat : tomcat 8.0
java : 1.8.0_73
数据库名称:测试
用户名:root
密码:123
控制器:
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(在我的项目中)
<!-- 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
(在TOMCAT_HOME/conf)
(也在 workspace\Servers\Tomcat v8.0 服务器的 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>
日志
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 ............
您输入了(并且可能编程为)不正确的 DataSource
。您的异常 (java.lang.ClassCastException ... cannot be cast to javax.activation.DataSource
) 告诉您您使用了 javax.activation.DataSource
,但您想要 javax.sql.DataSource
。修改com.joe.db.writeDB
并更改
import javax.activation.DataSource;
到
import javax.sql.DataSource;
此外,您 不需要 Class.forName("com.mysql.jdbc.Driver");
(这不会造成任何伤害,但 JDBC 司机现在自己注册)。
我做了这一切:
how to connect tomcat 7 and mysql
但它仍然无法正常工作......为什么?我错过了什么?
环境
OS : Win7
日食:J2EE 火星
Tomcat : tomcat 8.0
java : 1.8.0_73
数据库名称:测试
用户名:root
密码:123
控制器:
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(在我的项目中)
<!-- 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
(在TOMCAT_HOME/conf)
(也在 workspace\Servers\Tomcat v8.0 服务器的 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>
日志
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 ............
您输入了(并且可能编程为)不正确的 DataSource
。您的异常 (java.lang.ClassCastException ... cannot be cast to javax.activation.DataSource
) 告诉您您使用了 javax.activation.DataSource
,但您想要 javax.sql.DataSource
。修改com.joe.db.writeDB
并更改
import javax.activation.DataSource;
到
import javax.sql.DataSource;
此外,您 不需要 Class.forName("com.mysql.jdbc.Driver");
(这不会造成任何伤害,但 JDBC 司机现在自己注册)。