南宁志愿者官网电话:求助:MYSQL与JSP做的用户登录与注册问题
来源:百度文库 编辑:查人人中国名人网 时间:2024/05/05 21:54:44
我用MYSQL与JSP做了一个用户登录与注册,现在遇到问题向各位求助,注册后的只有在数据库中排第一的能登录,但是把第一个删了,第二个也能登录,主是什么问题呢?
代码如下:check_login.jsp
<%@ include file="db.jsp" %>
<%@ page session="true" buffer="16kb" import="java.util.*,java.text.*"%>
<%@ page contentType="text/html; charset=gb2312" %>
<%
if (rs.next()) {
// Setup variables
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
if ((username.equals(rs.getString("username"))) && (passwd.equals(rs.getString("passwd")))) {
// Write cookies
response.sendRedirect("./myroom/inf.jsp");
}else{
response.sendRedirect("login_error.jsp");
}
}
%>
db.jsp
<%@ page import="java.sql.*"%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch (Exception ex){
out.println(ex.toString());
}
String xDb_Conn_Str = "jdbc:mysql://127.0.0.1:3306/room";
Connection conn = null;
try{
conn = DriverManager.getConnection(xDb_Conn_Str,"root","123");
}catch (SQLException ex){
out.println(ex.toString());
}
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
%>
代码如下:check_login.jsp
<%@ include file="db.jsp" %>
<%@ page session="true" buffer="16kb" import="java.util.*,java.text.*"%>
<%@ page contentType="text/html; charset=gb2312" %>
<%
if (rs.next()) {
// Setup variables
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
if ((username.equals(rs.getString("username"))) && (passwd.equals(rs.getString("passwd")))) {
// Write cookies
response.sendRedirect("./myroom/inf.jsp");
}else{
response.sendRedirect("login_error.jsp");
}
}
%>
db.jsp
<%@ page import="java.sql.*"%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch (Exception ex){
out.println(ex.toString());
}
String xDb_Conn_Str = "jdbc:mysql://127.0.0.1:3306/room";
Connection conn = null;
try{
conn = DriverManager.getConnection(xDb_Conn_Str,"root","123");
}catch (SQLException ex){
out.println(ex.toString());
}
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
%>
最后三条代码,应该是:
Statement stmt = conn.createStatement();
String sql = "select * from userinfo where username='"+username+"' and password='"+password+"'";
ResultSet rs = stmt.executeQuery(sql);
当然,要在你的类里边加入构造来定义这两个变量