I've also wished for this, but I think the 'last, grab, first' two-step is the only way to do this.. and of course, only with JDBC 3.0 Scrollable ResultSets (or maybe that was available back in JDBC2). The only other way I can think of is to construct a query exactly the same as the one you are running, except replace the SELECT clause with SELECT COUNT(*). Run this once, either before or after your 'real' query. Of course this isn't so great for cases where someone else inserts a dozen rows between your two queries.
> -----Original Message----- > From: Joe Krause [mailto:[EMAIL PROTECTED] > Sent: Tuesday, July 22, 2003 4:10 PM > To: 'Tomcat Users List' > Subject: RE: JDBC + MySQL Datasource + Tomcat = ClassCastException > > > Ok - implemented the getGeneratedKeys() method and everything > works great. > > Thank you very much Mike! > > On another topic, is there any quick way to determine the > total rows returned in a ResultSet without stepping through > it. I want to build array's or objects and I need to know in > advance what the array dimension will be. I could use lists, > but I'd rather use array's. Here's what I came up with: > > ResultSet rs = ps.executeQuery(); > rs.last(); > int total = rs.getRow(); > rs.beforeFirst(); > > When I get the resultset, I go to the last row, get its > number, and then reset the resultset cursor back to before > the first row. Is there already a method to give me this > data? I looked but couldn't find any... > > Joe > > -----Original Message----- > From: Mike Curwen [mailto:[EMAIL PROTECTED] > Sent: Tuesday, July 22, 2003 1:05 PM > To: 'Tomcat Users List' > Subject: RE: JDBC + MySQL Datasource + Tomcat = ClassCastException > > I'd drop the non-compliant way of getting this value, and use > what's provided by JDBC 3.0 (available with JDK 1.4.x and > ConnectorJ 3.x) > > statement.getGeneratedKeys() > > No casting required. > > > http://www.mysql.com/articles/autoincrement-with-connectorj.html > > > > > -----Original Message----- > > From: Joe Krause [mailto:[EMAIL PROTECTED] > > Sent: Tuesday, July 22, 2003 2:49 PM > > To: '[EMAIL PROTECTED]' > > Subject: JDBC + MySQL Datasource + Tomcat = ClassCastException > > > > > > 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?autoRec > > onnect=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 > > > > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]