Calling stored procedures in Java

Source: Internet
Author: User

Stored Procedures in Databases

Create procedure [DBO]. [proc_deletestubyno]
-- Add the parameters for the stored procedure here
@ Stuno char (7 ),
@ Result int output
As
Begin
Set nocount on;
Delete from studinfo where studno = @ stuno
Set @ result = @ rowcount
End

 

Java calls the stored procedure:

String stuno = "A1 ";

Int result = 0;

Connection connection = conndb. getconection ();
Callablestatement callstm = NULL;
Try {
Callstm = connection. preparecall ("{call proc_deletestubyno (?,?)} ");
Callstm. setstring ("stuno", stuno); // Add an input parameter to the callablestatement object
Callstm. registeroutparameter ("result", java. SQL. types. integer); // registers output parameters for the callablestatement object

} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Callcmd.exe cuteupdate ();
Result = callstm. getint ("result"); // obtain the value in the output parameter
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Try {
Callstm. Close ();
Connection. Close ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
If (result> 0 ){
System. Out. println ("deleted successfully ");
} Else {

System. Out. println ("failed to delete ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.