编译不能通过,求助!

doctorzw 2004-10-24 11:51:31
下面是我编写的一个bean,主要功能是从连接池中取一个connection
tomcat5服务器
连接池配置绝对没有问题,已经测试通过
我在编译的时候报错
程序如下:

package DBConnectionPool;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class ConPool
{
private DataSource ds=null;

public ConPool()
{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");
}

public Connection getCon()
{
return ds.getConnection();
}

public void deleteCon(Connection con)
{
con.close();
}
}


错误信息:
--------------------Configuration: <Default>--------------------
D:\Java\Tomcat\jakarta-tomcat-5.0.16\webapps\ROOT\ConPool.java:13: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
InitialContext ctx=new InitialContext();
^
D:\Java\Tomcat\jakarta-tomcat-5.0.16\webapps\ROOT\ConPool.java:14: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");
^
D:\Java\Tomcat\jakarta-tomcat-5.0.16\webapps\ROOT\ConPool.java:19: unreported exception java.sql.SQLException; must be caught or declared to be thrown
return ds.getConnection();
^
D:\Java\Tomcat\jakarta-tomcat-5.0.16\webapps\ROOT\ConPool.java:24: unreported exception java.sql.SQLException; must be caught or declared to be thrown
con.close();
^
4 errors

这是为什么啊

...全文
89 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zouyiyy 2004-10-25
  • 打赏
  • 举报
回复
InitialContext ctx=new InitialContext();改为 Context ctx = new InitialContext()
tom2005 2004-10-25
  • 打赏
  • 举报
回复
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");
}
catch(javax.naming.NamingException e){}

try{
return ds.getConnection();
}
catch(java.sql.SQLException e){}
mlovex 2004-10-25
  • 打赏
  • 举报
回复
在程序中加入try catch
或者在方法外抛出异常
spiritsl 2004-10-25
  • 打赏
  • 举报
回复
unreported exception javax.naming.NamingException; must be caught or declared to be thrown
unreported exception java.sql.SQLException; must be caught or declared to be thrown

程序将抛出上面两个异常,
你应该把你的代码放在try {} catch{}中。
liuyonghailiuyong 2004-10-25
  • 打赏
  • 举报
回复
放到try里,捕获异常!
Sasser_roy 2004-10-25
  • 打赏
  • 举报
回复
"must be caught or declared to be thrown"!!!
plxyz2000 2004-10-25
  • 打赏
  • 举报
回复
将代码放到try{}中,catch(){}捕捉异常
wandou999 2004-10-25
  • 打赏
  • 举报
回复
是的,没捕捉异常

81,091

社区成员

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

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