大家帮我看看这小段代码那里错了啊?????????

zhang80718 2006-04-10 11:31:56
public class SqlBean {
public static synchronized Connection getConnection() throws Exception{

try{
Class.forName(DB_DRIVER).newInstance(); //常量定义过了
String url =DB_CONNSTRE;
String username = "sa";
String password = "sa";
Connection con = DriverManager.getConnection(url,username,password);
return con;
}catch(SQLException e){
System.out.println(e);
throw e;
}
}

public void executeInsert(String sql)
{
try
{
Statement stmt = this.getConnection().createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.out.print("执行插入有错误" + ex.getMessage());
}
}
}


Statement stmt = this.getConnection().createStatement(); 这提示我“未处理的异常类型Exception”
...全文
313 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
smart886 2006-04-10
  • 打赏
  • 举报
回复
Statement stmt = this.getConnection().createStatement();
改成
Connection con = getConnection();
Statement stmt = con().createStatement();
试试
可能是那个this多了,不用this.getConnection().createStatement();
直接Statement stmt = getConnection().createStatement();
wsk_228 2006-04-10
  • 打赏
  • 举报
回复
楼上正解,如果在不清楚是什么异常的学以致情况下,建意还是用Exception来捕获异常
chouchy 2006-04-10
  • 打赏
  • 举报
回复

catch(SQLException ex)
{
System.out.print("执行插入有错误" + ex.getMessage());
}
后继续
catch(Exception e)
{
}
interpb 2006-04-10
  • 打赏
  • 举报
回复
Class.forName(DB_DRIVER).newInstance(); 这个方法会抛出ClassNotFoundException


interpb 2006-04-10
  • 打赏
  • 举报
回复
public static synchronized Connection getConnection() throws Exception{

try{
Class.forName(DB_DRIVER).newInstance(); //常量定义过了
String url =DB_CONNSTRE;
String username = "sa";
String password = "sa";
Connection con = DriverManager.getConnection(url,username,password);
return con;
}catch(SQLException e){
System.out.println(e);
throw e;
} catch(ClassNotFoundException ce) {
///
}

}

81,092

社区成员

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

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