非常基础的问题,没办法从SQL中取数据

user2003 2008-07-03 05:46:36

从一个JSP 的LOGIN页面输入用户名和密码
但是始终没有办法验证成功
LOGIN的ACTION是到 login_confirm,具体的数据库连接在sqlBean中
附上这两个文件,请高手帮我看看问题出在哪里?谢谢了/





login_confirm.java

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");
HttpSession session=req.getSession(true);
session.setAttribute("id",String.valueOf(id));
String password=null;
password= req.getParameter("password");

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

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

if(kind.equals("student")) {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/student.jsp");
rd.forward(req, res);}
if(kind.equals("teacher")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/teacher.jsp");
rd.forward(req, res);}
if(kind.equals("admin")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(req, res);}
}




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

try{
ResultSet rs=db.executeQuery(sql);
if(rs.next() ){
pw=rs.getString("password");
System.out.println("password");
}

}
catch(Exception e)
{ System.out.print(e.toString());}
//System.out.println(pw);
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("/login.jsp");
rd.forward(req, res);
} }

}



sqlBean.java

import java.io.*;
import java.sql.*;

public class sqlBean{///


public java.sql.Connection conn = null;
public ResultSet rs=null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "ClassDB";
private final String userName = "sa";
private final String password = "";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";

// Constructor
public sqlBean(){}

private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";

}

private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(conn!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return conn;
}




public int executeInsert(String sql){
int num=0;
try{
conn = this.getConnection();
Statement stmt=conn.createStatement( );
num=stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行插入有错误:"+ex.getMessage() );
System.out.print("执行插入有错误:"+ex.getMessage());//输出到客户端
}

CloseDataBase();
return num;
}
// display data

public ResultSet executeQuery(String sql){
rs=null;
try{
conn = this.getConnection();
Statement stmt=conn.createStatement( );
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("执行查询有错误:"+ex.getMessage() );
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端
}

return rs;
}
// delete data
public int executeDelete(String sql){
int num=0;
try{

conn = this.getConnection();
Statement stmt=conn.createStatement( );
num=stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行删除有错误:"+ex.getMessage() );
System.out.print("执行删除有错误:"+ex.getMessage()); //输出到客户端
}
CloseDataBase();
return num;
}
//////////////////
public void CloseDataBase(){
try{
conn.close();
}
catch(Exception end){
System.err.println("执行关闭Connection对象有错误:"+end.getMessage( ) );
System.out.print("执行执行关闭Connection对象有错误:有错误:"+end.getMessage()); //输出到客户端
}
}
}




...全文
80 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangshizhu79 2008-07-04
  • 打赏
  • 举报
回复
你把后台的输出贴出来看看啊
limon758 2008-07-04
  • 打赏
  • 举报
回复
用断点看看取到值没有
user2003 2008-07-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cxyy_ 的回复:]
如果数据库连接正确的话,用IDE工具的DEBUG模式跟踪下,主要看下从数据库取出的用户名和密码,如果是你想要的结果,再看下录入的字符是否有多余的空格。
[/Quote]


不是

user2003 2008-07-04
  • 打赏
  • 举报
回复
找到原因了
String sql="select password from "+kind+" where id='"+id+"'";

这里应该是NAME='"+id+"'";
真是郁闷,太粗心了
user2003 2008-07-04
  • 打赏
  • 举报
回复
后台没有输出啊。TOMCAT里没有输出任何错误提示的。
我奇怪的是
if(rs.next() ){
pw=rs.getString("password");
System.out.println("password");
}


这个怎么没有输出,是不是说根本就没有运行到这里过。
cxyy_ 2008-07-03
  • 打赏
  • 举报
回复
如果数据库连接正确的话,用IDE工具的DEBUG模式跟踪下,主要看下从数据库取出的用户名和密码,如果是你想要的结果,再看下录入的字符是否有多余的空格。
sheen7758 2008-07-03
  • 打赏
  • 举报
回复
if (true)
就找sqlbean的错误吧。
else
检查web.xml的配置了。
sheen7758 2008-07-03
  • 打赏
  • 举报
回复
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

// 这里输出一下,看看有没有进入这个servlet
system.out.println("loginValidate");


String message=null;
String id=null;
id=req.getParameter("id");
HttpSession session=req.getSession(true);
session.setAttribute("id",String.valueOf(id));
String password=null;
password= req.getParameter("password");

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

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧