为什么我的finally不能通过编译?
我写了一段简单的代码来检验一下java中的try-catch-finally语句,可是为什么我的代码不能通过编译呢?
代码如下:(请高手指点)
import java.io.*;
import java.sql.*;
public class Data
{
public static void main(String args[])
{
Connection conn=null;
Statement stat=null;
ResultSet set=null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
String url ="jdbc:mysql://localhost:3306/work?
user=root&password=&useUnicode=true&
characterEncoding=GB2312";
conn= DriverManager.getConnection(url);
stat=conn.createStatement();
set=stat.executeQuery("select * from company ");
while (set.next())
{
System.out.println(set.getString(1));
}
}
catch(SQLException ee)
{
ee.printStackTrace();
}
finally
{
set.close();
stat.close();
conn.close();
}
}
}
为什么我的这段代码在编译的时候总是提示下面的错误信息:
---------- Compile Java Program ----------
Data.java:35: unreported exception java.sql.SQLException; must be caught or declared to be thrown
set.close();
^
Data.java:36: unreported exception java.sql.SQLException; must be caught or declared to be thrown
stat.close();
^
Data.java:37: unreported exception java.sql.SQLException; must be caught or declared to be thrown
conn.close();
^
3 errors
请问是什么原因?