JSP調用SQL Server存儲過程的實例
創建表
CREATE TABLE [BookUser] (
[UserID] [int] IDENTITY (
[UserName] [varchar] (
[Title] [nvarchar] (
[Guid] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BookUser_Guid] DEFAULT (newid())
[BirthDate] [datetime] NOT NULL
[Description] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL
[Photo] [image] NULL
[Other] [varchar] (
CONSTRAINT [DF_BookUser_Other] DEFAULT (
CONSTRAINT [PK_BookUser] PRIMARY KEY CLUSTERED
(
[UserID]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
創建存儲過程
CREATE PROCEDURE InsertUser
@UserName varchar(
@Title varchar(
@Guid uniqueidentifier
@BirthDate DateTime
@Description ntext
@Photo image
@Other nvarchar(
@UserID int output
As
Set NOCOUNT ON
If Exists (select UserID from BookUser Where UserName = @UserName)
RETURN
ELSE
Begin
INSERT INTO BookUser (UserName
VALUES(@UserName
SET @UserID = @@IDENTITY
RETURN
End
GO
JSP代碼
<%@ page language="java" contentType="text/html; charset=UTF
<%@ page import = "java
<!DOCTYPE html PUBLIC "
"
<html xmlns="
<head>
</head>
<body>
<%
//注意
Class
String url="jdbc:sqlserver://localhost:
String sql = "{? = call InsertUser(?
Connection cn = null;
CallableStatement cmd = null;
try
{
cn = DriverManager
cmd = cn
java
String FilePath = application
java
Date rightNow = Date
cmd
//注意修改這裡
cmd
&nb
sp; cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
int returnValue = cmd
int UserID = cmd
if(returnValue ==
{
out
out
out
}
else
{
out
}
f
}
catch(Exception ex)
{
out
}
finally
{
try
{
if(cmd != null)
{
cmd
cmd = null;
}
if(cn != null)
{
cn
cn = null;
}
}
catch(Exception e)
{
e
}
}
%>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20269.html