Java Null Pointer
Does anyone know why I am getting a null pointer error when I call the getResultSet() method from MyServ2 class
here is my DBClass (imports etc omitted)
public DBClass(){
}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public ResultSet getResultSet(String query){
try{
stmt = conn.createStatement();
result = stmt.executeQuery(query);
} catch(Exception e){
e.printStackTrace();
return null;
}
return result;
}
}
and this is my MyServ2 class
public class MyServ2 extends HttpSe开发者_如何学运维rvlet {
private static final long serialVersionUID = 1L;
private DBClass db;
public MyServ2() {
super();
db = new DBClass();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResultSet rs = db.getResultSet("Select * from ....ect");
try {
while(rs.next()){
System.out.println(rs.getString(1).toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
You're not calling db.dbConnect()
, so db.conn
will be null.
精彩评论