注册验证java代码[针对上篇文章]
程序员文章站
2022-03-06 23:37:35
复制代码 代码如下:<%@ page contenttype="text/html; charset=gb2312" language="java"%> <...
复制代码 代码如下:
<%@ 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>