主题:有关于数据库异常捕获的问题
我在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中则无法通过编译?望各位高手指点。谢谢!!!
<%
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中则无法通过编译?望各位高手指点。谢谢!!!