unreported exception java.sql.SQLException; must be caught or declared to be thrown

zhyx21century 2006-01-04 08:47:47
private static void sendSms(DBConnection conn, int lngEmpID, String strMessage) {
String sql = "SELECT STRMOVETEL FROM EMPBASEINFO WHERE (LNGEMPID = " + lngEmpID + ")";
String phone = DBUtil.getString(conn, sql);
if (phone != null && phone.startsWith("13")) {

Connection conn_orcl =null;
javax.sql.DataSource ds = null;
ResultSet rs = null;
Statement stm =null;

try {
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
ds = (javax.sql.DataSource) ctx.lookup("jdbc/swoa2.0");
conn_orcl = ds.getConnection();
stm = conn_orcl.createStatement();
String sql_orcl ="insert into sms_tosend(msgid, sendtime,f_flag, phone,msgtext,sendflag) VALUES(SELL.NEXTVAL,SYSDATE-3,'0','"+phone+"','"+strMessage+"','111')";
System.out.println(sql_orcl);
stm.executeUpdate(sql_orcl);
}
catch (Exception e){
System.out.println(e.getMessage());
}
finally
{
//就是这几行代码报错
/*if(rs!=null) rs.close();
if(stm!=null) stm.close();
if(conn_orcl!=null) conn_orcl.close();*/
}
}
}

C:\IBM\WebSphere\AppServer\installedApps\zhangyx\cdyc_war.ear\cdyc.war\WEB-INF\c
lasses>javac swoa\common\Message.java
swoa\common\Message.java:91: unreported exception java.sql.SQLException; must be
caught or declared to be thrown
if(rs!=null) rs.close();
^
swoa\common\Message.java:92: unreported exception java.sql.SQLException; must be
caught or declared to be thrown
if(stm!=null) stm.close();
^
swoa\common\Message.java:93: unreported exception java.sql.SQLException; must be
caught or declared to be thrown
if(conn_orcl!=null) conn_orcl.close();

^
3 errors

什么原因阿???
...全文
586 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mark_MaoHUA 2006-01-05
  • 打赏
  • 举报
回复
楼上正解,写程序要养成好习惯
  • 打赏
  • 举报
回复
必须捕捉异常,或者方法抛出异常。
giant216 2006-01-05
  • 打赏
  • 举报
回复
支持楼上
yuzl32 2006-01-05
  • 打赏
  • 举报
回复
简单改写函数
private static void sendSms(DBConnection conn, int lngEmpID, String strMessage) throws SQLException // 声明可能将抛出异常
{
... ...

... ..
}
j2me_home 2006-01-05
  • 打赏
  • 举报
回复
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
public static void main(String args[]) {

}

private static void sendSms(DBConnection conn, int lngEmpID,
String strMessage) throws SQLException {
String sql = "SELECT STRMOVETEL FROM EMPBASEINFO WHERE (LNGEMPID = "
+ lngEmpID + ")";
String phone = DBUtil.getString(conn, sql);
if (phone != null && phone.startsWith("13")) {

Connection conn_orcl = null;
javax.sql.DataSource ds = null;
ResultSet rs = null;
Statement stm = null;

try {
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
ds = (javax.sql.DataSource) ctx.lookup("jdbc/swoa2.0");
conn_orcl = ds.getConnection();
stm = conn_orcl.createStatement();
String sql_orcl = "insert into sms_tosend(msgid, sendtime,f_flag, phone,msgtext,sendflag) VALUES(SELL.NEXTVAL,SYSDATE-3,'0','"
+ phone + "','" + strMessage + "','111')";
System.out.println(sql_orcl);
stm.executeUpdate(sql_orcl);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (rs != null)
rs.close();
if (stm != null)
stm.close();
if (conn_orcl != null)
conn_orcl.close();
}
}
}
}
kevinmartin 2006-01-05
  • 打赏
  • 举报
回复
unreported exception java.sql.SQLException; must be caught or declared to be thrown

从你发表的题目里面就能看出问题所在,不知道你是不是一点英语都不懂呢?
我给你翻译一下吧。

未报告的异常:java.sql.SQLException, (这个异常)必须被捕获或者声明抛出。

他的意思就是说,你在代码中使用了sql的一些代码,这些代码必须使用try-catch捕获SQLException异常。如果你不捕获的话,就必须在函数声明的后面加上:throws SQLException,否则程序会认为你没有考虑这里可能出现的错误情况,不予编译通过。

由此可以得出结论:
1、下次再出这种问题的时候多注意看错误信息。
2、好好学习英语。
3、好好学习java,增加经验。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧