package cn.hlxk.com.userDaoImp;import java.sql.*;import cn.hlxk.com.user.DB.DBConn;
import cn.hlxk.com.user.vo.User;
import cn.hlxk.com.userDao.UserDao;public class UserDaoImp implements UserDao {

private static final String isLoginSQL="select count(*) from customer where name=?"; public Boolean isLogin(User user) {
// TODO Auto-generated method stub
Boolean isLogin=false;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;

try{

conn=new DBConn().getConnection();
ps=conn.prepareStatement(this.isLoginSQL);

ps.setString(1, user.getName());

rs=ps.executeQuery();

if(rs.next()){

int i=rs.getInt(1);
System.out.println(i);
if(i==0){

isLogin=true;
}
}



}catch (Exception e) {
// TODO: handle exception

//throw new RuntimeException(e.getMessage(),e);
}finally{

close(conn, ps, rs);
}


return isLogin;
}
private void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {

rs.close();

} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());

}
try {

ps.close();

} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());

}
try {

conn.close();

} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());

}
}
}

解决方案 »

  1.   


    就是我去获取数据库里的数据字段  name,数据库也有数据,可为何获取到的结果都为o
      

  2.   

     if(rs.next()){
                    
                    int i=rs.getInt(1);
                    System.out.println(i);
                    if(i==0){
                        
                        isLogin=true;
                    }我看呀,吧if(rs.next())改成while(rs.next())应该能解决
      

  3.   

    大哥,能用,但是出现一个情况,就是什么账号都可以登录啦。还是select count(*)……获取的应该是一条数据才取呀。  if(i==0)代表什么意思了?是不存在此数据,还是有返回值了?
      

  4.   

    程序无错。执行也没报异常,返回结果0,说明就是0SQL查询中如果用=比较,请特别注意字段值中含空格的问题。可改成like方案。
      

  5.   

    返回结果是0,是代表没有查询到数据库中的数据呀?改为like方案,就是模糊查询了,是吧,那样就没有意义了呀?因为我做的是一个登录,账号应该就是唯一的咯。你说的特别注意字段值中含空格的问题,具体是什么问题了?
      

  6.   

    if(rs.next()){
        
      int i=rs.getInt(1);
      System.out.println(i);
      if(i==0){
        
      isLogin=true;
      }我看呀,吧if(rs.next())改成while(rs.next())应该能解决这个应该可以吧
      

  7.   

    是的,没有符合你的条件的记录。你可以先将where条件去掉执行试试看。有时候数据库会在你存入的字段值后追加空格以保证数据长度。
      

  8.   


    或者 打印 user.getName() 的值看看
      

  9.   

    select count(*) .....jdbc得到的东西是int 不是Integer
    int的0不是Integer的 null收工
      

  10.   

    用赋值查询吧 .....这样好点....或者rs.getString("你的字段名");  试试
      

  11.   

    3L的也可以啊 只要能进入 while()方法,说明就有数据,那么就代表登录过了,isLogin = true .
      

  12.   

    package cn.hlxk.com.Servlet;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import cn.hlxk.com.user.vo.User;
    import cn.hlxk.com.userDaoImp.UserDaoImp;public class MyServlet extends HttpServlet implements Servlet { /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {


    } /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    doPost(request, response);

    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    //设置编码以及接收参数

    request.setCharacterEncoding("GBK");
    String name=request.getParameter("name");
    String pass=request.getParameter("pass");




    User us=new User();

    us.setName("name");
    us.setPass("pass");


    boolean  login=new UserDaoImp().isLogin(us);
    System.out.println(login);
    if(new UserDaoImp().isLogin(us)){

    response.sendRedirect("loginSuc.html");

    }else {

    response.sendRedirect("loginFail.html");
    }

         
    } /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
    // Put your code here
    }}
    应该是这个有问题
      

  13.   

    private static final String isLoginSQL="select count(*) from customer where name=?";这个SQL语句好像和下面的不对路子吧 
    改成
    select * from customer where name=? and password=? 
    你那都没判断密码肯定是所有账号都能登录的 
      if(rs.next()){
                    
                    int i=rs.getInt(1);
                    System.out.println(i);
                    if(i==0){
                        
                        isLogin=true;
                    }
    这里可以改成if(rs.getRows()!= 0)
    {
         return true;
    }
    前提是你要确保name字段里没有重复的
      

  14.   

    select * from customer where name=? and password=?  
    不是把所有的数据都查询出来了吗?那样比较浪费服务器的资源哦