Can anybody tell me what is the problem with the following code.Whenever I run this,it throws a null pointer exception.Can anybody tell me the reason why it throws null pointer exception.My database has seven fields of type "Text".
import java.sql.*;
publicclass DBCon
{ Connection con=null;
Statement stmt;
public DBCon()
{
insert();
}//End of Constructor
publicstaticvoid main(String[] args) {
DBCon db = new DBCon(); }
publicvoid insert(){
try{ //----------------- Driver -------------------
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//----------------- URL WITHOUT DSN -------
String conString = "jdbc:odbc:DRIVER={MicroSoft Access Driver (*.mdb)};" +
"DBQ=./Data/MuktadirNiketon.mdb";
con = DriverManager.getConnection(conString,"","");
String sql=" INSERT INTO Flat_Rent VALUES ('3-2008','ss','ss','ss','ss','ss','ss') " ;
System.out.println("SQL>>"+sql);
stmt.executeUpdate(sql);
}catch(SQLException sqle){
System.out.println("DSN Not Found !!!"); }
catch(ClassNotFoundException sqle){
System.out.println("Driver Not Found !!!"); }
}
}//End of Class
The variable stmt is not being initialised before it is used. The Statement object sends the SQL to the database but has not been given initialised with the database connection con. You need a line similar to:
stmt = con.createStatement();
in there somewhere (between making the connection and executing the statement).
Woah, JDBC + Microsoft Access as a Database? This surely has to be a small project? No persistence framework (less error prone) or something more capable than MS Access (PostgreSql - cheap and decent)?