Forum Stats

  • 3,734,847 Users
  • 2,247,054 Discussions
  • 7,857,524 Comments

Discussions

Null Pointer Exception: table Create USING MYSQL (If not exists")

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited May 12, 2016 1:53AM in New To Java

Hi,

i have written a code to create a table 7 to insert data in it. However I am getting a null pointer Exception. Table already exist & "create table if not exists" doesnt seem to work. Some body please guide me.

This is my code:

import java.sql.*;

public class InsertCreate {

   // JDBC driver name and database URL

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 

   static  String DB_URL = "jdbc:mysql://localhost/STUDENTS";

   //  Database credentials

   static  String USER = "root";

   static  String PASS = "";

   //String sql="";

public static void main(String[] args) {

     

   Connection conn = null;

   Statement stmt = null;

   String sql="";

  

try{

   //STEP 2: Register JDBC driver

      Class.forName("com.mysql.jdbc.Driver");

  

   conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //creating table

      sql = "CREATE TABLE if not exists REGISTRATION " +

                   "(id INTEGER not NULL, " +

                   " first VARCHAR(255), " +

                   " last VARCHAR(255), " +

                   " age INTEGER, " +

                   " PRIMARY KEY ( id ))";

     System.out.println("SSSSSSSSSSSSS");

      stmt.executeUpdate(sql);  //ThIS LINE GIVING ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

     System.out.println("QQQQQQQQQQQQQ");

      //inserting data

      sql = "Insert into registeration values (1, 'first1', 'last1', 20)";

      stmt.executeUpdate(sql);

     

      //STEP 6: Clean-up environment

     

      stmt.close();

      conn.close();

   }catch(SQLException se){

      //Handle errors for JDBC

      se.printStackTrace();

   }catch(Exception e){

      //Handle errors for Class.forName

      e.printStackTrace();

   }finally{

      //finally block used to close resources

      try{

         if(stmt!=null)

            stmt.close();

      }catch(SQLException se2){

      }// nothing we can do

      try{

         if(conn!=null)

            conn.close();

      }catch(SQLException se){

         se.printStackTrace();

      }//end finally try

   }//end try

   System.out.println("Goodbye!");

}//end main

}//end FirstExample


This is output of my program:

>javac InsertCreate.java

>java InsertCreate

SSSSSSSSSSSSS

java.lang.NullPointerException

        at InsertCreate.main(InsertCreate.java:49)

Goodbye!

>


Somebody please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited May 12, 2016 1:53AM Accepted Answer

    Hi,

    Thanks for your guidance. I have solved this problem. The correct code or creating table & inserting data into it is given below:

    import java.sql.*;

    public class InsertCreate {

       // JDBC driver name and database URL

       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 

       static  String DB_URL = "jdbc:mysql://localhost/STUDENTS";

       //  Database credentials

       static  String USER = "root";

       static  String PASS = "";

       //String sql="";

    public static void main(String[] args) {

         

       Connection conn = null;

       Statement stmt = null;

       String sql="";

      

    try{

       //STEP 2: Register JDBC driver

          Class.forName("com.mysql.jdbc.Driver");

      

       conn = DriverManager.getConnection(DB_URL,USER,PASS);

         

    stmt = conn.createStatement();

          //creating table

          sql = "CREATE TABLE if not exists REGISTRATION " +

                       "(id INTEGER not NULL, " +

                       " first VARCHAR(255), " +

                       " last VARCHAR(255), " +

                       " age INTEGER, " +

                       " PRIMARY KEY ( id ))";

         System.out.println("SSSSSSSSSSSSS");

          stmt.executeUpdate(sql);

         System.out.println("QQQQQQQQQQQQQ");

          //inserting data

          sql = "Insert into registration values (2, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (3, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (4, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (5, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (6, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (7, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (8, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (9, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (10, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (11, 'first2', 'last2', 20)";

          sql = "Insert into registration values (12, 'first2', 'last2', 20)";

          sql = "Insert into registration values (14, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

    System.out.println("RRRRRRRRRRRR");

         

          //STEP 6: Clean-up environment

         

          stmt.close();

          conn.close();

       }catch(SQLException se){

          //Handle errors for JDBC

          se.printStackTrace();

       }catch(Exception e){

          //Handle errors for Class.forName

          e.printStackTrace();

       }finally{

          //finally block used to close resources

          try{

             if(stmt!=null)

                stmt.close();

          }catch(SQLException se2){

          }// nothing we can do

          try{

             if(conn!=null)

                conn.close();

          }catch(SQLException se){

             se.printStackTrace();

          }//end finally try

       }//end try

       System.out.println("Goodbye!");

    }//end main

    }//end

    Zulfi.

Answers

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited May 11, 2016 8:36AM

    Statement stmt = null;

    ...


    stmt.executeUpdate(sql);



    must end up with null pointer exception (NPE).

    User_AYF65
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited May 12, 2016 1:53AM Accepted Answer

    Hi,

    Thanks for your guidance. I have solved this problem. The correct code or creating table & inserting data into it is given below:

    import java.sql.*;

    public class InsertCreate {

       // JDBC driver name and database URL

       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 

       static  String DB_URL = "jdbc:mysql://localhost/STUDENTS";

       //  Database credentials

       static  String USER = "root";

       static  String PASS = "";

       //String sql="";

    public static void main(String[] args) {

         

       Connection conn = null;

       Statement stmt = null;

       String sql="";

      

    try{

       //STEP 2: Register JDBC driver

          Class.forName("com.mysql.jdbc.Driver");

      

       conn = DriverManager.getConnection(DB_URL,USER,PASS);

         

    stmt = conn.createStatement();

          //creating table

          sql = "CREATE TABLE if not exists REGISTRATION " +

                       "(id INTEGER not NULL, " +

                       " first VARCHAR(255), " +

                       " last VARCHAR(255), " +

                       " age INTEGER, " +

                       " PRIMARY KEY ( id ))";

         System.out.println("SSSSSSSSSSSSS");

          stmt.executeUpdate(sql);

         System.out.println("QQQQQQQQQQQQQ");

          //inserting data

          sql = "Insert into registration values (2, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (3, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (4, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (5, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (6, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (7, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (8, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (9, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (10, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

          sql = "Insert into registration values (11, 'first2', 'last2', 20)";

          sql = "Insert into registration values (12, 'first2', 'last2', 20)";

          sql = "Insert into registration values (14, 'first2', 'last2', 20)";

          stmt.executeUpdate(sql);

    System.out.println("RRRRRRRRRRRR");

         

          //STEP 6: Clean-up environment

         

          stmt.close();

          conn.close();

       }catch(SQLException se){

          //Handle errors for JDBC

          se.printStackTrace();

       }catch(Exception e){

          //Handle errors for Class.forName

          e.printStackTrace();

       }finally{

          //finally block used to close resources

          try{

             if(stmt!=null)

                stmt.close();

          }catch(SQLException se2){

          }// nothing we can do

          try{

             if(conn!=null)

                conn.close();

          }catch(SQLException se){

             se.printStackTrace();

          }//end finally try

       }//end try

       System.out.println("Goodbye!");

    }//end main

    }//end

    Zulfi.

This discussion has been closed.