Get the App
SLTechnology News&Howtos  ›  Database  › 

How to call SqlServer stored procedure in Java

Shulou Source: shulou.com Published: 2022-05-31 21:40:27 09月29日 Update

Java how to call SqlServer stored procedures, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Database connection

Packagecom.hujuan.conn;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;publicclassDatabaseConn {publicstaticConnectiongetConnection () {StringclassName= "com.microsoft.jdbc.sqlserver.SQLServerDriver"; Stringurl= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=login"; Stringname= "sa"; Stringpassword= "sasa"; try {Class.forName (className); returnDriverManager.getConnection (url,name,password);} catch (ClassNotFoundExceptione) {e.printStackTrace ();} catch (SQLExceptione) {e.printStackTrace ();} returnnull;}}

two。 Call a stored procedure through a connection

Packagecom.hujuan.dao;importjava.sql.CallableStatement;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Types;importcom.hujuan.conn.DatabaseConn;publicclassSqlServerProc {publicstaticvoidmain (String [] args) {Connectionconn=null;CallableStatementcall=null;conn=DatabaseConn.getConnection (); try {call=conn.prepareCall ("{callinsertUserinfo (,)}"); call.setString (1, "hujuan"); call.setString (2, "female"); call.setString (3, "1985-06-16"); call.registerOutParameter (4) Call.executeUpdate (); intvalue=call.getInt (4); System.out.println (value);} catch (SQLExceptione) {e.printStackTrace ();} finally {try {if (callbacks null) call.close (); if (connexual null) conn.close ();} catch (SQLExceptione) {e.printStackTrace ();}

How to implement Java calling SqlServer stored procedure

Returns the result set

Packagecom.hujuan.dao;importjava.sql.CallableStatement;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Types;importcom.hujuan.conn.DatabaseConn;publicclassSqlServerProc {publicstaticvoidmain (String [] args) {Connectionconn=null;CallableStatementcall=null;ResultSetrs=null;conn=DatabaseConn.getConnection (); try {call=conn.prepareCall ("{= callselectUserinfo ()}"); call.registerOutParameter (1Med type.real); rs=call.executeQuery (); while (rs.next ()) {System.out.println (rs.getString ("name")) }} catch (SQLExceptione) {e.printStackTrace ();} finally {try {if (rswatches null) rs.close (); if (callbacks null) call.close (); if (connexion null) conn.close ();} catch (SQLExceptione) {e.printStackTrace ();} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Process storage help clarity content to this data database article novice more knowledge result industry information information channel problem demand channel enter one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Docker Huawei Shulou Tech Info