package com.sp.modle;import java.sql.*;
import java.util.*;public class UserBeanCl {

private Statement sm = null;
private ResultSet rs = null;
private Connection ct = null;
private int pageSize=5;
private int rowCount=0;
private int pageCount=0;

    
public int getPageCount(){

try {

ct= new ConnDB().getConn();
sm = ct.createStatement();

rs = sm.executeQuery("select count(*) from userss");
if (rs.next()) { rowCount = rs.getInt(1); }

if (rowCount % pageSize == 0) { pageCount = rowCount / pageSize;
} else { pageCount = rowCount / pageSize + 1;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}  finally{
this.Close();
}
             return pageCount;
} public ArrayList getUserByPage(int pageNow){

ArrayList al =new ArrayList();

try {

ct=new ConnDB().getConn();

    sm = ct.createStatement();
    

    
rs=sm.executeQuery("select top "+pageSize+" *from userss where userId not in (select "+pageSize*(pageNow-1)+" userId from userss )");

    
    while(rs.next()){
    
     UserBean ub = new UserBean();
     ub.setUserId(rs.getInt(1));
     ub.setUsername(rs.getString(2));
     ub.setPasswd(rs.getString(3));
     ub.setEmail(rs.getString(4));
     ub.setGrate(rs.getInt(5));    
     al.add(ub);
    
    }
    
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{

this.Close();
}
return al;
}    public void Close(){
    
     try{

if(rs!=null){
rs.close();
rs=null;
}

if(sm!=null){
sm.close();
sm=null;
}

if(ct!=null){
ct.close();
ct=null;
}
} catch (Exception e2) {

e2.printStackTrace();
}
   }    public boolean checkUser(String u,String p){
     
     boolean b = false;
   try {
ct= new ConnDB().getConn();
sm = ct.createStatement();//抛出异常的地方
 
  rs = sm.executeQuery("select passwd from userss where username = '" +u+ "'");  if (rs.next()) { 
  if (rs.getString(1).equals(p)) { 
  b=true;
  }
  }
  } catch (Exception e) {
 
  e.printStackTrace();
  }finally{
 
 
         this.Close();
  }
  return b;
    }


}

解决方案 »

  1.   

    ct= new ConnDB().getConn();
    得到的ct是空的  你需要检查ConnDB().getConn();
      

  2.   

    ct得到的connection为空,连接数据库有问题,去看下有没有把数据库的驱动JAR导进去,还要加载驱动类
      

  3.   

    在上面的几个类中也同样引用了ct,可是并没有报出异常啊。ConnDB在另外一个页面
    package com.sp.modle;
    import java.sql.*;
    public class ConnDB { private Connection ct = null;
    public Connection getConn(){


    try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        
              ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=master","sa","");

    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }
    return ct;
    }

    }