程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP中調用SQL Server存儲過程實例講解

JSP中調用SQL Server存儲過程實例講解

編輯:關於JSP

    下面介紹一個JSP調用SQL Server存儲過程的實例:

    創建表:

    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 ('默認值'),
        CONSTRAINT [PK_BookUser] PRIMARY KEY  CLUSTERED
        (
            [UserID]
        )  ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    創建存儲過程:

    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代碼:

    <%@ 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>
    <%
     

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved