Java calls SQL server stored procedure

Recently, I did database system development at the end of the class, using the java language platform, and the call of the stored procedure was used, but it was not mentioned in the book, so I checked Baidu, and finally made a summary (forgive me for the poor writing)

Stored procedure call without parameters

The stored procedure without parameters is the simplest, but I feel that it is basically not used in the general database management system, because without parameters, the query statement function of the database can be satisfied, but let's take a look at the stored procedures without parameters. How to call the parameters with java. First use the T-SQL statement to create a stored procedure

create proc student
as
select st_id,st_name 
From st_info

Create a stored procedure to query the student number and name in the st_info table

Java calls the student stored procedure (key code)

	try {
	Connection con = null;
   	ResultSet res = null;  
        PreparedStatement statement = null;		 
            Class.forName(cfn);
            con = DriverManager.getConnection(url,"sa","123");
            statement = con.prepareStatement();
            res = statement.executeQuery("{call dbo.student}");//调用student存储过程
            while(res.next()){
                String stid = res.getString("st_id");//获取st_id列的元素         
		String stname=res.getString("st_name");
            }
            
        } catch (Exception a) {
            // TODO: handle exception
            a.printStackTrace();
        }               

Stored procedure call with input parameters

First look at the T-SQL statement
create proc clname @stid nchar(10)
as
select c_name,c_no,score
from st_info
where st_id=@stid

The stored procedure with input parameters looks for a statement to convert the input sql parameters. This statement is the setInt() or setString() method, depending on the format and type of the value of your input parameters.
try{
		Connection con = null;
         Class.forName(cfn);
         con = DriverManager.getConnection(url,"sa","12345");
        ResultSet res = null;
PreparedStatement pstmt=con.prepareStatement("{call dbo.clname(?)}");//带一个输入参数占位符为?
pstmt.setString(1,"0603060108");//参数的输入值
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("c_name") + ","+rs.getString("c_no") +","+rs.getString("score"));
}
rs.close();
pstmt.close();
}
catch(Exception e){
e.printStackTrace();
}
In this code call dbo.clname(?), "?" means the position where we create the input parameters of the stored procedure in SQL. How many input parameters are "?", then pstmt.setString(1,0603060108) means that the input value of the first parameter in the stored procedure is "0603060108". If there are two parameter values ​​in the stored procedure, write it as {call dbo.stored procedure name(?,?)}; ptstmt.setString(1,"xxxx");ptstmt.steString(2,"xxxx"); this form





Guess you like

Origin blog.csdn.net/AAAAAAAKing/article/details/53458402