Detailed description of the stored procedure for calling SQL Server in Java-calling the stored procedure with update count

Source: Internet
Author: User

5. After calling the stored procedure using the sqlservercallablestatement class, you can call the stored procedure using any of the execute or executeupdate methods. The executeupdate method returns an int value that contains the number of rows affected by the stored procedure, but the execute method does not return this value. If you use the execute method and want to obtain the affected number of rows, you can call the getupdatecount method after running the stored procedure. As an instance, create the following tables and stored procedures in the SQL Server 2005 adventureworks sample database:

SQL code: Create Table testtable

(Col1 int identity,

Col2 varchar (50 ),

Col3 INT );

Create procedure updatetesttable

@ Col2 varchar (50 ),

@ Col3 int

As

Begin

Update testtable

Set col2 = @ col2, col3 = @ col3

End;

In the following example, the open connection of the adventureworks sample database will be passed to this function, and the updatetesttable stored procedure will be called using the execute method, and the row count affected by the stored procedure will be returned using the getupdatecount method.

Java code public static void executeupdatestoredprocedure (connection con ){

Try {

Callablestatement cstmt = con. preparecall ("{call DBO. updatetesttable (?, ?)} ");

Cstmt. setstring (1, "");

Cstmt. setint (2,100 );

Cstmt.exe cute ();

Int COUNT = cstmt. getupdatecount ();

Cstmt. Close ();

System. Out. println ("rows affected:" + count );

}

Catch (exception e ){

E. printstacktrace ();

}

}

 

From: http://www.cn-java.com/www1? Action-viewnews-itemid-55626

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.