import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import entity.User;public class UserDao extends ConnectionManager{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql=null;
int result=0; public int getUser(User user)
{
sql="select userId from userinfo where loginname=? and password=?";
try {
conn=getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getLoginName());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
if(rs.next())
{
result=rs.getInt("userId");
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
}
return result;
}
}import java.beans.Statement;public class User {
private int userId;
private String loginName;
private String username;
private String password;
private String password1;
private String password2;
public User() { }
public User(int userId, String loginName,String username, String password,String password1,String password2) {
super();
this.userId = userId;
this.loginName = loginName;
this.username = username;
this.password = password;
this.password1 = password1;
this.password2 = password2;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getpassword1() {
return password;
}
public void setpassword1(String password1) {
this.password1 = password1;
}
public String getpassword2() {
return password;
}
public void setpassword2(String password2) {
this.password2 = password2;
}

}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import entity.User;
public class UserregisterDao {
private static final long serialVersionUID = 1L;
Connection con=null;   
    Statement sta=null;   
    ResultSet rs=null; 
    PreparedStatement pstmt = null;
    String sql="SELECT Schoolmate.* FROM Schoolmate WHERE Schoolmate.KL_Name NOT IN (SELECT KL_Name FROM Universitymate)";
    String GET_ONE_SQL = "SELECT SchoolSystem.* FROM SchoolSystem WHERE userType=? AND userName=? AND password=?";
    public UserregisterDao()
{
try
{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
catch(ClassNotFoundException e)
        {
            e.printStackTrace();   
            System.out.println("驱动未找到");
        }
        try
        {
            con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databasename=JavaWebDB","zwd","123456");
            if(!con.isClosed())
            {
                System.out.println("连接成功!");//显示是否连接成功
            }     
        }
        catch(SQLException e)
        {
            e.printStackTrace();   
            System.out.println("数据库未能连接!");
        }
}
    public User Register(String username ,String password1,String password2) throws SQLException
    {
     sql="select userId from userinfo where username=? and password1=? and password2=?";
     User user=new User();
     try{
     String Message ="INSERT INTO master.* FROM WHERE username='"+username+"' AND password1='"+password1 +"'AND password2='"+password2+"'";
     System.out.print(Message);
     sta=con.createStatement();
            rs=sta.executeQuery(Message);
     if(rs.next())
     {
     user.setusername(rs.getString(1));
     user.setpassword1(rs.getString(2));
     user.setpassword2(rs.getString(3));
     System.out.println("^^"+rs.getString(1)+rs.getString(2)+rs.getString(3));
     }
     rs.close();
     sta.close();    
     con.close();
     }catch(SQLException se)
     {
     System.out.println("出错!");
     return null;
     }
     finally
     {
     try{
     con.close();
     }
     catch(SQLException se)
         {}
     }
return user;
    }}import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.UserDao;
import dao.UserregisterDao;
import entity.User;
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {        
        request.setCharacterEncoding("UTF-8");        
String username =request.getParameter ("username");
String password1 =request.getParameter ("password1");
String password2 =request.getParameter ("password2");
UserDao userDao = new UserDao();
UserregisterDao userregisterDao = new UserregisterDao();
User user =new User();
user.setusername(username);
user.setpassword1(password1);
user.setpassword2(password2);
int result = userDao.getUser(user);
HttpSession session = request.getSession(true);
if(result>0){
Object count = session.getAttribute("count");
int counter = 0;
if (count == null) {
counter = 1;
session.setAttribute("count", new Integer(1));
} else {
counter = Integer.valueOf(session.getAttribute("count")
.toString());
counter++;
session.setAttribute("count", counter);
}
response.sendRedirect("pages/registerRF.jsp");
} else {
try
{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    user.setusername(request.getParameter("username"));
    user.setpassword1(request.getParameter("password1"));
    user.setpassword2(request.getParameter("password2"));
    User registerresult=userregisterDao.Register(username,password1,password2);
 }
catch(Exception e){
log("Error");
}
response.sendRedirect("pages/registerRT.jsp");
}
}

}
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<head>
<title>注册提示</title>
</head>
<body>
<font color="#9999FF" size="5">
<b>注册失败!</b> 
</font><br>
<a href="login.jsp">返回</a>
</body><%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<head>
<title>注册提示</title>
</head>
<body>
<font color="#9999FF" size="5">
<b>注册成功!</b> 
</font><br>
<a href="books.jsp">进入书店</a>
</body>