In Java's JSP, JSP + jstl, And. Net's Asp.net combined with SQL Server database development, we will introduce the general methods and simplest methods used in stored procedures.
This document uses SQL Server2000 as an example.
1. Use stored procedures in JSP (JSTL:
1. Standard usage of stored procedures in Java:
Various functions in stored procedures can be implemented. Compared with the simplest method, the out parameter and the returned value of the process can be obtained.
? Is the placeholder of the parameter. A question mark represents a parameter.
String proc = "{call procname (?,? )}";
Callablestatement stat = con. preparecall (Proc );
// If the first parameter is in, what is the description? The number should be in the English input method. This blog cannot enter a question mark in English.
Stat. setstring (1, 'paravalue '); // set the parameter value
// If the second parameter is set as out
Stat. registeroutparameter (1, java. SQL. types. varchar); // set the parameter type
Stat.exe cute ();
String paravalue = Stat. getstring (2); // obtain the out parameter value.
For example:
% @ Page import = "Java. SQL. *" %>
% @ Page import = "Java. Text. *" %>
% @ Page contenttype = "text/html; charset = gb2312" %>
% @ Include file = "opendata. jsp" %>
String strsql = "{call pdrivertotal (,)}";
Callablestatement stmt = con. preparecall (strsql );
Stmt. setstring (1, "2005-08-20 ");
Stmt. setstring (2, "2005-10-01 ");
Stmt.exe cute ();
%>
2. The simplest method of stored procedures:
Idea: Execute the process as a normal SQL statement, that is, it is the same as the query and update statement. The execution process can be implemented and relevant datasets can be obtained, but it cannot obtain
The out output parameters of the process are applicable in 90% of development conditions.
String proc = "Exec process name" + parameter 1 + "," + parameter 2;
Statement stat = con. createstatement ();
Resultset rs = stat.exe cutequery (Proc );
For example:
// Used in JSP
Statement stat = con. createstatement ();
ResultSet rs = stat.exe cuteQuery ("zyfproc 20 'name '");
// Zyfproc: stored procedure name 20: int type parameter, name: varchar type parameter
%>
// Use jstl label in jsp
2. Use stored procedures in asp.net (C:
1C # Standard Edition for Stored ProceduresPrivate void SQL _proc ()
{
SqlConnection conn = new SqlConnection ("server =.; uid = sa; pwd = 1234; database = china ");
String SQL = "proc_out ";
SqlCommand comm = new SqlCommand (SQL, conn );
// Change the Command execution type to the stored procedure mode. The default value is Text.
Comm. CommandType = CommandType. StoredProcedure;
// Pass an input parameter, which must be assigned a value
SqlParameter sp = comm. Parameters. Add ("@ uid", SqlDbType. Int );
Sp. Value = 10;
// Define an output parameter without assigning a value. Direction is used to describe the parameter type.
// Direction is the input parameter by default, and has the output parameter and return value type.
Sp = comm. Parameters. Add ("@ output", SqlDbType. VarChar, 50 );
Sp. Direction = ParameterDirection. Output;
// Define the return value parameter of the process. After the process is executed, the return value of the process is assigned to Paremeters named myreturn.
Sp = comm. Parameters. Add ("myreturn", SqlDbType. Int );
Sp. Direction = ParameterDirection. ReturnValue;
// Use SqlDataAdapter to automatically open and close the database, and execute the corresponding t-SQL statement or stored procedure
// If the stored procedure only performs related operations, such as cascade deletion or update, use the execute method of SqlCommand.
SqlDataAdapter da = new SqlDataAdapter (comm );
DataSet ds = new DataSet ();
Da. Fill (ds );
// After the stored procedure is executed, the output parameters are displayed.
String myout = comm. Parameters ["@ output"]. Value. ToString ();
// Print the output parameters:
Response. Write ("print output parameters:" + myout );
// Print the Stored Procedure Return Value
Myout = comm. Parameters ["myreturn"]. Value. ToString ();
Response. Write ("Stored Procedure return value:" + myout );
This. DataGrid1.DataSource = ds;
This. DataGrid1.DataBind ();
}
2. Use lite versions of stored procedures:Private void SQL _jyh ()
{
// The simplest way to use stored procedures as t-SQL statements. Syntax: exec process name parameter
SqlConnection conn = new SqlConnection ("server =.; uid = sa; pwd = 1234; database = china ");
String SQL = "execute proc_out 10, '12 '";
SqlCommand comm = new SqlCommand (SQL, conn );
// Use SqlDataAdapter to automatically open and close the database, and execute the corresponding t-SQL statement or stored procedure
// If the stored procedure only performs related operations, such as cascade deletion or update, use the execute method of SqlCommand.
SqlDataAdapter da = new SqlDataAdapter (comm );
DataSet ds = new DataSet ();
Da. Fill (ds );
// Bind data
This. DataGrid1.DataSource = ds;
This. DataGrid1.DataBind ();
}
In summary, the SQL Server database can be used in any language as a normal query statement.
String SQL = "execute proc_out 10, '12 '";
Using this SQL statement as a parameter can be correctly executed in java or C. This is also the simplest way to use stored procedures.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1633644