1. Create a table:
Create table [mytest] (
[Id] [int] not null,
[Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS not null,
[Phone] [varchar] (13) COLLATE Chinese_PRC_CI_AS NULL,
[Addr] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
2. Create a stored procedure:
Create procedure get_info @ name varchar (50), @ phone varchar (13), @ addr varchar (50)
As
Select *
From mytest
Go
3. JSP code:
<% @ Page language = "java" pageEncoding = "UTF-8" import = "java. SQL. *, tools. Database" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" divfix = "bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" divfix = "html" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-logic" divfix = "logic" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-tiles" divfix = "tiles" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html: html locale = "true">
<Head>
<Html: base/>
<Title> proc_test.jsp </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
</Head>
<Body>
This is a test for procedure. <br>
<%
String username = "sa ";
String password = "123456 ";
String url = "jdbc: microsoft: sqlserver: // 192.168.1.112: 1433; DatabaseName = test ";
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver"). newInstance ();
Connection conn = DriverManager. getConnection (url, username, password );
String SQL = "execute get_info ";
// Create a CallableStatement object to call the database Stored Procedure
// CallableStatement comm = conn. divpareCall (SQL );
Statement stmt = conn. createStatement ();
ResultSet res = stmt.exe cuteQuery (SQL );
While (res. next ())...{
%>
<Table>
<Tr>
<Td> <% = res. getString ("id") %> </td>
<Td> <% = res. getString ("name") %> </td>
<Td> <% = res. getString ("phone") %> </td>
<Td> <% = res. getString ("addr") %> </td>
</Tr>
</Table>
<%}
Res. close ();
Stmt. close ();
Conn. close ();
%>
</Body>
</Html: html>
4. Execution result:
This is a test for procedure.
1 cc 1234 cs
2 dd 4567 bj
3 ee 1245 ca