JSP database operations routines-stored procedures

Source: Internet
Author: User
Tags odbc
<%--
Author: He Zhiqiang [hhzqq@21cn.com]
Date: 2000-08-04
2000-08-16
Version: 1.1
Functions: JSP database Operations routines-stored procedures-Jdbc-odbc-sql Server

The stored procedures for SQL Server are as follows:
CREATE PROCEDURE Sp_jsptest
@yourname varchar (50),
@myname varchar (m) output
As
Select "Hello," + @yourname + ", very pleased to meet you, ^_^"
Set @myname = "He Zhiqiang"
Return 1
Go
--%>

<%@ page contenttype= "text/html;charset=gb2312"%>

<%
Variable declaration
Java.lang.String StrName; Name

Get data entered by the user
StrName = Request.getparameter ("name");
if (strname==null) {//user does not enter a name
%>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
&LT;TITLE&GT;JSP database Operations routines-stored procedures-Jdbc-odbc-sql server</title>
<body>

<form action= "jdbc-odbc.jsp" method= "POST" >
Your name: <input type= "text" name= "maxlength=" >
<input type= "Submit" value= "submitted" >
</form>

</body>

<%
}
else{
Make the necessary character encoding conversion for the data entered by the user
StrName = new Java.lang.String (strname.getbytes ("iso-8859-1"));

Variable declaration
Java.sql.Connection Sqlcon; Database Connection objects
Java.sql.CallableStatement sqlstmt; Callable Statement Object
Java.sql.ResultSet Sqlrst; Result set Object

Java.lang.String Strcon; Database connection string
Java.lang.String strSQL; SQL statement

Java.lang.String Strwelcome; Welcome words
Java.lang.String Strmyname; My name.
int intreturn; return value

Load JDBC-ODBC Driver
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

To set the database connection string
Strcon = "Jdbc:odbc:jspdemo";

Connecting to a database
Sqlcon = Java.sql.DriverManager.getConnection (Strcon, "sa", "");

Preparing SQL statements
strSQL = "{? = Call Sp_jsptest (?,?)} ";

To prepare a callable statement object
sqlstmt = Sqlcon.preparecall (strSQL);

Set Input parameters
Sqlstmt.setstring (2,strname);

Register OUTPUT parameters
Sqlstmt.registeroutparameter (1,java.sql.types.integer);
Sqlstmt.registeroutparameter (3,java.sql.types.varchar);

Executes the stored procedure and returns the result set
Sqlrst = Sqlstmt.executequery ();

Get data from the result set
Sqlrst.next ();
Strwelcome = sqlrst.getstring (1);

Get the value of an output parameter
Strmyname = sqlstmt.getstring (3);

Get return value
Intreturn = Sqlstmt.getint (1);

Turn off recordsets
Sqlrst.close ();

To close a callable statement object
Sqlstmt.close ();

Close Database objects
Sqlcon.close ();
%>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
&LT;TITLE&GT;JSP database Operations routines-stored procedures-Jdbc-odbc-sql server</title>
<body>

<%=strWelcome%><br>
I'm a <%=strMyName%><br>
The return value is <%=intReturn%>

</body>

<%
}
%>


Related Article

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.