Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why the JDBC statement is always null

Status
Not open for further replies.

IPOz

Programmer
Jul 11, 2001
109
CN
Hi,friends
In order to test the JDBC performance i wrote a small java application below.(The JDK is 1.3.1)

[DbTest.java]
import java.util.*;

import DatabaseOperation;

public class DbTest {
public static void main(String[] args) {
try {
DatabaseOperation DbOper;
DbOper = new DatabaseOperation();

Date startTime = new Date();
DbOper.executeQuery("select * from fc_room");
Date stopTime = new Date();
Date lapsedTime = new Date(stopTime.getTime() - startTime.getTime());
System.out.println("Total cost=" + lapsedTime.getMinutes()+"m:" + lapsedTime.getSeconds()+"s");

DbOper.executeClose();
}
catch(Exception e) {
System.err.println("Why:"+e.getMessage());
}
}
}

[DatabaseOperation.java]
import java.sql.*;

public class DatabaseOperation {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:eek:dbc:jboss_odbc";
Connection conn;
ResultSet rs;
Statement stmt;

public DatabaseOperation() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("sql_data():"+e.getMessage());
}
}
public void executeInsert(String sql) {
try {
conn = DriverManager.getConnection(sConnStr,"ihpms","ihpms");
Statement stmt= conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("sql_data.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"ihpms","ihpms");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("sql_data.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeDelete(String sql) {
try {
conn = DriverManager.getConnection(sConnStr,"ihpms","ihpms");
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("sql_data.executeDelete:"+ex.getMessage());
}
}
public void executeUpdate(String sql) {
try {
conn = DriverManager.getConnection(sConnStr,"ihpms","ihpms");
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("sql_data.executeDelete:"+ex.getMessage());
}
}
public void executeClose() {
try {
stmt.close();
conn.close();
rs.close();
}
catch(Exception ex) {
System.out.println(ex.toString());
}
}
}

The compile is ok but when i run it by java DbTest
java.lang.NullPointerException is raised!

Using jdb i found in DatabaseOperation.executeQuery method the stmt is null(the conn has value assigned).

Hope i can get helps from all of you.Thanks! IPO_z@cmmail.com
Garbage in,Garbage out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top