null pointer exception
hai,
i am trying this i am getting null pointer exception at Db.insertData();
<jsp:useBean id="Db" scope="session" class="DataBaseConnect">
</jsp:useBean>
<%
String sql=null;
sql="insert into employee(job_desc,emp_name) values ('manager','xxxx')";
Db.openConn();
Db.insertData(sql);
Db.closeStmt();
Db.closeConn();
%>
bean code:
import java.sql.*;
import java.io.*;
public class DataBaseConnect {
Connection con = null;
Statement stmt = null;
ResultSet result = null;
public DataBaseConnect() {}
public void openConn() throws Exception {
try {
Class.forName("com.jnetdirect.jsql.JSQLDriver");
String sHost = "127.0.0.1";
con = DriverManager.getConnection
("jdbc:JSQLConnect://"+sHost+"/database=res&user=sa");
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertData(String query) throws Exception{
stmt=con.createStatement();
int i=stmt.executeUpdate(query);
}
public void closeStmt()
throws Exception {
stmt.close();
stmt = null;
}
public void closeConn()
throws Exception {
con.close();
con = null;
}
}
please help me .
what could be the problem
[1427 byte] By [
reddy94] at [2007-9-26 2:33:21]

Reddy,
I will try to reconstruct your code so that you can make use of a bean to add a new record in the database. I hope that will be okay. I will also try to make a comment on some of the lines so you'll better appreciate it. if in case you would need clarification, post your question here. I will include this topic in my watch list.
your jsp File here:
<jsp:useBean id="Db" scope="session" class="DataBaseConnect"/>
<%
String sql = "insert into employee(job_desc,emp_name) values ('manager','xxxx')";
Db.openConn();
if (Db.insertData(sql)) {
%>
<p> Add successfull </p>
<%} else {%>
<p> Add not successfull!</p>
<%}
Db.closeStmt();
Db.closeConn();
%>
your bean here:
> import java.sql.*;
public class DataBaseConnect {
private Connection con;
private Statement stmt;
private ResultSet result;
//declare you variables private to make them safe because all methods are declared public.
public DataBaseConnect() {
//initialize your variables at your bean constructor
con = null;
stmt = null;
result = null;
}
public void openConn() throws Exception {
// you don't need the try and catch statement since you want to throw your exception
Class.forName("com.jnetdirect.jsql.JSQLDriver");
String sHost = "127.0.0.1";
con = DriverManager.getConnection("jdbc:JSQLConnect://"+sHost+"/database=res&user=sa");
}
public boolean insertData(String query) throws Exception{
//we declare this boolean so that we will know if the add is successful or not
stmt=con.createStatement();
int i=stmt.executeUpdate(query); // if add is successful i should be equal to 1
if (i > 0) {return true;}
else {return false;}
}
public void closeStmt() throws Exception {
stmt.close();
}
public void closeConn() throws Exception {
con.close();
}
}
i hope this code will help you.
Cris