java - Tomcat6连接mySQL问题

标签 java mysql apache tomcat

我已经关注了另一个 stackover 流线程来达到这一点,就是这个:

Tomcat6 MySql JDBC Datasource configuration

我遇到的问题是那一行:

Connection conn = ds.getConnection();

来自这个区 block :

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

Eclipse 给我错误 getConnection() is undefined for the type DataSource。

它的解决方案是这样做的:

    Connection conn = ((java.sql.Statement) ds).getConnection();

没有教程表明需要这样做,而且当我这样做时它不起作用。我正在使用名为 mysql-connector-java-5.1.18-bin 的 mySQL jar我以前将它与 RMI 一起使用过,但从未与 Tomcat 一起使用过,它是与 Tomcat 一起使用的正确类型吗?

TIA

最佳答案

如果我查看 Java API 文档 http://docs.oracle.com/javase/6/docs/api/我找到了带有 getConnection() 方法的 javax.sql.DataSource 接口(interface)。我假设您的 DataSource 不是实现 javax.sql.DataSource 接口(interface)的东西。导入了什么“DataSource”?

关于java - Tomcat6连接mySQL问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080121/

相关文章:

java - 保存代码生成的字符串层次结构

mysql - 有条件触发

mysql - 使用 SQL 和 Azure API 获取 Azure 中的前 20 个元素

apache - Tomcat - 在运行时区分 DEV 和 PROD 服务器

apache - Windows 10 Creators Update (1703) 后 WampServer Apache httpd.exe 应用程序无法正确启动 (0xc0000142)

java - 将 Java 字符串拆分为具有特殊字符和复杂情况的空格

java - JLabel.setText() 方法

regex - 仅当文件存在时,如何让 Apache 禁止文件?

java - MySQL-Server/Apache Tomcat 和使用 Hibernate/JPA 的 Tapestry 项目的组合使用什么配置?

mysql - Sails.JS + MySQL : Unknown column 'NaN' in 'where clause'