[JConnector] stored proc
Posted by: MAZUIR Nathanaƫl
Date: November 12, 2004 10:11AM

Hello.

I'm facing some difficulties with stored procs in Java code.

Here's my config :
- MySQL : mysql-5.0.1-alpha-snapshot-win.zip
- JConnector : mysql-connector-java-3.1.4-beta.zip

Here's my stored procedure's code :

 
CREATE PROCEDURE simpleproc (OUT param INT) 

BEGIN 
SELECT COUNT(*) INTO param FROM eucalyptus.mouvem04; 
END

When I run the proc from the shell, no problem, I got a result :

 
mysql> CALL eucalyptus.simpleproc(@a); 
Query OK, 0 rows affected (0.06 sec) 

mysql> select @a; 
+-------+ 
| @a | 
+-------+ 
| 59406 | 
+-------+ 
1 row in set (0.00 sec)

When I run the proc from a Java program, I got an error.

Here's my Java code :
 
package com.timci.koala.eucalyptus; 

import java.sql.CallableStatement; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.sql.Types; 

public class ProcTest { 

public static final String URL = "jdbc:mysql://localhost:3306/eucalyptus"; 
public static final String DRIVER = "com.mysql.jdbc.Driver"; 
public static final String USER = "prog"; 
public static final String PASSWORD = "timci"; 

public static void main(String args[]) { 
try { 
Class.forName(DRIVER); 
Connection connection = DriverManager.getConnection(URL, USER, PASSWORD); 
CallableStatement cStmt = connection.prepareCall("{call eucalyptus.simpleproc(?)}"); 

cStmt.registerOutParameter(1, Types.INTEGER); 
cStmt.execute(); 

int outputValue = cStmt.getInt(1); // <- error at this line 
System.out.println("resultat = " + outputValue); 

} catch (ClassNotFoundException ex) { 
ex.printStackTrace(); 

} catch (SQLException ex) { 
ex.printStackTrace(); 
} 
} 
}

Here's the error message when I run the main method :

 
java.sql.SQLException: Parameter index of 1 is out of range (1, 0) 
at com.mysql.jdbc.CallableStatement.checkParameterIndexBounds(CallableStatement.java:1001) 
at com.mysql.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:970) 
at com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:863) 
at com.timci.koala.eucalyptus.ProcTest.main(ProcTest.java:23)

What did (or didn't) I do ? :?
What's wrong in my code ?

I'm stuck. :-(

If someone has an idea (a good one would be great ;-) ) ...

Anyway (if you can't help), thanks for reading this message.

Options: ReplyQuote


Subject
Written By
Posted
[JConnector] stored proc
November 12, 2004 10:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.