1

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.