非常基础的问题,没办法从SQL中取数据
从一个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()); //输出到客户端
}
}
}