|
Posted on 2009-12-13 15:58 Gavin.lee 阅读(289) 评论(0) 编辑 收藏 所属分类: JDBC
下面是一哥们的文章,出处: http://dev.yesky.com/154/7530154.shtml
我姑且不看他的内容,主要看下做法:
一下意思就是插入一条数据,无则插入,有则失败:
创建表:
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" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</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 e) {
out.print(e.getLocalizedMessage());
}finally {
try {
if(cmd != null) {
cmd.close();
cmd = null;
} if(cn != null) {
cn.close();
cn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
用了这么久,才知道原来这么基础的我都还没搞清楚,惭愧啊,多谢这位仁兄了··
|