Prepared Statement exceptions please help
exception
java.lang.NullPointerException
at jdbcstatements.Insertion.main(Insertion.java:37)
Exception in thread "main" java.lang.NullPointerException
at jdbcstatements.Insertion.main(Insertion.java:50)
my code
Code :
package jdbcstatements;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.jdbc.OraclePreparedStatement;
public class Insertion {
/**
* @param args
* @throws SQLException
*
*/
public static Connection getConnection()throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
Connection con=DriverManager.getConnection( url, "scott", "tiger");
System.out.println("connection created");
return con;
}
public static void main(String[] args) throws SQLException {
Connection con=null;
OraclePreparedStatement pstmt = null;
try {
String query = "insert into EMP(ENAME, JOB, SAL) values(?, ?, ?)";
System.out.println("insert the record");
pstmt = (OraclePreparedStatement) con.prepareStatement(query); // create a statement
pstmt.setString(1, "Navatha"); // set input parameter 2
pstmt.setString(2, "Manger"); // set input parameter 3
pstmt.setString(3, "3000");
pstmt.executeUpdate();
} catch(Exception e){e.printStackTrace();}
finally {
pstmt.close();
con.close();
}
}
}
Re: Prepared Statement exceptions please help
Code :
Connection con=null;
...
pstmt = (OraclePreparedStatement) con.prepareStatement(query); // create a statement
Where is your connection established (eg instantiated)?
Re: Prepared Statement exceptions please help
Connection con=DriverManager.getConnection( url, "scott", "tiger");
Re: Prepared Statement exceptions please help
Quote:
Originally Posted by
nrao
Connection con=DriverManager.getConnection( url, "scott", "tiger");
Are you sure about that? I see one place where you local connection in main is assigned
Re: Prepared Statement exceptions please help
it is in getConnection method above
Re: Prepared Statement exceptions please help
Quote:
Originally Posted by
nrao
it is in getConnection method above
So? At what point do you call getConnection()? If that still doesn't make sense, you should really re-visit the basics of the java language: Learning the Java Language