JSP calls SQL server 2000 Stored Procedure

Source: Internet
Author: User

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


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.