Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call SqlServer stored procedure in Java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report