You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Edwin van Nuil <nu...@oblivion.nl> on 2002/08/13 14:55:11 UTC

Auto Increment with JDBC and MySQL stopped working ?

I have a application where I use the statement :

last_id = ((org.gjt.mm.mysql.Statement) pstmt).getLastInsertID();

with struts 1.1b1 and mm.mysql driver (most recent) it works good. But
now I have upgraded to a newer version of struts (and mysql) and it
stopped working. I get the following error :

java.lang.ClassCastException:
org.apache.commons.dbcp.DelegatingPreparedStatement
	at
nl.linuxonline.shop.product.PictureJdbcBean.insert(PictureJdbcBean.java:
67)
	at
nl.linuxonline.shop.product.CreatePictureAction.execute(CreatePictureAct
ion.java:100)
	at
org.apache.struts.action.RequestProcessor.processActionPerform(RequestPr
ocessor.java:446)
.....

I replaced the code with a max(id) and now it works again, but does
anybody know why this doesn't work anymore ??

Or is there a better way to use autoincrement ??

Regards,

Edwin


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Auto Increment with JDBC and MySQL stopped working ?

Posted by John Rothfield <jo...@sfhypnosis.com>.
You might have to dig into the mm.mysql and jdbc drivers and change the
cast appropriately.

Heres a snippet of code that I used:
			// Non-portable code follows. Only works with jboss and mysql.
			// be sure to add jbosspool.jar and mm-mysql2.0.jar to your classpath!!
			// Now uses some trickery to get the last autoincrement id generated by
mysql
			// to do this we need to access methods on mysql's implementation of
statement
			// first, cast stmt to a jboss statement. Then get the underlying
statement and
			// cast it to a mysql statement. Then calll getLastInsertID to get the
last autogenerated
			// id.
			if (L.ON) log.debug("stmt.class="+stmt.getClass());

			if (stmt instanceof org.gjt.mm.mysql.jdbc2.PreparedStatement) {

retValue=(int)((org.gjt.mm.mysql.jdbc2.PreparedStatement)stmt).getLastInsert
ID();
			} else {
				org.jboss.pool.jdbc.StatementInPool jbossStmt=
					(org.jboss.pool.jdbc.StatementInPool)stmt;
				org.gjt.mm.mysql.jdbc2.PreparedStatement mysqlStmt=

(org.gjt.mm.mysql.jdbc2.PreparedStatement)jbossStmt.getUnderlyingStatement();
				retValue=(int)mysqlStmt.getLastInsertID();
			}


Regards, John

At 02:55 PM 8/13/2002 +0200, you wrote:
>I have a application where I use the statement :
>
>last_id = ((org.gjt.mm.mysql.Statement) pstmt).getLastInsertID();
>
>with struts 1.1b1 and mm.mysql driver (most recent) it works good. But
>now I have upgraded to a newer version of struts (and mysql) and it
>stopped working. I get the following error :
>
>java.lang.ClassCastException:
>org.apache.commons.dbcp.DelegatingPreparedStatement
>	at
>nl.linuxonline.shop.product.PictureJdbcBean.insert(PictureJdbcBean.java:
>67)
>	at
>nl.linuxonline.shop.product.CreatePictureAction.execute(CreatePictureAct
>ion.java:100)
>	at
>org.apache.struts.action.RequestProcessor.processActionPerform(RequestPr
>ocessor.java:446)
>.....
>
>I replaced the code with a max(id) and now it works again, but does
>anybody know why this doesn't work anymore ??
>
>Or is there a better way to use autoincrement ??
>
>Regards,
>
>Edwin
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>