java.lang.NullPointerException的问题
这是JDBConnection.java代码
package com.wy.tool;
import java.sql.*;
public class JDBConnection {
private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_BlodMay";
private final String userName = "sa";
private final String password = "";
private Connection con = null;
//通过构造方法加载数据库驱动
static {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}
//创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
//对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
}
//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
}
-------------------------------------------------------------------------------------------------------
下面是出错的情况
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.wy.tool.JDBConnection.executeQuery(JDBConnection.java:52) com.wy.dao.ConsumerDao.getConsumerForm(ConsumerDao.java:98)
com.wy.webiter.ConsumerServlet.checkConsumer(ConsumerServlet.java:169)
com.wy.webiter.ConsumerServlet.doGet(ConsumerServlet.java:16)
com.wy.webiter.ConsumerServlet.doPost(ConsumerServlet.java:186)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
先讲下红的重点的吧