实例解析Java/JSP中调用SQL Server存储过程_JSP教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:如何在Struts 2中实现文件上传前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题, 所以今天我们就来讨论一下这个问题。 实现原理 Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑
最近做了个Java的小项目(第一次写Java的项目哦), 到网上搜索了半天,找到了一个比较好点的调用存储过程的例子,而且网上普遍采用的都是setXXX((int parameterIndex, XXX x)的形式。这种形式感觉不是很直观,下面就发布一个完整的采用setXXX(String parameterName, XXX x)的编写方法。创建数据表,存储过程的代码都完整发布。
创建表:
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
[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
@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>
<%
//注意:下面的连接方法采用最新的SQL Server的JDBC,
//请到 http://msdn2.microsoft.com/zh-cn/data/aa937724.aspx 下载
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
{
cn = DriverManager.getConnection(url);
cmd = cn.prepareCall(sql);
java.util.UUID Guid = java.util.UUID.randomUUID();
String FilePath = application.getRealPath("") + "\test\logo.gif";
java.io.FileInputStream f = new java.io.FileInputStream(FilePath);
Date rightNow = Date.valueOf("2007-9-9");
cmd.setString("UserName","mengxianhui"); //注意修改这里,存储过程验证了UserName的唯一性。
cmd.setString("Title","孟宪会");
cmd.setString("Guid",Guid.toString());
cmd.setString("BirthDate","2007-9-9");
cmd.setDate("BirthDate",rightNow);
cmd.setString("Description","【孟子E章】");
cmd.setBinaryStream("Photo",f,f.available());
cmd.setString("Other",null);
cmd.registerOutParameter(1,java.sql.Types.INTEGER);
cmd.registerOutParameter("UserID",java.sql.Types.INTEGER);
cmd.execute();
int returnValue = cmd.getInt(1);
int UserID = cmd.getInt("UserID");
if(returnValue == 1)
{
out.print("<li>添加成功!");
out.print("<li>UserID = " + UserID);
out.print("<li>returnValue = " + returnValue);
}
else
{
out.print("<li>添加失败!");
}
f.close();
}
catch(Exception ex)
{
out.print(ex.getLocalizedMessage());
}
finally
{
try
{
if(cmd != null)
{
cmd.close();
cmd = null;
}
if(cn != null)
{
cn.close();
cn = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
%>
</body>
</html>
<%@ 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,
//请到 http://msdn2.microsoft.com/zh-cn/data/aa937724.aspx 下载
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
{
cn = DriverManager.getConnection(url);
cmd = cn.prepareCall(sql);
java.util.UUID Guid = java.util.UUID.randomUUID();
String FilePath = application.getRealPath("") + "\test\logo.gif";
java.io.FileInputStream f = new java.io.FileInputStream(FilePath);
Date rightNow = Date.valueOf("2007-9-9");
cmd.setString("UserName","mengxianhui"); //注意修改这里,存储过程验证了UserName的唯一性。
cmd.setString("Title","孟宪会");
cmd.setString("Guid",Guid.toString());
cmd.setString("BirthDate","2007-9-9");
cmd.setDate("BirthDate",rightNow);
cmd.setString("Description","【孟子E章】");
cmd.setBinaryStream("Photo",f,f.available());
cmd.setString("Other",null);
cmd.registerOutParameter(1,java.sql.Types.INTEGER);
cmd.registerOutParameter("UserID",java.sql.Types.INTEGER);
cmd.execute();
int returnValue = cmd.getInt(1);
int UserID = cmd.getInt("UserID");
if(returnValue == 1)
{
out.print("<li>添加成功!");
out.print("<li>UserID = " + UserID);
out.print("<li>returnValue = " + returnValue);
}
else
{
out.print("<li>添加失败!");
}
f.close();
}
catch(Exception ex)
{
out.print(ex.getLocalizedMessage());
}
finally
{
try
{
if(cmd != null)
{
cmd.close();
cmd = null;
}
if(cn != null)
{
cn.close();
cn = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
%>
</body>
</html>
分享:FCKeditor编辑器在JAVA中的使用与配置方法工作前的准备 版本是FCKeditor_2.4.2.zip【组件的主文件包】,FCKeditor.Java 2.3.zip【这里面提供了标签库和图片上传的jar】。这些都可以在http://www.fckeditor.net 网站下载到! 配置 1. 新建工程compass,在WebRoot文件夹下新建文件夹FCKeditor(用来存
相关JSP教程:
- jsp response.sendRedirect不跳转的原因分析及解决
- JSP指令元素(page指令/include指令/taglib指令)复习整理
- JSP脚本元素和注释复习总结示例
- JSP FusionCharts Free显示图表 具体实现
- 网页模板:关于jsp页面使用jstl的异常分析
- JSP页面中文传递参数使用escape编码
- 基于jsp:included的使用与jsp:param乱码的解决方法
- Java Web项目中连接Access数据库的配置方法
- JDBC连接Access数据库的几种方式介绍
- 网站图片路径的问题:绝对路径/虚拟路径
- (jsp/html)网页上嵌入播放器(常用播放器代码整理)
- jsp下显示中文文件名及绝对路径下的图片解决方法
- 相关链接:
- 教程说明:
JSP教程-实例解析Java/JSP中调用SQL Server存储过程。