Reputation: 135
Trying to connect to a (local) SQL database using jTDS JDBC drivers.
JAVA CODE
private JtdsDataSource dataSource = null;
public Connection getConnection() throws SQLException, NamingException {
...
Context initContext = new InitialContext();
dataSource = (JtdsDataSource) initContext.lookup("java:comp/env/jdbc/postcodes");
conn = dataSource.getConnection();
...
}
CONTEXT.XML
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/postcodes"
auth="Container"
type="javax.sql.DataSource"
username="user"
password="pass"
driverClassName="net.sourceforge.jtds.jdbcx.JtdsDataSource"
url="jdbc:jtds:sqlserver://localhost:1433/AUSPostcodes"
validationQuery="select 1"
maxActive="10"
maxIdle="4"/>
</Context>
When I run this on tomcat, get the following error....
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to net.sourceforge.jtds.jdbcx.JtdsDataSource
Would appreciate some help with this.
Thanks in advance.
Upvotes: 1
Views: 1521
Reputation: 108971
Tomcat doesn't give you access to a JtdsDataSource
directly. It only uses that datasource to populate its internal DBCP datasource. When you use JNDI to ask for a datasource, you get the DBCP datasource.
BTW: There is (usually) no reason why you would cast to any other interface than javax.sql.DataSource
. The solution therefor is to cast to that interface (javax.sql.DataSource
).
Upvotes: 1