cenqin8899
发贴: 6
积分: 0
|
于 2006-10-27 11:03
系统错误为java.lang.NullPointerException 请高手指点!下面是2个java类和一个jsp文件的源码,非常感谢!
package db; import java.util.*; import java.sql.*; import java.io.*;
public class DbConnection { Connection conn=null; Statement stmt=null; ResultSet rset=null;
public DbConnection() {
}
/*********************************************************** * 方法名称:openConnection * 参数:无 * 返回值类型:boolean * 说明:打开数据库Connection的方法 * 为了打开Connection,读取位于类路径下的db.properties ***********************************************************/ public boolean openConnection() {
String jdbc="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspbook"; String user="sa"; String password="sa";
try{ Class.forName(jdbc); } catch(ClassNotFoundException e){ System.out.println("JDBC登录过程中出现错误"+e.getMessage()); return false; }
//----------------------------------------------------- //STEP 5.打开数据库Connection //----------------------------------------------------- try{ this.conn=DriverManager.getConnection(url,user,password); } catch(SQLException e){ System.out.println("生成Connection过程中出现错误:"+e.getMessage()); return false; } return true; }
/*********************************************************** * 方法名称:executeQuery * 参数:query(SQL询问语句) * 返回值类型:java.sql.ResultSet * 说明:查询数据库的方法(SELECT) ***********************************************************/ public ResultSet executeQuery(String query) { try{ this.stmt=conn.createStatement(); this.rset=stmt.executeQuery(query); } catch(SQLException e){
} return rset; }
/*********************************************************** * 方法名称:executeUpdate * 参数:query(SQL询问语句) * 返回值类型:void * 说明:修改数据库的方法(UPDATE,DELETE,INSERT) ***********************************************************/ public void executeUpdate(String query) { try{ this.stmt = conn.createStatement(); stmt.executeUpdate(query); if (stmt != null) stmt.close(); } catch(SQLException e){
}
}
/*********************************************************** * 方法名称:close * 参数:无 * 返回值类型:void * 说明:返回数据库链接的相关资料 ***********************************************************/ public void close() { try{ if (conn != null) conn.close(); if (rset != null) rset.close(); if (stmt != null) stmt.close(); } catch(SQLException e){
}
}
/*********************************************************** * 方法名称:finalize * 参数:无 * 返回值类型:void * 说明:服务器回收资源 ***********************************************************/ public void finalize() { try { this.close(); } catch (Exception e) { } } }
---------------------------
package db; import java.sql.*;
public class ViewQueryBean { DbConnection dc=null; ResultSet rset=null;
//****************************************** //建构子 //****************************************** public ViewQueryBean() { dc=new DbConnection(); }
//****************************************** //生成Connection //****************************************** public boolean openConnection(){ return dc.openConnection(); }
//****************************************** //执行SQL语句(SELECT) //****************************************** public void executeQuery(String query)throws SQLException{ this.rset=dc.executeQuery(query); }
//****************************************** //执行SQL语句(INSERT, DELETE, UPDATE) //****************************************** public void executeUpdate(String query)throws SQLException { dc.executeUpdate(query);
}
//****************************************** //求出全部Column的个数 //****************************************** public int getColumnCount()throws SQLException{ ResultSetMetaData rsmd=rset.getMetaData(); return rsmd.getColumnCount(); }
//****************************************** //通过检索号码获取Column的名称 //****************************************** public String getColumnName(int index) throws SQLException{ ResultSetMetaData rsmd=rset.getMetaData(); return rsmd.getColumnName(index); }
//****************************************** //通过Column index获取数据 //****************************************** public String getData(int index) throws SQLException{ return rset.getString(index).trim(); }
//****************************************** //通过Column的名称获取数据 //****************************************** public String getData(String columnName) { String str=null; try{ str= rset.getString(columnName).trim(); } catch(Exception e){} return str; }
//****************************************** //将ResultSet的指针移至下一个记录 //****************************************** public boolean next() throws Exception { return rset.next(); }
//****************************************** //整理对象 //****************************************** public void close() { try{ if(rset!=null) rset.close(); if(dc!=null) dc.close(); }catch(Exception e){}
}
//****************************************** //finalize() //****************************************** public void finalize() { try{ close(); }catch(Exception e){}
} }
-----------------------------
<%@ page info="[About JSP] DB Connection Bean案例" contentType="text/html; charset=GBK" import="db.ViewQueryBean"%> <jsp:useBean id="bean" scope="request" class="db.ViewQueryBean" /> <% try{ String query=request.getParameter("query")==null?"SELECT id FROM student":request.getParameter("query"); bean.openConnection(); bean.executeUpdate(query); }catch(Exception e){} %> <html> <head> <title> ViewQuery </title> </head> <body bgcolor="#ffffff">
<!--检索Table--> <h3>SQL查询语句 </h3> <form action="ViewQuery.jsp" method="post"> <input size="80" name="query" /> <input type="submit" value="传送查询语句" /> </form>
<hr />
<h3>结果 VIEW</h3> <table border="1"> <!--[开始] 显示Column名称--> <tr> <% try{ for(int i=1; i<=bean.getColumnCount();i++){ %> <td><%=bean.getColumnName(i) %></td> <%} }catch(Exception e){e.printStackTrace();}%> </tr> <!--[结尾] 显示Column名称-->
<!--[开始]显示数据--> <% try{ while(bean.next()) {%> <tr> <% for(int i=1;i<=bean.getColumnCount();i++){ %> <td><%=bean.getData(i) %></td> <%} %> </tr> <%} }catch(Exception e){} %> <!--[结尾显示数据]--> </table> </body> </html>
why edited on 2006-10-27 11:14
|