Hi Folks, I am trying to recast the JDBC Statement object that is given to
me from a Connection object that I get out of Tomcat's datasource connection
pool. If I recast the generic java.sql.Statement to a
com.mysql.jdbc.Statment, I can use the non JDBC compliant methods such as
getLastInsertID. When I do this in a test class using standard JDBC, it
works perfectly. But when I try to do this from a connection object that is
retrived from tomcat's connection pool, I get a ClassCastException. Does the
datasource mechanism alter the connection somehow so that it would no longer
give me com.mysql.jdbc.Statment objects, but some other kind?

 

Here's the code that works...

 

            Class.forName("com.mysql.jdbc.Driver");

            Connection con =
DriverManager.getConnection(:mysql://jedi.x:3306/vegas?autoReconnect=true,
username, password);

            

            Statement stmt = con.createStatement();

            stmt.executeUpdate("INSERT INTO role VALUES (null, 'test',
'123')");

            

            com.mysql.jdbc.Statement m = (com.mysql.jdbc.Statement) stmt;

            long id = m.getLastInsertID();

 

Here's the code that doesn't work:

 

            Context ctx = new InitialContext();

            DataSource ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/vegas");

            Connection con = ds.getConnection();

            

            Statement stmt = con.createStatement();

            stmt.executeUpdate("INSERT INTO role VALUES (null, 'test',
'123')");

            

            com.mysql.jdbc.Statement m = (com.mysql.jdbc.Statement) stmt;

            long id = m.getLastInsertID();

 

I should note that other than this, everything works fine with the
datasource. Its configured properly (I think) and I can fully access the
database in all respects.

 

I am using:

Tomcat 4.1.24

Linux 2.4.20

Mysql 4.0.13

Java 1.4.1.03

Mysql Connector/J 3.0.8

 

Thanks For the help!

 

Joe Krause

 

Reply via email to