JSP调用SQL Server存储过程的实例:
创建表:
CREATE TABLE [BookUser] (
CONSTRAINT [DF_BookUser_Other] DEFAULT ('默认值'),
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
创建存储过程:
CREATE PROCEDURE InsertUser
@UserName varchar(50),
@Title varchar(255),
@Guid
@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代码:
<%@ 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>
<%
//注意:下面的连接方法采用SQL
Server的JDBC,先下载sqlserver驱动。
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String
url="jdbc:sqlserver://localhost:1433;databaseName=Book;user=sa;password=";
String sql = "{? = call InsertUser(?,?,?,?,?,?,?,?)}";
Connection cn = null;
CallableStatement cmd = null;
try
{
}
catch(Exception ex)
{
}
finally
{
}
%>
</body>
</html>
插入表情