The following is an example of a JSP calling SQL Server Stored Procedure:
Create a table:
Create table [BookUser] (
[UserID] [int] IDENTITY (1, 1) not null,
[UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS not null,
[Title] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS not null,
[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] (50) COLLATE Chinese_PRC_CI_AS NULL
CONSTRAINT [DF_BookUser_Other] DEFAULT ('default '),
CONSTRAINT [PK_BookUser] PRIMARY KEY CLUSTERED
(
[UserID]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Create a stored procedure:
Create procedure InsertUser
@ UserName varchar (50 ),
@ Title varchar (255 ),
@ Guid uniqueidentifier,
@ BirthDate DateTime,
@ Description ntext,
@ Photo image,
@ Other nvarchar (50 ),
@ UserID int output
As
Set NOCOUNT ON
If Exists (select UserID from BookUser Where UserName = @ UserName)
RETURN 0
ELSE
Begin
Insert into BookUser (UserName, Title, Guid, BirthDate, Description, Photo, Other)
VALUES (@ UserName, @ Title, @ Guid, @ BirthDate, @ Description, @ Photo, @ Other)
SET @ UserID = @ IDENTITY
RETURN 1
End
GO
JSP code:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
</Head>
<Body>
<%