读书人

JSP+JavaBean+SQL Server,用户登陆,密

发布时间: 2012-01-21 21:31:43 作者: rapoo

JSP+JavaBean+SQL Server,用户登陆,密码错误
JSP+JavaBean+SQL Server,用户登陆,密码错误,这个问题捆饶我很久了,QQ:82689774,高手请赐教!急!!急!排除了密码中含有空格的可能!
连接SQL数据库,sqlBean.java
package hou;

import java.sql.*;
public class sqlBean{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=educationi ";

String user= "sa ";
String password= "1 ";
Connection conn= null;
Statement stmt = null;
ResultSet rs = null;
public sqlBean()
{
try
{Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println( "sqlBean(): " + e.getMessage());
}
}

/////////////////插入操作
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{System.err.println( "sqlBean.executeUpdate: "+ex.getMessage());
}
}

//////////////////////查询操作
public ResultSet executeQuery(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println( "sqlBean.executeQuery: "+ex.getMessage());
}
return rs;
}

/////////////////////////更新操作
public void executeUpdate(String sql)
{
try {
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println( "sqlBean.executeQuery: " + ex.getMessage());
}
}

//执行删除操作
public void executeDelete(String sql)
{
try
{conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println( "sqlBean.executeDelete: "+ex.getMessage());
}
}

//关闭操作
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();


}
catch(SQLException e){
e.printStackTrace();
}
}
}
密码查询校验代码login_confirm.java
package hou;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class login_confirm extends HttpServlet{


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String message=null;
String id=null;
id=req.getParameter( "id ");
id=id.trim();
HttpSession session=req.getSession(true);
session.setAttribute( "id ",String.valueOf(id));
String password=null;
password=req.getParameter( "password ");
password= password.trim();


String temp =getPassword(req,res,id);
if( password.equals(temp))
goo(req,res);
else {
message= "用户名或密码有误! ";
doError(req,res,message);
}
}



public void goo(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{

RequestDispatcher rd = getServletContext().getRequestDispatcher( "/teacher.jsp ");

}




public String getPassword(HttpServletRequest req, HttpServletResponse res,
String id)
throws ServletException, IOException {
sqlBean db= new sqlBean();
String pw= " ";
String sql= "select password from member where id= ' "+id+ " ' ";
try{
ResultSet rs=db.executeQuery(sql);

pw=rs.getString( "password ");
pw=pw.trim();
}

catch(Exception e)


{ System.out.print(e.toString());}
return pw;
}



public void doError(HttpServletRequest req,
HttpServletResponse res,
String str)
throws ServletException, IOException {

req.setAttribute( "problem ", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher( "/errorpage.jsp ");
rd.forward(req, res);
}


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String action = action = req.getParameter( "action ");
if ( "logout ".equalsIgnoreCase(action)) {
HttpSession session=req.getSession(true);
session.invalidate();
RequestDispatcher rd = getServletContext().getRequestDispatcher( "/jiemian.jsp ");
rd.forward(req, res);
} }

}




[解决办法]
把password和从数据库取到的密码打印到控制台看看,password和密码的长度也一并打印出来
[解决办法]
在接受数据后把两边的空格去掉,问题是由数据库中的字段数据类型引起的。
[解决办法]
实在不行的话,如果就是为了登陆那就把你的SQL改下,
把密码也作为检索条件加上,查询出的结果有数据那就
是登陆成功,没有数据那就是登陆失败了~~
String sql= "select password from member where id= ' "+id+ " ' and password= ' "+password+ " ' ";

读书人网 >Java Web开发

热点推荐