数据库连接池连接出问题了。。

sawen21 2006-10-23 07:31:29
在tomcat中的配置绝对是正确的,而且我单独写了个test.jsp测试能访问到数据库,
但我多写了个类封装数据库访问就出问题了。。返回的connection=null;
请问这是怎么回事,代码如下:
package beans;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class DatabaseBean {

public static synchronized Connection getConnection(){
DataSource ds=null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
return ds.getConnection();
}catch(NamingException ex){
ex.printStackTrace(System.err);
return null;
}
catch(SQLException e){
e.printStackTrace(System.err);
return null;
}
}
public static void close(ResultSet rs,Statement stat,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(SQLException ex){
ex.printStackTrace(System.err);
}
}
if(stat!=null){
try{
stat.close();
}catch(SQLException ex){
ex.printStackTrace(System.err);
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace(System.err);
}
}
}
public static synchronized int getMaxID(Connection conn,String tablename){
int nMaxID=0;
try{
Statement stat=conn.createStatement();
String sql="Select maxid from tableseq where tablename='"+tablename+"'";
ResultSet rs=stat.executeQuery(sql);
while(rs.next()){
nMaxID=rs.getInt(1);
}
rs.close();
if(nMaxID>0){
nMaxID++;
sql="update tableseq set maxid='"+nMaxID+"' where tablename='"+tablename+"'";
}else{
nMaxID=1;
sql="insert into tableseq values('"+tablename+"','"+nMaxID+"')";
}
stat.executeUpdate(sql);
stat.close();
return nMaxID;
}catch(SQLException ex){
ex.printStackTrace(System.err);
return -1;
}
}
}


test.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="BIG5"%>
<%@ page import="beans.DatabaseBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

<%
Connection conn=DatabaseBean.getConnection();
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery("select * from tableseq");
while(rs.next()){
out.print("tablename="+ rs.getString("tablename"));
out.print("<br>");
out.print("maxid="+ rs.getInt("maxid"));
DatabaseBean.close(rs,stat,conn);
}
if(conn==null) out.print("can't connect to db!");
%>
</body>
</html>

问题:
org.apache.jasper.JasperException: Exception in JSP: /myjsp.jsp:31

28:
29: <%
30: Connection conn=DatabaseBean.getConnection();
31: Statement stat=conn.createStatement();
32: ResultSet rs=stat.executeQuery("select * from tableseq");
33: while(rs.next()){
34: out.print("tablename="+ rs.getString("tablename"));


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
org.apache.jsp.myjsp_jsp._jspService(myjsp_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


...全文
278 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
GemScorpio 2006-10-24
  • 打赏
  • 举报
回复
public static synchronized Connection getConnection(){
DataSource ds=null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
return ds.getConnection();
}catch(NamingException ex){
ex.printStackTrace(System.err);
return null;
}
catch(SQLException e){
e.printStackTrace(System.err);
return null;
}
}
这个方法改一下
public static synchronized Connection getConnection(){
DataSource ds=null;
try{
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
ds = (DataSource)envCtx.lookup("jdbc/test");
return ds.getConnection();
}catch(NamingException ex){
ex.printStackTrace(System.err);
return null;
}
catch(SQLException e){
e.printStackTrace(System.err);
return null;
}
}
sawen21 2006-10-23
  • 打赏
  • 举报
回复
没人来理会一下么。。。
不会这么无情吧。。

81,092

社区成员

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

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