一个将注册信息输入到数据库的jsp
<%
@page contentType="text/html;charset=GB2312" %>
<%@page import="java.sql.*" %>
<%
String driverName="com.mysql.jdbc.Driver";
String dbName="student";
String userName="root";
String userPasswd="123456";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Connection connection=null;
PreparedStatement pstmt=null;
String sql=null;
ResultSet rs=null;
%>
<html>
<body>
<center>
<h1>北京物资学院留言板</h1>
<hr>
<br>
<%
//设置标志,判断是否出错,true表示出错,false表示正常
boolean isError=false;
//保存错误信息
String Str_Error="";
request.setCharacterEncoding("GB2312");
String Username=request.getParameter("username");
String Password1=request.getParameter("pwd1");
String pwd=request.getParameter("pwd2");
String TrueName=request.getParameter("truename");
if("".equals(Username) || "".equals(Password1) || "".equals(TrueName))
{
//输入信息不完整,设置错误信息
Str_Error="您输入的信息不完整";
}
else if(!Password1.equals(pwd))
{
//两次输入的密码不相同,设置错误信息
Str_Error="您两次输入的密码不同,请重新输入!";
}
else
{
//当两次输入的密码相同时,判断用户名是否存在、
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
string sql="Insert INTO member (username,password1,true_name,mem_status) VALUES (?,?,?,?)";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
String sql="Select * FROM member Where username=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,Username);
rs=pstmt.executeQuery();
if(rs.next())
{
//用户名已存在,设置错误信息
Str_Error="您的用户名已注册";
}
rs.close();
pstmt.close();
conn.close();
}
catch(Exception e){}
//当用户名正常时,判断昵称是否存在
if(!"您的用户名已注册".equals(Str_Error))
{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
String sql="Select * FROM member Where true_name=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,TrueName);
rs=pstmt.executeQuery();
if(rs.next())
{
//昵称已存在,设置错误信息
Str_Error="您的昵称已注册";
}
rs.close();
pstmt.close();
conn.close();
}
catch(Exception e){}
}
if(!"".equals(Str_Error))
{
//有错误信息,提示返回
response.setHeader("refresh","3;URL=register.jsp");
%>
<h3>对不起,<%=Str_Error %>,请重新输入!</h3>
3秒后自动返回注册页面<br>
如果没有跳转,请点<a href="register.jsp">这里</a>
<%
}
else
{
//没有错误,将注册信息存入数据库
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
String sql="Insert INTO member (username,password1,true_name,mem_status) VALUES (?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,Username);
pstmt.setString(2,Password1);
pstmt.setString(3,TrueName);
pstmt.setString(4,"member");
pstmt.executeUpdate();
pstmt.close();
connection.close();
}
catch(Exception e)
{
isError=true;
System.out.print(e);
}
if(isError)
{
%>
<h3>注册失败!</h3>
<%
}
else
{
%>
<h3>注册成功!</h3>
<% 【就是这里显示报错】
}
response.setHeader("refresh","3;URL=addStudent.html");
%>
3秒后自动返回首页<br>
如果没有跳转,请点<a href="addStudent.html">这里</a>
</center>
</body>
</html>