Calling Stored Procedures from a database ( MySQL )
Hello, I've been trying to get my stored procedures to work for more than a week now, I am able to connect to my database, register the output, but when it get's to the execute query line, the try statement drops out. Any Ideas, I tried using a resultset earlier, but have created a single value returning stored proc now.
publicstatic String sp01(){
String returnedID2="21345";
String returnedID="1";
int rtid;
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/corrovu?user=root&password=Glancino");
CallableStatement cStmt = conn.prepareCall("{? = call sp3()}");
returnedID="2";//2 is returned
cStmt.registerOutParameter(1, java.sql.Types.INTEGER);
returnedID="3";//3 is returned
cStmt.execute();
returnedID="4";//4 fails to return !!!!
rtid = cStmt.getInt(1);
returnedID="5";
if (returnedID ==null){
returnnull;
}else{
return returnedID;
}
}catch(Exception e){
}
return returnedID;//Note I had to insert this line to get the method working
}
If I type CALL sp3(); in the query editor, it returns '4' which is the value in the database.
I don't know why it dies on the cStmt.execute(); Am I not pereparing the call correctly? I tried "? = CALL sp3();" as well to no avail.
Thanks, looking forward to the replies, Once I have got this working, I will write up the correct methods to access stored procs's from MySQL.
[2513 byte] By [
3Pc] at [2007-11-26 6:33:01]

# 1
Hi,Have a look at this thread http://forum.sun.com/jive/thread.jspa?forumID=123&threadID=63648MJ
# 2
http://dev.mysql.com/doc/refman/5.0/en/cj-using-callable-statements.html
# 3
Thank you for the fast replies, I have seen these examples before and tried them, this metod is still crashing out when it hit's the execute() command. If you open a new project, insert a button, called button1, insert 2 text boxes called, txt1 & txt2, open up the java and insert this code in the relevent spots. To see where it fails, include the returnedID lines i've included in the comment lines.
The stored procedure is simple, it just seraches a string and returns a single integer.
DELIMITER $$;
DROP PROCEDURE IF EXISTS `corrovu`.`sp3`$$
CREATE PROCEDURE `sp3`()
BEGIN
SELECT diam FROM details WHERE PID = '0050003';
END$$
DELIMITER ;$$
diam is '4' where PID is '0050003' so the txt1 should display 4 but it doesn't, this is really driving me crazy, I've probably spent more than 50 hours trying to get the stored proc working, I don't know what more to do. I've looked at hundreds of expamples and can't get any of them working.
package spca;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.Page;
import javax.faces.FacesException;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.StaticText;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
public class Page1 extends AbstractPageBean {
static int outputValue = 69;
public static String sp01() {
String returnedID2="21345";
String returnedID="1";
try {
//The following line loads the driver (it work's to see include - returnedID="2";)
Class.forName("com.mysql.jdbc.Driver").newInstance();
//This line connects to the database (it work's )- returnedID="3";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/corrovu?user=root&password=Glancino");
//This line prepares the call (apparantly work's, If i put a non-existant stored proc, this line fails)- returnedID="4";
CallableStatement cStmt = conn.prepareCall("{?= call sp3()}");
//This line registers the out paramaters (it apperntly works, If I insert incorrect values, it fails)- returnedID="5";
cStmt.registerOutParameter(1, Types.INTEGER);
//This line exit's the Try, I changed it as per the example, it still fails. I don't know what I'm doing wrong.- returnedID="6";
boolean hadResults = cStmt.execute();
//The rest of the code doesn't get parsed - returnedID="7";
while (hadResults) {
ResultSet rs = cStmt.getResultSet();
hadResults = cStmt.getMoreResults();
}
outputValue = cStmt.getInt(1); // index-based
if (returnedID == null){
return null;
} else {
return returnedID;
}
} catch(Exception e){
}
//I had to insert this following line to make this method work, this was not in the example as per these forums?
return returnedID;
}
public String button1_action() {
txt1.setText(sp01());
txt2.setText(""+outputValue);
return null;
}
}
Cheers, I hope someone can help me.
3Pc at 2007-7-6 14:33:00 >

# 4
> Thank you for the fast replies, I have seen these
> examples before and tried them, this metod is still
> crashing out when it hit's the execute() command.
How do you know that it's on the execute()?
You don't print anything in the exception block, such as printing
the exception's message and the stack trace.
DON'T EVER DO THAT.Now if you did, but just removed
it from your post, then please forgive my yelling.
But that will tell you the line it's barfing on.
Just looking at your code I can tell it won't work.
Assuming you don't want to change your stored proc....
> The stored procedure is simple, it just seraches a
> string and returns a single integer.
NO it does NOT return a single integer.
> CREATE PROCEDURE `sp3`()
> BEGIN
> SELECT diam FROM details WHERE PID = '0050003';
> END$$
>
It returns a ResultSet, with the ResultSet having 0 or more rows.
The ResultSet has one column, which I'll take your word for it
that's it's an integer.
> conn.prepareCall("{?= call sp3()}");
let's try "{ call sp3()}"
> //This line registers the out paramaters
> (it apperntly works, If I insert incorrect values, it
> fails)- returnedID="5";
> cStmt.registerOutParameter(1,
> Types.INTEGER);
remove that.
> //This line exit's the Try, I changed it
> as per the example, it still fails. I don't know what
> I'm doing wrong.- returnedID="6";
> boolean hadResults = cStmt.execute();
> //The rest of the code doesn't get parsed -
> returnedID="7";
> while (hadResults) {
>ResultSet rs = cStmt.getResultSet();
boolean hadResults = cStmt.execute();
if (hadResults) { // stored proc only has one SELECT, so no loop necessary.
ResultSet rs = cStmt.getResultSet();
if ( rs.next() ) { // make sure there's one row.
outputValue = rs.getInt(1) ;
resultId = Integer.toString(outputValue) ;
}
}
Please try again.
And this isn't a Creator question, so I suggest you move on
over to the JDBC forum at
http://forum.java.sun.com/forum.jspa?forumID=48
