這是我嘗試使用jdk進行編譯的程序。在程序中的「satya」引用msaccess數據庫文件的數據庫。當我嘗試編譯它顯示錯誤時"MyClass.java:0:error:unreported exception ClassNotFoundException;must be caught or declared to be thrown"
。即使我將程序中的異常從SQLException
更改爲異常,它也可以成功編譯。但運行程序時拋出異常。如何執行?未報告的異常ClassNotFoundException;必須被捕獲或聲明爲拋出
import java.sql.*;
class MyClass
{
public static void main(String args[])
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:satya","","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from studentinfo");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getString(3)+"\t");
}
rs.close();
st.close();
}
catch (SQLException e) {
System.out.println("<P>" + "There was an error doing the query:");
System.out.println ("<PRE>" + e + "</PRE> \n <P>");
}
}
}