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;
}
}
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;
}
}
得到的ct是空的 你需要检查ConnDB().getConn();
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;
}
}