我建的数据库名字叫book, 有两张表 books 和users
语句里并没有写user 而且怎么会出现 book.user这种奇怪的报错
下面是用户登录注册对数据库的操作,报错行已加粗:
public class UserDao {
private Connection conn;
//用户注册
public boolean registerUser(User user) throws Exception{
conn = DBCon.getConnection();
String sql = "insert into users (name,password)values(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
try{
ps.setString(1, user.getName());
ps.setString(2, user.getPwd());
ps.executeUpdate();
conn.commit();
return true;
}catch(SQLException e){
conn.rollback();//数据回滚,有一条数据插入失败所有的都不插入,数据库回滚到操作之前的样子
e.printStackTrace();
}finally{
conn.close();
}
return false;
}
//登录
public boolean login(User user) throws Exception{
conn = DBCon.getConnection();
String sql1 ="select * from users where name='"+user.getName()+"' and password = '"+user.getPwd()+"'";
PreparedStatement ps = conn.prepareStatement(sql1);
ResultSet rs = ps.executeQuery();
if(rs.next()){
System.out.print("----"+rs);
return true;
}
return false;
}
下面是login的servlet,同样有报错行:
public class ServletLogin extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String forward = "";
User user = new User();
user.setName(username);
user.setPwd(pwd);
if(username.equals("")&&username!=null&&pwd.equals("")&&pwd!=null){
forward="error.jsp";
}else
if(username.equals("admin")&&username!=null&&pwd.equals("admin")&&pwd!=null){
BookDao bookdao = new BookDao();
List list = bookdao.findBooks();
request.setAttribute("booklist", list);
forward = "menu.jsp";
}else{
UserDao userdao = new UserDao();
try {
userdao.login(user);
session.setAttribute("username", username);
BookDao bookdao = new BookDao();
List list = bookdao.findBooks();
request.setAttribute("booklist", list);
forward = "menu.jsp";
forward="userhome.jsp";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
RequestDispatcher rd = request.getRequestDispatcher(forward);
rd.forward(request, response);
}
}
麻烦大家看看!!!