Java stored procedure call (sqlsever database)

Source: Internet
Author: User

Stored Procedure:

-- Call myproc (?,?,?)
Alter proc myproc
(@ ID int,
@ Customerid nchar (5) output,
@ Employeeid int output
)
Begin
Select @ customerid = customerid, @ employeeid = employeeid from orders
Where orderid = @ ID
End
Go

Declare
@ Cu char (5 ),
@ Em int
Exec myproc 10248, @ Cu output, @ em output
Print (cast (@ em as char (5) + @ Cu)

The following is a Java stored procedure call:

Import java. SQL .*;

Public class study
{
Private connection con;
Public resultset re;
Private callablestatement callsta;
Private string STR;
Private string use = "sa ";
Private string Pwd = "sa ";

Public Study ()
{
Try {
// Connect to the database driver
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
String STR = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = northwind ";
Con = drivermanager. getconnection (STR, use, PWD );

// Set stored procedure parameters
String ST = "{call myproc (?,?,?)} ";
Callsta = con. preparecall (ST );
Callsta. setint (1,10248 );
Callsta. registeroutparameter (2, types. varchar );
Callsta. registeroutparameter (3, types. integer );

// Test whether the call is successful
System.out.println(callsta.exe cute ());

// Output the record results of calling the Stored Procedure cyclically
If(callsta.exe cute () = true)
{
Re = callsta. getresultset ();
While (Re. Next ())
{
System. Out. println (Re. getint (1) + "" + RE. getstring (2) + "" + RE. getint (3 ));
}
}
Else {system. Out. println ("dfas ");}
}
Catch (exception e) {e. printstacktrace ();}
}

Public static void main (string [] age)
{
Study study = new study ();
}
}

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.