简体   繁体   中英

Tomcat6 connect to mySQL problems

I've followed another stackover flow thread to get to this point, it was this one here:

Tomcat6 MySql JDBC Datasource configuration

The problem I have is that the line that goes:

Connection conn = ds.getConnection();

from this 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 gives me the error getConnection() is undefined for the type DataSource.

Its solution is to do this:

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

No tutorials show the need to do this, and its not working when I do that. I'm using mySQL jar named, mysql-connector-java-5.1.18-bin I've used it with RMI before but never Tomcat, is it the correct type for use with Tomcat?

TIA

If I look into the Java API docs http://docs.oracle.com/javase/6/docs/api/ I find the javax.sql.DataSource interface with a getConnection() method. I assume your DataSource to be something else than one implementing the javax.sql.DataSource interface. What "DataSource" is imported?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM