初入程序界,以下代码不知道哪儿有错
小弟刚刚加入学习java,参考教程编写了一个登陆模块的代码,具体实现是表单提交——servlet接收——调用以下DBConnectionDAO处理,其中的StudentDAO是一个实例类,工程运行正常,但是最后的结果本来我是要显示登陆的帐号密码的,却是不能显示,返回的是false(请看下下边的servlet代码),仔细看了几遍代码,没有发现那里有错,所以只好来请教各位前辈,我的代码到底是哪儿错了,对了,数据插入是正常的....但是读数据却不知道错在哪儿!
数据处理代码:
package dao;
import java.sql.*;
import dao.StudentDAO;
public class DBConnectionDAO {
/**
* @param args
*/
public StudentDAO FindStudent(String name,String password) {
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
}catch(
ClassNotFoundException e){
}
Connection coon=null;
// Statement stmt=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
try{
String url="jdbc:derby://localhost:1527/myeclipse";
String user="classiccars";
String psw="classiccars";
String sql="select * from Student2 where name=? and password=?";
coon=java.sql.DriverManager.getConnection(url, user, psw);
// stmt=coon.createStatement();
// stmt.executeUpdate("insert into student2(name,password,age)values('张淋','19861108',23)");
pstmt=coon.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
rs=pstmt.executeQuery();
if(rs!=null&&rs.next()){
StudentDAO stu=new StudentDAO();
stu.setname(rs.getString("name"));
stu.setPassword(rs.getString("password"));
stu.setAge(rs.getInt(3));
return stu;
}
}catch(Exception e){
}
finally{
try{
rs.close();
pstmt.close();
// stmt.close();
coon.close();
}catch(Exception e){
}
}
return null;
}
}
以下为servlet代码:
package servlet;
import
java.io.IOException;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.*;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws
ServletException,
IOException {
StudentDAO stu = new DBConnectionDAO().FindStudent(
request.getParameter("name"),request.getParameter("password")
);
if(stu!= null&&
stu.getname().equals(request.getParameter("name"))&&stu.getPassword().equals(request.getParameter("password"))) {
request.setAttribute("message", "Successful"); request.getSession().setAttribute("stu", stu);
} else {
request.setAttribute("message", "False");
}
request.getRequestDispatcher("/result.jsp").forward(request,response);
}
}
------解决方案--------------------}catch(Exception e){
}
这里要把错误信息显示出来
}catch(Exception e){
e.printStackTrace();//把这句加进去,再运行看看吧.估计是有异常了.
}
------解决方案--------------------呵呵
你的判断有意义么??
你"StudentDAO stu = new DBConnectionDAO().FindStudent(
request.getParameter("name"),request.getParameter("password")
); "已经把要判断的参数传进去了,还判断它干嘛?
对了,你在把request.getParameter("name")和request.getParameter("password")
输出看看有没有得到