[bean]两个bean,报异常。含LOG,大家给看看。谢谢
两个bean,报异常。已经追踪到异常产生的地方,可是想不通为什么关闭结果集(“OK ... 002”后面)和连接(“OK ... 004”后面)会产生空指针异常。含LOG,大家给指点指点。谢谢。Bridge_Pooled_1.java,作为JSP到数据库操作bean之间的连接,向数据库操作bean传递操作,向JSP传递处理过的操作结果。
package test;
import java.lang.*;
import java.sql.*;
//import jsp_db_oprt.*;
/**
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;
*/
public class Bridge_Pooled_1
{
//>>Put out.
private static int stat=0;
private static String text="";
private static String strOUT="";
private static Exception excp_rtn=null;
//<<Put out.
public static int bridge(String conffile,String strIN)
{
stat=0;
text="";
strOUT="";
excp_rtn=null;
String string_1="[Start public static int bridge()]\n <Version: 2004-10-19-03>\n -- Skip over EXCP if it is NULL.\n -- Set NULL after useing.\n -- Use TFW-Standard-Interface.\n -- Provide TFW-Standard-Interface.";
String string_2="[Ended public static int bridge()]";
System.out.println(string_1);
String sql = strIN;
sql=strIN;
//>>Call DB_operator
int rs_stat=0;
String rs_text="";
ResultSet rs=null;
Exception rs_excp=null;
rs_stat=DB_operator_Pooled_1.db_operate(conffile,sql);
rs_text=DB_operator_Pooled_1.notice();
rs=DB_operator_Pooled_1.result();
rs_excp=DB_operator_Pooled_1.exception();
//<<Call DB_operator
int numbCols=0;
String nameCols="";
String l_s="<tr>";//line_start
String l_e="</tr>";//line_ended
String c_s="<td>";//colum_start
String c_e="</td>";//colum_ended
String s1="";
String s2="";
int i1=0;
int i2=0;
ResultSetMetaData rsmd=null;
if(stat==0)
{
try
{
rsmd=rs.getMetaData();
numbCols=rsmd.getColumnCount();
i1=1;s2=l_s;
while(i1<=numbCols)
{
s1=rsmd.getColumnName(i1);
s2=s2+c_s+s1+c_e;
i1++;
}
s2=s2+l_e;
while(rs.next())
{
i1=1;
s2=s2+l_s;
while(i1<=numbCols)
{
s1=rs.getString(i1);
s2=s2+c_s+s1+c_e;
i1++;
}
s2=s2+l_e;
}
s1="<table>"+s2+"</table>";
strOUT=s1;
}
catch(Exception ne)
{
stat=1;
excp_rtn=ne;
}
}
//>>Shut down DB_operator
i1=DB_operator_Pooled_1.close();
if(i1!=0)
{
stat=i1;
excp_rtn=DB_operator_Pooled_1.exception();
}
//>>Shut down DB_operator
try
{
rsmd=null;
rs.close();rs=null;
}
catch(Exception e_other)
{
stat=1;
excp_rtn=e_other;
}
System.out.println(string_2);
return stat;
}
public static String result()
{
String string_1="[Start public static String result()]";
String string_2="[Ended public static String result()]";
System.out.println(string_1);
System.out.println(string_2);
return strOUT;
}
public static String notice()
{
String string_1="[Start public static String notice()]";
String string_2="[Ended public static String notice()]";
System.out.println(string_1);
if(stat==0)
{
text="Every thing goes on well.";
}
else if(stat==2)
{
text="Exception while input or output.";
}
else if(stat==3)
{
text="Exception caused by no class or no file found.";
}
else if(stat==4)
{
text="Exception with SQL.";
}
else
{
text="Exception somewhere.";
}
System.out.println(string_2);
return text;
}
public static Exception exception()
{
String string_1="[Start public Exception exception()]";
String string_2="[Ended public Exception exception()]";
System.out.println(string_1);
System.out.println("Exception caught: "+excp_rtn);
System.out.println(string_2);
return excp_rtn;
}
}
DB_operator_Pooled_1.java,执行数据库操作,返回结果集。
/**
Instruction:
As a bean of JSP, do the SQL operation && return the "ResultSet". Uses && provides TFW-Starndard-Interface.
return 0: Every thing goes on well.
return 1: Exception somewhere.
return 2: Exception while input or output.
return 3: Exception caused by no class or no file found.
return 4: Exception with SQL.
New version modified much, still need to do futher test.
介绍:
一个执行SQL操作并返回“结果集”的bean。使用和提供TFW标准接口。
返回0: 一切正常。
返回1:有地方异常。
返回2: 有输入输出异常。
返回3: 有文件或类未找到的异常。
返回4: SQL操作异常。
新版本改动很大,还需要进一步测试。
Guide:
"db_operate(String conffile,String sql)".
package AA.BB...NN;Use this while package in pack "AA.BB...NN". Attention, the pack must be placed in one of the "CLASSPATH"s.
指引:
“db_operate(String conffile,String sql)”。
package AA.BB...NN;
如果要打到 “AA.BB...NN” 包里,就添加这些。注意,包必须放置在其中一处“CLASSPATH”下。
*/
package test;
import java.lang.*;
import java.io.*;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import tfw.db_deal.core.*;
public class DB_operator_Pooled_1
{
//>>Put out.
private static int stat=0;
private static String text="";
private static ResultSet rs=null;
private static Exception excp_rtn=null;
//<<Put out.
private static Context initCtx=null;
private static Context envCtx=null;
private static DataSource ds=null;
private static Connection conn=null;
private static Statement stmt=null;
private static ResultSetMetaData rsmd=null;
public static int db_operate(String conffile,String sql)
{
stat=0;
text="";
excp_rtn=null;
String string_1="[Start public static void db_operate()*P]\n <Version: 2004-10-19-04>\n -- Skip over the EXCP if it is NULL.\n -- Use TFW-Standard-Interface.\n -- Provide TFW-Standard-Interface.\n -- Need to close \"rs\".\n -- Trying Connection-Pool.";
String string_2="[Ended public static void db_operate()*P]";
System.out.println(string_1);
char LG='E';
int GUI=0;
int conn_stat=0;
String conn_text="";
Connection conn=null;
Exception conn_excp=null;
int rs_stat=0;
String rs_text="";
rs=null;
Exception rs_excp=null;
try
{
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/TestPool");
if(ds!=null)
{
conn=ds.getConnection();
if(conn!=null)
{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
}
}
catch(SQLException esql_1)
{
stat=2;
excp_rtn=esql_1;
}
catch(Exception e1)
{
stat=1;
excp_rtn=e1;
}
System.out.println(string_2);
return stat;
}
public static String notice()
{
String string_1="[Start public static String notice()]";
String string_2="[Ended public static String notice()]";
System.out.println(string_1);
if(stat==0)
{
text="Every thing goes on well.";
}
else if(stat==2)
{
text="Exception while input or output.";
}
else if(stat==3)
{
text="Exception caused by no class or no file found.";
}
else if(stat==4)
{
text="Exception with SQL.";
}
else
{
text="Exception somewhere.";
}
System.out.println(string_2);
return text;
}
public static ResultSet result()
{
String string_1="[Start public static ResultSet result()]";
String string_2="[Ended public static ResultSet result()]";
System.out.println(string_1);
System.out.println(string_2);
return rs;
}
public static Exception exception()
{
String string_1="[Start public static Exception exception()]";
String string_2="[Ended public static Exception exception()]";
System.out.println(string_1);
System.out.println("Exception caught: "+excp_rtn);
System.out.println(string_2);
return excp_rtn;
}
public static int close()
{
String string_1="[Start public static int close()]";
String string_2="[Ended public static int close()]";
System.out.println(string_1);
System.out.println("001 ... OK");
try
{
System.out.println("002 ... OK");
/*rs.close();*/rs=null;
System.out.println("003 ... OK");
stmt.close();stmt=null;
System.out.println("004 ... OK");
conn.close();conn=null;
System.out.println("005 ... OK");
ds=null;
System.out.println("006 ... OK");
envCtx.close();envCtx=null;
System.out.println("007 ... OK");
initCtx.close();initCtx=null;
System.out.println("008 ... OK");
}
catch(SQLException excp_sql)
{
stat=2;
System.out.println("009 ... OK");
excp_rtn=excp_sql;
System.out.println("010 ... OK");
}
catch(Exception excp_close)
{
System.out.println("011 ... OK");
stat=1;
System.out.println("012 ... OK");
excp_rtn=excp_close;
}
System.out.println("013 ... OK");
System.out.println(string_2);
return stat;
}
}
相关Log。
[Start public static int bridge()]
<Version: 2004-10-19-03>
-- Skip over EXCP if it is NULL.
-- Set NULL after useing.
-- Use TFW-Standard-Interface.
-- Provide TFW-Standard-Interface.
[Start public static void db_operate()*P]
<Version: 2004-10-19-04>
-- Skip over the EXCP if it is NULL.
-- Use TFW-Standard-Interface.
-- Provide TFW-Standard-Interface.
-- Need to close "rs".
-- Trying Connection-Pool.
[Ended public static void db_operate()*P]
[Start public static String notice()]
[Ended public static String notice()]
[Start public static ResultSet result()]
[Ended public static ResultSet result()]
[Start public static Exception exception()]
Exception caught: java.lang.NullPointerException
[Ended public static Exception exception()]
[Start public static int close()]
001 ... OK
002 ... OK
003 ... OK
004 ... OK
011 ... OK
012 ... OK
013 ... OK
[Ended public static int close()]
[Start public static Exception exception()]
Exception caught: java.lang.NullPointerException
[Ended public static Exception exception()]
[Ended public static int bridge()]
[Start public static String result()]
[Ended public static String result()]
[Start public Exception exception()]
Exception caught: java.lang.NullPointerException
[Ended public Exception exception()]
[Start public static int bridge()]
<Version: 2004-10-19-03>
-- Skip over EXCP if it is NULL.
-- Set NULL after useing.
-- Use TFW-Standard-Interface.
-- Provide TFW-Standard-Interface.
[Start public static void db_operate()*P]
<Version: 2004-10-19-04>
-- Skip over the EXCP if it is NULL.
-- Use TFW-Standard-Interface.
-- Provide TFW-Standard-Interface.
-- Need to close "rs".
-- Trying Connection-Pool.
[Ended public static void db_operate()*P]
[Start public static String notice()]
[Ended public static String notice()]
[Start public static ResultSet result()]
[Ended public static ResultSet result()]
[Start public static Exception exception()]
Exception caught: null
[Ended public static Exception exception()]
[Start public static int close()]
001 ... OK
002 ... OK
003 ... OK
004 ... OK
011 ... OK
012 ... OK
013 ... OK
[Ended public static int close()]
[Start public static Exception exception()]
Exception caught: java.lang.NullPointerException
[Ended public static Exception exception()]
[Ended public static int bridge()]
[Start public static String result()]
[Ended public static String result()]
[Start public Exception exception()]
Exception caught: java.lang.NullPointerException
[Ended public Exception exception()]
把异常的堆栈打印出来可以帮助你定位是在那一行出的问题。
大概看了一下,发现一个问题:
//>>Shut down DB_operator
i1=DB_operator_Pooled_1.close();
if(i1!=0)
{
stat=i1;
excp_rtn=DB_operator_Pooled_1.exception();
}
//>>Shut down DB_operator
try
{
rsmd=null;
rs.close();rs=null;
}
catch(Exception e_other)
{
stat=1;
excp_rtn=e_other;
}
在DB_operator_Pooled_1.close()中,已经关闭了Statement、Connection,这时ResultSet已经不可用了。 就是说,只要“连接.close()”,结果集就自动失效而不必再手动“结果集.close()”了?这时可以直接“结果集=null”吗? 我用SYBASE的驱动试了一下,发现只关闭Connection,不关闭Statement时,ResultSet还可用,其它的驱动就不知道了。
一般情况下,最好还是按ResultSet、Statement、Connection的顺序把它们都关闭。 我用SYBASE的驱动试了一下,发现只关闭Connection,不关闭Statement时,ResultSet还可用,其它的驱动就不知道了。
不使用连接池时,PostgreSQL的也如此。
页:
[1]