package JDBC;import java.sql.*;public class DBO {
public static void register(){
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.print("success");
}
catch (ClassNotFoundException e) {
e.printStackTrace();

}
public static Connection getCon() {
Connection con = null;
try {
//Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root","root");
System.out.print("success");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
} public static Statement getstatement(Connection con) {
Statement sta = null;
try {
if(con!=null)
sta = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return sta;
}
   public static PreparedStatement getprepared(Connection con,String sql){
   PreparedStatement pre =null;
   try{
   if(con!=null)
   pre = con.prepareStatement(sql);
   }catch (SQLException e) {
e.printStackTrace();
}
   return pre;
   }
/*public static ResultSet getResult(Statement sta, String sql) {
ResultSet rs = null;

try {
if(sta!=null)
rs = sta.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace(); 
}
return rs;
}*/
   public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt!= null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
@SuppressWarnings("finally")
public static boolean executeUpdate(PreparedStatement ps) {
int n = 0;
try {
if(ps!= null) {
 n = ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
if(n!=0)
return true;
else
return false;


}
public static void close(Connection con, Statement sta) {
try {
if (sta != null) {
sta.close();
sta = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
} }
public static void main(String ags0[]){
DBO.register();
DBO.getCon();
}
}
/*****************************************************************************************************/
package Model;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import JDBC.DBO;
public class Enterprise {
private String enterId;
private String enterName;
private String enterPassword;
private String address;
private String enterTel;
private int isPassed; public String getEnterId() {
return enterId;
} public void setEnterId(String enterId) {
this.enterId = enterId;
} public String getEnterName() {
return enterName;
} public void setEnterName(String enterName) {
this.enterName = enterName;
} public String getEnterPassword() {
return enterPassword;
} public void setEnterPassword(String enterPassword) {
this.enterPassword = enterPassword;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getEnterTel() {
return enterTel;
} public void setEnterTel(String enterTel) {
this.enterTel = enterTel;
} public int getIsPassed() {
return isPassed;
} public void setIsPassed(int isPassed) {
this.isPassed = isPassed;
}
public static List<Enterprise> getUsers() {//得到所有未审核用户
List<Enterprise> enters = new ArrayList<Enterprise>();
Connection conn = DBO.getCon();
String sql = "select * from enterprise where isPassed = 0";
Statement stmt = DBO.getstatement(conn);
ResultSet rs = DBO.getResultSet(stmt, sql);
try {
while (rs.next()) {
Enterprise u = new Enterprise();
    u.setEnterId(rs.getString(1));
    u.setEnterName(rs.getString(2));
    u.setEnterPassword(rs.getString(3));
    u.setAddress(rs.getString(4));
    u.setEnterTel(rs.getString(5));
    u.setIsPassed(rs.getInt(6));
enters.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
            DBO.close(rs);
            DBO.close(conn, stmt);
}
return enters;
}
public static int getUsers(List<Enterprise> enters, int pageNo, int pageSize) { int totalRecords = -1; Connection con = DBO.getCon();
String sql = "select * from enterprise limit " + (pageNo - 1) * pageSize
+ "," + pageSize;
Statement stmt = DBO.getstatement(con);
ResultSet rs = DBO.getResultSet(stmt, sql); Statement stmtCount = DBO.getstatement(con);
ResultSet rsCount = DBO.getResultSet(stmtCount,
"select count(*) from enterprise"); try {
rsCount.next();
totalRecords = rsCount.getInt(1); while (rs.next()) {
Enterprise u = new Enterprise();
    u.setEnterId(rs.getString(1));
    u.setEnterName(rs.getString(2));
    u.setEnterPassword(rs.getString(3));
    u.setAddress(rs.getString(4));
    u.setEnterTel(rs.getString(5));
    u.setIsPassed(rs.getInt(6));
enters.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
    DBO.close(rs);
            DBO.close(con, stmt);
} return totalRecords;
}
public static void main(String args[]){
List<Enterprise> enters = getUsers();
System.out.print(enters.size());
}
}
/*******************************************************************************/
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBC.DBO.getCon(DBO.java:19)
at Model.Enterprise.getUsers(Enterprise.java:67)
at Model.Enterprise.main(Enterprise.java:128)
Exception in thread "main" java.lang.NullPointerException
at Model.Enterprise.getUsers(Enterprise.java:72)
at Model.Enterprise.main(Enterprise.java:128)
高手给看看 怎么回事啊   急死了  ···············第一个测试连接成功呢  为什么第二个就报异常了

解决方案 »

  1.   

    没有mysql的jdbc驱动吧?到mysql网站上去下载JDBC的JAR文件,放入lib目录或任意目录,在classpath中设置.
      

  2.   

    //Class.forName("com.mysql.jdbc.Driver");
    本来这句就是注册驱动的,你都把这句代码给注释掉了。连接不同的数据库,就需要不同的 驱动包;
    你去下载个mysql的驱动包;然后在你连接数据库的Java类头部 import 进来。就OK了。
      

  3.   

    //Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root","root");这里正确了不,要加载驱动撒
      

  4.   

    No suitable driver
    意思是没合适的驱动 少驱动啊//Class.forName("com.mysql.jdbc.Driver");
    不该注释掉的