Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Prepared Statement exceptions please help

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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();
    		    }
    	}
     
    }
    Last edited by copeg; November 11th, 2010 at 04:03 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Prepared Statement exceptions please help

    Connection con=null;
    ...
    pstmt = (OraclePreparedStatement) con.prepareStatement(query); // create a statement

    Where is your connection established (eg instantiated)?

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prepared Statement exceptions please help

    Connection con=DriverManager.getConnection( url, "scott", "tiger");

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Prepared Statement exceptions please help

    Quote Originally Posted by nrao View Post
    Connection con=DriverManager.getConnection( url, "scott", "tiger");
    Are you sure about that? I see one place where you local connection in main is assigned
    Connection con=null;

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prepared Statement exceptions please help

    it is in getConnection method above

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Prepared Statement exceptions please help

    Quote Originally Posted by nrao View Post
    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

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 07:43 AM
  3. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 13th, 2010, 12:25 AM
  4. Replies: 0
    Last Post: December 12th, 2009, 10:09 PM
  5. Replies: 2
    Last Post: November 19th, 2009, 11:55 PM