编译不能通过,求助!

doctorzw 2004-10-24 11:34:14
下面是我编写的一个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

这是为什么啊
...全文
67 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
trumplet 2004-10-26
  • 打赏
  • 举报
回复
这个问题发了几个贴呀?

try {
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");
return ds.getConnection();
con.close();
}
catch (Exception e) {
//异常处理
}
onelifeonelove 2004-10-25
  • 打赏
  • 举报
回复
"unreported exception javax.naming.NamingException"
一看就知道没有做扑或异常处理
icy_csdn 2004-10-25
  • 打赏
  • 举报
回复
分别catch
javax.naming.NamingException
java.sql.SQLException

nanjia 2004-10-25
  • 打赏
  • 举报
回复
没有Exception的处理

ConPool throws Excetion{


}
相关推荐
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2004-10-24 11:34
社区公告
暂无公告