这是我尝试使用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>");
}
}
}