tomcat连接oracle数据库问题,急~~~~

04-12-17 chain
文件名是contest.jsp

<%

DataSource ds = null;

try{

Context ctx = new InitialContext();

if(ctx == null) {

throw new Exception("访问数据库失败,初始化环境失败");

}

ds = (DataSource)ctx.lookup("java:comp/env/jdbc/OracleDB");

} catch (NamingException nex) {

throw new Exception("访问数据库失败,初始化环境失败:" + nex.toString());

}

Connection conn = null;

PreparedStatement stmtData = null;

ResultSet rsData = null;

out.println("connect to DB");

try {

conn = ds.getConnection();

String strSql = " select id,name from test ";

rsData = stmtData.executeQuery(strSql);

if(rsData.next()){

out.println(rsData.getString(1));

out.println(rsData.getString(2));

}

stmtData.close(); stmtData = null;

rsData.close(); rsData = null;

conn.close(); conn = null;

} catch (SQLException ex) {

throw new Exception("数据库更新用户资料错误:" + ex.toString());

} finally {

if (stmtData != null) {

try{ stmtData.close(); } catch(SQLException e){ ; }

stmtData = null;

}

if (conn != null) {

try{ conn.setAutoCommit(true); conn.close(); } catch(SQLException e) { ; }

conn = null;

}

}

%>

测试后数据库可以连接,但是只要运行到 rsData = stmtData.executeQuery(strSql);这句就出问题,下面是给出的错误:

exception

org.apache.jasper.JasperException

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)

................

............

root cause[/b}

java.lang.NullPointerException

at org.apache.jsp.contest_jsp._jspService(contest_jsp.java:82)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)

.......

...........

.......

请教高手指点,哪里出错了,很急~~~谢谢了!!!!

wzbbuaa
2004-12-17 14:26
我没有看错的话你的那个 PreparedStatement stmtData = null;

smstData为空当然不能查了.

需要,smstData=con.getPreparedStatement();

才可以吧!

猜你喜欢