wood burning stoves*
The moose likes JDBC and the fly likes Array Index Out Of Bounds Exception: 0  ( Mistake with My MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Array Index Out Of Bounds Exception: 0  ( Mistake with My MySQL "count"?)" Watch "Array Index Out Of Bounds Exception: 0  ( Mistake with My MySQL "count"?)" New topic
Author

Array Index Out Of Bounds Exception: 0 ( Mistake with My MySQL "count"?)

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I am using the Tomcat and MySQL. Below is the message in the Tomcat log:

----- Root Cause -----
java.lang.ArrayIndexOutOfBoundsException: 0
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1845)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1056)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:217)
at org.apache.artimus.message.dao.MySQLMessageDAO.getNumberOfThreadBeans_forReceiver(MySQLMessageDAO.java:514)
at org.apache.artimus.message.ListThreadHandler.getNumberOfThreads_forReceiver(ListThreadHandler.java:74)
at org.apache.artimus.message.ListThread.execute(ListThread.java:119)


I suppose that the problem is with the method getNumberofThreadBeans_forReceiver (which is line # 514 in the MySQLMessageDAO class). And I also suppose that the "0" caused the ArrayIndexOutOfBoundsException (please point out if I am wrong). However, the method is to "count" the number of records found in the database. And I went to look at my database. I was able to find 3 matches with my eyes.

Please help me to see the mistakes in that method (shown below):
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
No you are not thinking in the right direction. See the code you have written

public int getNumberOfThreadBeans_forReceiver( String memberName ) throws AssertionException, DatabaseException { Connection conn = null; PreparedStatement stmt = null; String query = "SELECT count(*) FROM message_thread WHERE message_receiver = '" + memberName + "'"; ResultSet rs = null; try { conn = DBConnection.getDBConnection(); stmt = conn.prepareStatement( query ); stmt.setString( 1, memberName ); rs = stmt.executeQuery();


as you can see you have written String query = "SELECT count(*) FROM message_thread WHERE message_receiver = '" + memberName + "'"; and you are using PreparedStatement. You are setting the message_receiver here in this query and then you are trying to use stmt.setString(1,memberName).There is no ? in the query.
So Strange. Isn't it?

Use String query = "SELECT count(*) FROM message_thread WHERE message_receiver = ?"; instead. It will solve the problem.
[ May 20, 2004: Message edited by: Ali Gohar ]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thank you for pointing out my mistake.

It works now. The error message is gone.
 
GeeCON Prague 2014
 
subject: Array Index Out Of Bounds Exception: 0 ( Mistake with My MySQL "count"?)