import java.sql.*;
public class Test072301{ 
//public void ListStudents(){  //同样的错误!
public void ListStudents() throws SQLException{
  try{
int i, NoOfColumns;
String StNo, StFName, StLName;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Ex1Con = DriverManager.getConnection("jdbc:odbc:StudentDB","admin","sa");
Statement Ex1Stmt = Ex1Con.createStatement();
ResultSet Ex1rs = Ex1Stmt.executeQuery("SELECT StudentNumber,FirstName,LastName From Students");
System.out.println("Student Number   First Name   Last Name");
while(Ex1rs.next()){
StNo = Ex1rs.getString(1);
StFName = Ex1rs.getString(2);
StLName = Ex1rs.getString(3);
System.out.print(StNo);
}
}
}
catch(Exception ex){}

解决方案 »

  1.   

    you must thrown ClassNotFoundException;
    or add try ...catch;
      

  2.   

    访问数据库时的异常没有捕获
    catch它就ok
      

  3.   

    可以说说
    public void ListStudents()  

    public void ListStudents() throws SQLException的区别吗?
      

  4.   

    if you throws SQLException
      you may not catch SQLException, the catch work is trun to function caller work
    else
      you must catch SQLException
      

  5.   

    我调了一下。
    如果是public void ListStudents()  
    那么同 schwarzenegger所写的就可以了。如果是public void ListStudents() throws SQLException
    需要在使用这个函数的地方test.ListStudents();进行异常捕获。那位dd可以说一下这是为什么啊?或者java就是这么定义这两个区别的?
      

  6.   

    问题不是这里
    抛出了异常就一定要捕获 不捕获会报编译期错
    public void ListStudents()  

    public void ListStudents() throws SQLException//用这个方法时 你必须捕获抛出的异常
    Class.forName等这些方法都是有异常抛出的 你得捕获异常 否则报错
      

  7.   

    to schwarzenegger:
    如你所说,必须对数据库访问的function进行异常捕获,对吗?
    那除了对数据库的访问外,还有那些是必须要异常捕获的?
      

  8.   

    to shoulder2001(马儿) 
    我明白了,谢谢。
    你可以说说还有那些方法是异常抛出的吗?======================================================十分种后结帖。
    谢谢两位!!