我编了一个数据库的bean文件,但运行时却出现空指针异常,望大家指教!代码如下:
propDB.properties文件:
DB_ClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
DB-URL=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Demo
DB_User=sa
DB_Password=198699
数据库bean文件:(ConnDb.java)
package MyPackage;
import java.sql.*;
import java.io.*;
import java.util.*;
public class ConnDb {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private String propFileName="F:\\EclipseMyEclipse\\BeanDatabase\\src\\propDB.properties";
private Properties prop=new Properties();
private String dbClassName=null;
private String dbUrl=null;
private String dbUser=null;
private String dbPwd=null;
public void loadProperty(){
try{
prop.load(new FileInputStream(propFileName));
dbClassName=prop.getProperty("DB_ClassName");
dbUrl=prop.getProperty("DB-URL");
dbUser=prop.getProperty("DB-User");
dbPwd=prop.getProperty("DB-Password");
}catch (Exception e){
System.out.println(e.toString());
}
}
public Connection getConnection(){
loadProperty();
try{
Class.forName(dbClassName).newInstance();
conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
if (conn==null){
System.out.println("数据库由于某种原因没有连接上!");
}
}catch (Exception e1){
System.out.print(e1.toString());
}
return conn;
}
public ResultSet executeQuery(String sql){
try{
conn=getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch (Exception e2){
System.out.print(e2.toString());
}
return rs;
}
public int executeUpdate(String sql){
int result=0;
try{
conn=getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch (Exception e3){
result=0;
}
try{
}catch (Exception e4){
System.out.print(e4.toString());
}
return result;
}
public void close(){
try{
if (rs!=null){
rs.close();
}
}catch (Exception er){
System.out.print(er.toString());
}
try{
if (stmt!=null){
stmt.close();
}
}catch (Exception es){
System.out.print(es.toString());
}
try{
if (conn!=null){
conn.close();
}
}catch (Exception ec){
System.out.print(ec.toString());
}
}
}
运行时的JSP文件:(DbDemo.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'DbDemo.jsp' starting page</title>
</head>
<body>
<jsp:useBean id="db" class="MyPackage.ConnDb" scope="request"/>
<%
ResultSet rs=null;
try{
rs=db.executeQuery("select * from Student");
out.print("<table border=1>");
while (rs.next()){
out.print("<tr><td>"+rs.getString(1)+"</td></tr>");
out.print("<tr><td>"+rs.getString(2)+"</td></tr>");
out.print("<tr><td>"+rs.getInt(3)+"</td></tr>");
out.print("<tr><td>"+rs.getInt(4)+"</td></tr>");
}
out.print("</table>");
rs.close();
}catch (Exception e){
out.print(e.toString());
}
%>
</body>
</html>
希望能指教一下小弟,感激万分!
注:我已经用最原始的方法测试过DbDemo.jsp文件中的记录集rs一直为空,但我不知道为什么,请指点,谢谢~
[此贴子已经被作者于2007-9-30 0:54:18编辑过]