回 帖 发 新 帖 刷新版面

主题:有关于数据库异常捕获的问题

我在JSP文件中直接用
<%
Connection con=null;
ResultSet rs=null;
Statement st=null;
String i="1";
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};Dbq=e:\\project\\database\\book.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery("select count(*) from book_class");
if (rs.next())
  i=rs.getString(1);
}catch (Exception e)
{
out.print(e.getMessage());
}finally
{
rs.close();
st.close();
con.close();
}
out.println(i);
%>
程序运行正常。但是我在JBean中使用
public String getI()
{
  String i="1";
  try
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection(url,"","");
      st=con.createStatementResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
       rs=st.executeQuery("select count(*) from book_class");
       if (rs.next())
           i=rs.getString(1);
     }catch (Exception e)
     {
        System.out.print(e.getMessage());
      }finally
      {
        rs.close();
        st.close();
        con.close();
      }
return i;
}
不能通过编译。其提示错误:
E:\project\bookclass.java:42: unreported exception java.sql.SQLException; must be caught or declared to be thrown
                rs.close();
                                        ^
E:\project\bookclass.java:44: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            st.close();
                                ^
E:\project\bookclass.java:45: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            con.close();
                                 ^
3 errors
为什么会出现在JSP页面中直接使用可以,但在JBean中则无法通过编译?望各位高手指点。谢谢!!!

回复列表 (共1个回复)

沙发

其实在JSP中可以完全不用抛出异常的

我来回复

您尚未登录,请登录后再回复。点此登录或注册