一、新建一个数据库TestDemo
然后新建一个名字为User的数据表(UName,UPass)
新建一个javaweb工程
将网上下载的sqljdbc4.ja包放到webroot/web-inf/lib目录下面
编辑register.jsp:
<%@page import="java.lang.*, java.io.*, java.sql.*, java.util.*" contentType="text/html;charset=gb2312"%>
<%
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>
<title>用户注册</title>
</head>
<body>
<form action="insert.jsp" method="post">
用户名:<input type="text" name="name" /><br>
密码: <input type="password" name="pass" /><br>
<input type="submit" name="submit" value="注册" />
</form>
</body>
</html>
编辑insert.jsp:
<%@page import="java.lang.*, java.io.*, java.sql.*, java.util.*" contentType="text/html;charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
<%
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager
.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TestDemo",
"sa", "qs@123456");
PreparedStatement pstate = conn.prepareStatement("insert into [User](UName,UPass) values(?,?)");
pstate.setString(1,request.getParameter("name"));
pstate.setString(2,request.getParameter("pass"));
int result = pstate.executeUpdate();
if (result == 1) {
out.println("用户注册成功");
} else {
out.println("用户注册失败");
}
} catch (SQLException e) {
out.println(e);
}
%>
</body>
</html>
运行:
结果:
到数据库中查看:
其实,无聊玩玩,没什么实际意义,不过也算为日后可能会使用j2ee跟sqlserver打交道做些代码准备,也有可能不会用到,不过无他~~玩下而已,学编程序就应该用这种玩下的心态。···