JSP调用SQL server 2000存储过程

ZDNet软件频道 时间:2008-09-22 作者: | 中国软件网 我要评论()
本文关键词:Server SQL JSP SQL Server
JSP调用SQL server 2000存储过程

 1.新建表:
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 procedure get_info @name varchar(50),@phone varchar(13),@addr varchar(50)
as
select *
from mytest
go

3.JSP代码:
<%@ 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";
//创建一个CallableStatement 对象来调用数据库存储过程
//CallableStatement comm = conn.divpareCall(SQL);
Statement stmt = conn.createStatement();

ResultSet res = stmt.executeQuery(SQL);
while(res.next())...
res.close();
stmt.close();
conn.close();
%>
</body>
</html:html>

4.执行结果:

This is a test for procedure.

1 cc 1234 cs

2 dd 4567 bj

3 ee 1245 ca

Server

SQL

JSP

SQL Server


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134