免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1055 | 回复: 1
打印 上一主题 下一主题

[bean]两个bean,报异常。含LOG,大家给看看。谢谢。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-19 17:25 |只看该作者 |正序浏览
两个bean,报异常。已经追踪到异常产生的地方,可是想不通为什么关闭结果集(“OK ... 002”后面)和连接(“OK ... 004”后面)会产生空指针异常。含LOG,大家给指点指点。谢谢。

Bridge_Pooled_1.java,作为JSP到数据库操作bean之间的连接,向数据库操作bean传递操作,向JSP传递处理过的操作结果。
  1. package test;
  2. import java.lang.*;
  3. import java.sql.*;
  4. //import jsp_db_oprt.*;
  5. /**
  6. import javax.naming.Context;
  7. import javax.sql.DataSource;
  8. import javax.naming.InitialContext;
  9. */
  10. public class Bridge_Pooled_1
  11. {
  12.         //>;>;Put out.
  13.         private static int stat=0;
  14.         private static String text="";
  15.         private static String strOUT="";
  16.         private static Exception excp_rtn=null;
  17.         //<<Put out.
  18.         public static int bridge(String conffile,String strIN)
  19.         {
  20.                 stat=0;
  21.                 text="";
  22.                 strOUT="";
  23.                 excp_rtn=null;
  24.                 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.";
  25.                 String string_2="[Ended public static int bridge()]";
  26.                 System.out.println(string_1);
  27.                 String sql = strIN;
  28.                 sql=strIN;
  29.                 //>;>;Call DB_operator
  30.                 int rs_stat=0;
  31.                 String rs_text="";
  32.                 ResultSet rs=null;
  33.                 Exception rs_excp=null;
  34.                 rs_stat=DB_operator_Pooled_1.db_operate(conffile,sql);
  35.                 rs_text=DB_operator_Pooled_1.notice();
  36.                 rs=DB_operator_Pooled_1.result();
  37.                 rs_excp=DB_operator_Pooled_1.exception();
  38.                 //<<Call DB_operator
  39.                 int numbCols=0;
  40.                 String nameCols="";
  41.                 String l_s="<tr>;";//line_start
  42.                 String l_e="</tr>;";//line_ended
  43.                 String c_s="<td>;";//colum_start
  44.                 String c_e="</td>;";//colum_ended
  45.                 String s1="";
  46.                 String s2="";
  47.                 int i1=0;
  48.                 int i2=0;
  49.                 ResultSetMetaData rsmd=null;
  50.                 if(stat==0)
  51.                 {
  52.                         try
  53.                         {
  54.                                 rsmd=rs.getMetaData();
  55.                                 numbCols=rsmd.getColumnCount();
  56.                                 i1=1;s2=l_s;
  57.                                 while(i1<=numbCols)
  58.                                 {
  59.                                         s1=rsmd.getColumnName(i1);
  60.                                         s2=s2+c_s+s1+c_e;
  61.                                         i1++;
  62.                                 }
  63.                                 s2=s2+l_e;
  64.                                 while(rs.next())
  65.                                 {
  66.                                         i1=1;
  67.                                         s2=s2+l_s;
  68.                                         while(i1<=numbCols)
  69.                                         {
  70.                                                 s1=rs.getString(i1);
  71.                                                 s2=s2+c_s+s1+c_e;
  72.                                                 i1++;
  73.                                         }
  74.                                         s2=s2+l_e;
  75.                                 }
  76.                                 s1="<table>;"+s2+"</table>;";
  77.                                 strOUT=s1;
  78.                         }
  79.                         catch(Exception ne)
  80.                         {
  81.                                 stat=1;
  82.                                 excp_rtn=ne;
  83.                         }
  84.                 }
  85.                 //>;>;Shut down DB_operator
  86.                 i1=DB_operator_Pooled_1.close();
  87.                 if(i1!=0)
  88.                 {
  89.                         stat=i1;
  90.                         excp_rtn=DB_operator_Pooled_1.exception();
  91.                 }
  92.                 //>;>;Shut down DB_operator
  93.                 try
  94.                 {
  95.                         rsmd=null;
  96.                         rs.close();rs=null;
  97.                 }
  98.                 catch(Exception e_other)
  99.                 {
  100.                         stat=1;
  101.                         excp_rtn=e_other;
  102.                 }
  103.                 System.out.println(string_2);
  104.                 return stat;
  105.         }
  106.         public static String result()
  107.         {
  108.                 String string_1="[Start public static String result()]";
  109.                 String string_2="[Ended public static String result()]";
  110.                 System.out.println(string_1);
  111.                 System.out.println(string_2);
  112.                 return strOUT;
  113.         }
  114.         public static String notice()
  115.         {
  116.                 String string_1="[Start public static String notice()]";
  117.                 String string_2="[Ended public static String notice()]";
  118.                 System.out.println(string_1);
  119.                 if(stat==0)
  120.                 {
  121.                         text="Every thing goes on well.";
  122.                 }
  123.                 else if(stat==2)
  124.                 {
  125.                         text="Exception while input or output.";
  126.                 }
  127.                 else if(stat==3)
  128.                 {
  129.                         text="Exception caused by no class or no file found.";
  130.                 }
  131.                 else if(stat==4)
  132.                 {
  133.                         text="Exception with SQL.";
  134.                 }
  135.                 else
  136.                 {
  137.                         text="Exception somewhere.";
  138.                 }
  139.                 System.out.println(string_2);
  140.                 return text;
  141.         }
  142.         public static Exception exception()
  143.         {
  144.                 String string_1="[Start public Exception exception()]";
  145.                 String string_2="[Ended public Exception exception()]";
  146.                 System.out.println(string_1);
  147.                 System.out.println("Exception caught: "+excp_rtn);
  148.                 System.out.println(string_2);
  149.                 return excp_rtn;
  150.         }
  151. }
复制代码

DB_operator_Pooled_1.java,执行数据库操作,返回结果集。
  1. /**
  2. Instruction:
  3. As a bean of JSP, do the SQL operation && return the "ResultSet". Uses && provides TFW-Starndard-Interface.
  4. return 0: Every thing goes on well.
  5. return 1: Exception somewhere.
  6. return 2: Exception while input or output.
  7. return 3: Exception caused by no class or no file found.
  8. return 4: Exception with SQL.
  9. New version modified much, still need to do futher test.
  10. 介绍:
  11. 一个执行SQL操作并返回“结果集”的bean。使用和提供TFW标准接口。
  12. 返回0: 一切正常。
  13. 返回1:有地方异常。
  14. 返回2: 有输入输出异常。
  15. 返回3: 有文件或类未找到的异常。
  16. 返回4: SQL操作异常。
  17. 新版本改动很大,还需要进一步测试。
  18. Guide:
  19. "db_operate(String conffile,String sql)".
  20. 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.
  21. 指引:
  22. “db_operate(String conffile,String sql)”。
  23. package AA.BB...NN;
  24. 如果要打到 “AA.BB...NN” 包里,就添加这些。注意,包必须放置在其中一处“CLASSPATH”下。
  25. */
  26. package test;
  27. import java.lang.*;
  28. import java.io.*;
  29. import java.sql.*;
  30. import javax.sql.DataSource;
  31. import javax.naming.Context;
  32. import javax.naming.InitialContext;
  33. import tfw.db_deal.core.*;
  34. public class DB_operator_Pooled_1
  35. {
  36.         //>;>;Put out.
  37.         private static int stat=0;
  38.         private static String text="";
  39.         private static ResultSet rs=null;
  40.         private static Exception excp_rtn=null;
  41.         //<<Put out.
  42.         private static Context initCtx=null;
  43.         private static Context envCtx=null;
  44.         private static DataSource ds=null;
  45.         private static Connection conn=null;
  46.         private static Statement stmt=null;
  47.         private static ResultSetMetaData rsmd=null;
  48.         public static int db_operate(String conffile,String sql)
  49.         {
  50.                 stat=0;
  51.                 text="";
  52.                 excp_rtn=null;
  53.                 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.";
  54.                 String string_2="[Ended public static void db_operate()*P]";
  55.                 System.out.println(string_1);
  56.                 char LG='E';
  57.                 int GUI=0;
  58.                 int conn_stat=0;
  59.                 String conn_text="";
  60.                 Connection conn=null;
  61.                 Exception conn_excp=null;
  62.                 int rs_stat=0;
  63.                 String rs_text="";
  64.                 rs=null;
  65.                 Exception rs_excp=null;
  66.                 try
  67.                 {
  68.                         initCtx = new InitialContext();
  69.                         envCtx = (Context) initCtx.lookup("java:comp/env");
  70.                         ds = (DataSource)envCtx.lookup("jdbc/TestPool");
  71.                         if(ds!=null)
  72.                         {
  73.                                 conn=ds.getConnection();
  74.                                 if(conn!=null)
  75.                                 {
  76.                                         stmt = conn.createStatement();
  77.                                         rs = stmt.executeQuery(sql);
  78.                                 }
  79.                         }
  80.                 }
  81.                 catch(SQLException esql_1)
  82.                 {
  83.                         stat=2;
  84.                         excp_rtn=esql_1;
  85.                 }
  86.                 catch(Exception e1)
  87.                 {
  88.                         stat=1;
  89.                         excp_rtn=e1;
  90.                 }
  91.                 System.out.println(string_2);
  92.                 return stat;
  93.         }
  94.         public static String notice()
  95.         {
  96.                 String string_1="[Start public static String notice()]";
  97.                 String string_2="[Ended public static String notice()]";
  98.                 System.out.println(string_1);
  99.                 if(stat==0)
  100.                 {
  101.                         text="Every thing goes on well.";
  102.                 }
  103.                 else if(stat==2)
  104.                 {
  105.                         text="Exception while input or output.";
  106.                 }
  107.                 else if(stat==3)
  108.                 {
  109.                         text="Exception caused by no class or no file found.";
  110.                 }
  111.                 else if(stat==4)
  112.                 {
  113.                         text="Exception with SQL.";
  114.                 }
  115.                 else
  116.                 {
  117.                         text="Exception somewhere.";
  118.                 }
  119.                 System.out.println(string_2);
  120.                 return text;
  121.         }
  122.         public static ResultSet result()
  123.         {
  124.                 String string_1="[Start public static ResultSet result()]";
  125.                 String string_2="[Ended public static ResultSet result()]";
  126.                 System.out.println(string_1);
  127.                 System.out.println(string_2);
  128.                 return rs;
  129.         }
  130.         public static Exception exception()
  131.         {
  132.                 String string_1="[Start public static Exception exception()]";
  133.                 String string_2="[Ended public static Exception exception()]";
  134.                 System.out.println(string_1);
  135.                 System.out.println("Exception caught: "+excp_rtn);
  136.                 System.out.println(string_2);
  137.                 return excp_rtn;
  138.         }
  139.         public static int close()
  140.         {
  141.                 String string_1="[Start public static int close()]";
  142.                 String string_2="[Ended public static int close()]";
  143.                 System.out.println(string_1);
  144. System.out.println("001 ... OK");
  145.                 try
  146.                 {
  147. System.out.println("002 ... OK");
  148.                         /*rs.close();*/rs=null;
  149. System.out.println("003 ... OK");
  150.                         stmt.close();stmt=null;
  151. System.out.println("004 ... OK");
  152.                         conn.close();conn=null;
  153. System.out.println("005 ... OK");
  154.                         ds=null;
  155. System.out.println("006 ... OK");
  156.                         envCtx.close();envCtx=null;
  157. System.out.println("007 ... OK");
  158.                         initCtx.close();initCtx=null;
  159. System.out.println("008 ... OK");
  160.                 }
  161.                 catch(SQLException excp_sql)
  162.                 {
  163.                         stat=2;
  164. System.out.println("009 ... OK");
  165.                         excp_rtn=excp_sql;
  166. System.out.println("010 ... OK");
  167.                 }
  168.                 catch(Exception excp_close)
  169.                 {
  170. System.out.println("011 ... OK");
  171.                         stat=1;
  172. System.out.println("012 ... OK");
  173.                         excp_rtn=excp_close;
  174.                 }
  175. System.out.println("013 ... OK");
  176.                 System.out.println(string_2);
  177.                 return stat;
  178.         }
  179. }
复制代码

相关Log。
  1. [Start public static int bridge()]
  2. <Version: 2004-10-19-03>;
  3. -- Skip over EXCP if it is NULL.
  4. -- Set NULL after useing.
  5. -- Use TFW-Standard-Interface.
  6. -- Provide TFW-Standard-Interface.
  7. [Start public static void db_operate()*P]
  8. <Version: 2004-10-19-04>;
  9. -- Skip over the EXCP if it is NULL.
  10. -- Use TFW-Standard-Interface.
  11. -- Provide TFW-Standard-Interface.
  12. -- Need to close "rs".
  13. -- Trying Connection-Pool.
  14. [Ended public static void db_operate()*P]
  15. [Start public static String notice()]
  16. [Ended public static String notice()]
  17. [Start public static ResultSet result()]
  18. [Ended public static ResultSet result()]
  19. [Start public static Exception exception()]
  20. Exception caught: java.lang.NullPointerException
  21. [Ended public static Exception exception()]
  22. [Start public static int close()]
  23. 001 ... OK
  24. 002 ... OK
  25. 003 ... OK
  26. 004 ... OK
  27. 011 ... OK
  28. 012 ... OK
  29. 013 ... OK
  30. [Ended public static int close()]
  31. [Start public static Exception exception()]
  32. Exception caught: java.lang.NullPointerException
  33. [Ended public static Exception exception()]
  34. [Ended public static int bridge()]
  35. [Start public static String result()]
  36. [Ended public static String result()]
  37. [Start public Exception exception()]
  38. Exception caught: java.lang.NullPointerException
  39. [Ended public Exception exception()]
  40. [Start public static int bridge()]
  41. <Version: 2004-10-19-03>;
  42. -- Skip over EXCP if it is NULL.
  43. -- Set NULL after useing.
  44. -- Use TFW-Standard-Interface.
  45. -- Provide TFW-Standard-Interface.
  46. [Start public static void db_operate()*P]
  47. <Version: 2004-10-19-04>;
  48. -- Skip over the EXCP if it is NULL.
  49. -- Use TFW-Standard-Interface.
  50. -- Provide TFW-Standard-Interface.
  51. -- Need to close "rs".
  52. -- Trying Connection-Pool.
  53. [Ended public static void db_operate()*P]
  54. [Start public static String notice()]
  55. [Ended public static String notice()]
  56. [Start public static ResultSet result()]
  57. [Ended public static ResultSet result()]
  58. [Start public static Exception exception()]
  59. Exception caught: null
  60. [Ended public static Exception exception()]
  61. [Start public static int close()]
  62. 001 ... OK
  63. 002 ... OK
  64. 003 ... OK
  65. 004 ... OK
  66. 011 ... OK
  67. 012 ... OK
  68. 013 ... OK
  69. [Ended public static int close()]
  70. [Start public static Exception exception()]
  71. Exception caught: java.lang.NullPointerException
  72. [Ended public static Exception exception()]
  73. [Ended public static int bridge()]
  74. [Start public static String result()]
  75. [Ended public static String result()]
  76. [Start public Exception exception()]
  77. Exception caught: java.lang.NullPointerException
  78. [Ended public Exception exception()]
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-10-20 13:06 |只看该作者

[bean]两个bean,报异常。含LOG,大家给看看。谢谢。

顶。
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP