帮忙:配置JSP时数据库连接池的编译JAVABEAN发生错误
配置JSP时数据库连接池的编译JAVABEAN发生错误
错误提示如下:
D:\server\javadoc>javac jndiconn.java
jndiconn.java:18: unreported exception javax.naming.NamingException; must be cau
ght or declared to be thrown
initCtx = new InitialContext();
^
jndiconn.java:19: unreported exception javax.naming.NamingException; must be cau
ght or declared to be thrown
ctx = (Context) initCtx.lookup("java:comp/env");
^
jndiconn.java:20: unreported exception javax.naming.NamingException; must be cau
ght or declared to be thrown
obj = (Object) ctx.lookup(jndiName);
^
3 errors
代码如下:
package web;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class jndiconn
{
static String jndiName="TestDB";
private Context initCtx = null;
private Context ctx = null;
private Object obj = null;
private DataSource ds = null;
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public jndiconn()
{
try {
initCtx = new InitialContext();
ctx = (Context) initCtx.lookup("java:comp/env");
obj = (Object) ctx.lookup(jndiName);
ds = (DataSource)obj;
}
catch (java.lang.SecurityException se)
{
throw se;
}
}
}
帮忙,我该怎么配置和修改代码?
问题点数:20、回复次数:4Top
1 楼minisun2000(红色枫叶)回复于 2005-07-08 12:58:59 得分 10
错误提示多明显啊,javax.naming.NamingException异常没捕获!Top
2 楼chuxinfo(binbin)回复于 2005-07-08 13:17:57 得分 0
那我怎么做?Top
3 楼chenghu1982(㊣太空堡垒)回复于 2005-07-08 13:26:41 得分 10
明显就是throw exception错误嘛
try {
cont = new InitialContext(); //初始化环境对象
}
catch (NamingException ex) {
return 80002;
}
if (cont == null) { //如果Context对象为空(没有匹配的环境),返回错误代码
return 80003;
}
try {
ds = (DataSource) cont.lookup( //初始化DataSource对象
"java:comp/env/jdbc/connectDB");
}
catch (NamingException ex1) { //如果出错返回错误码
return 80004;
}
if (ds == null) { //如果DataSource对象为空(没有匹配的数据库),返回错误代码
return 80005;
}
try {
conn = ds.getConnection(); //由DataSource对象初始化Connection对象
}
catch (SQLException ex2) { //如果初始化出错返回错误码
return 80006;
}
try {
stmt = conn.createStatement(ResultSet.
TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY); //得到Statement对象
}
catch (SQLException ex3) { //如果初始化出错返回错误码
return 80007;
}
return 0;Top
4 楼pkhone(不要理我)回复于 2005-07-08 13:27:55 得分 0
我倒,这么明显的异常啊~~
无语~~~Top




