https://www.jb51.net/article/19801.htm
复制代码 代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用户注册</title>
</head>
<body>
<%
String strSql="";
String database_username="root";
String database_password="";
String url="jdbc:mysql://localhost/ch3?useUnicode=true&characterEncoding=gb2312";
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection dbConn= DriverManager.getConnection(url,database_username,database_password);
Statement stmt=dbConn.createStatement();
ResultSet rs=null;
String s_UserName = request.getParameter("UserName");
strSql = "select * from users where UserName like '"+s_UserName+"'";
rs = stmt.executeQuery(strSql);
if(rs.next())
{
out.println("<h1 align=center>用户名<font color=red>"+s_UserName+"</font>已经存在!</h1>");
out.println("<div align=center><input type=button name=btn value=返回 onClick='window.history.go(-1)'></div>");
}
else
{
String s_UserPassword = request.getParameter("UserPassword");
String s_NickName = request.getParameter("NickName");
String s_Sex = request.getParameter("Sex");
String s_Email = request.getParameter("Email");
strSql = "insert into users (UserName,UserPassword,NickName,Sex,Email) values('"+s_UserName+"','"+s_UserPassword+"','"+s_NickName+"','"+s_Sex+"','"+s_Email+"')";
try
{
stmt.executeUpdate(strSql);
out.println("<h1 align=center>用户注册成功,您的注册信息如下</h1>");
out.println("<p align=center>用户名:<font color=blue>"+s_UserName+"</font></p>");
out.println("<p align=center>密码:<font color=blue>"+s_UserPassword+"</font></p>");
out.println("<p align=center>昵称:<font color=blue>"+s_NickName+"</font></p>");
if(s_Sex.equals("0"))
{
out.println("<p align=center>性别:<font color=blue>男</font></p>");
}
else
{
out.println("<p align=center>性别:<font color=blue>女</font></p>");
}
out.println("<p align=center>Email:<font color=blue>"+s_Email+"</font></p>");
}
catch(Exception ex)
{
out.println("<h1 align=center>用户注册失败</h1>");
out.println("<div align=center><input type=button name=btn1 value=返回 onClick='window.history.go(-1)'></div>");
}
}
rs.close();
stmt.close();
dbConn.close();
}
catch(Exception ex)
{
out.println("<h1 align=center>数据库连接错误</h1>");
out.println("<div align=center><input type=button name=btn2 value=返回 onClick='window.history.go(-1)'></div>");
}
%>
</body>
</html>
复制代码 代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用户注册</title>
</head>
<body>
<%
String strSql="";
String database_username="root";
String database_password="";
String url="jdbc:mysql://localhost/ch3?useUnicode=true&characterEncoding=gb2312";
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection dbConn= DriverManager.getConnection(url,database_username,database_password);
Statement stmt=dbConn.createStatement();
ResultSet rs=null;
String s_UserName = request.getParameter("UserName");
strSql = "select * from users where UserName like '"+s_UserName+"'";
rs = stmt.executeQuery(strSql);
if(rs.next())
{
out.println("<h1 align=center>用户名<font color=red>"+s_UserName+"</font>已经存在!</h1>");
out.println("<div align=center><input type=button name=btn value=返回 onClick='window.history.go(-1)'></div>");
}
else
{
String s_UserPassword = request.getParameter("UserPassword");
String s_NickName = request.getParameter("NickName");
String s_Sex = request.getParameter("Sex");
String s_Email = request.getParameter("Email");
strSql = "insert into users (UserName,UserPassword,NickName,Sex,Email) values('"+s_UserName+"','"+s_UserPassword+"','"+s_NickName+"','"+s_Sex+"','"+s_Email+"')";
try
{
stmt.executeUpdate(strSql);
out.println("<h1 align=center>用户注册成功,您的注册信息如下</h1>");
out.println("<p align=center>用户名:<font color=blue>"+s_UserName+"</font></p>");
out.println("<p align=center>密码:<font color=blue>"+s_UserPassword+"</font></p>");
out.println("<p align=center>昵称:<font color=blue>"+s_NickName+"</font></p>");
if(s_Sex.equals("0"))
{
out.println("<p align=center>性别:<font color=blue>男</font></p>");
}
else
{
out.println("<p align=center>性别:<font color=blue>女</font></p>");
}
out.println("<p align=center>Email:<font color=blue>"+s_Email+"</font></p>");
}
catch(Exception ex)
{
out.println("<h1 align=center>用户注册失败</h1>");
out.println("<div align=center><input type=button name=btn1 value=返回 onClick='window.history.go(-1)'></div>");
}
}
rs.close();
stmt.close();
dbConn.close();
}
catch(Exception ex)
{
out.println("<h1 align=center>数据库连接错误</h1>");
out.println("<div align=center><input type=button name=btn2 value=返回 onClick='window.history.go(-1)'></div>");
}
%>
</body>
</html>