-1

I am attempting to create a database connection and update using a prepared statement with JDBC to MYSQL.

I have tried changing the connection a bit and had to change the preparedStatement class to static in order to not get errors. However, when running the app I got a NullPointerException error and I can't see where exactly I need to create a new object.

Currently, I get an error:

Exception in thread "main" java.lang.NullPointerException

at Alter.main(Alter.java:12)

import java.sql.PreparedStatement;
import java.sql.SQLException;

public abstract class Alter {

    public static void main(String[] args) throws SQLException {

                 String query = "INSERT INTO school.students (First_Name, Last_Name, Gender, Grade_Level, IEP, Disruptive_1to5_LowtoHigh, OffTask_1to5_LowtoHigh, Not_Responsible_1to5_LowtoHigh, Not_GetAlong_1to5_LowtoHigh) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
                 PreparedStatement myStmt = Connection.prepareStatement(query);     

                    myStmt.setString(1, "Chris");
                    myStmt.setString(2, "Johnson");
                    myStmt.setString(3, "Male");
                    myStmt.setString(4, "K");
                    myStmt.setString(5, "N");       
                    myStmt.setInt(6, 1);
                    myStmt.setInt(7, 3);
                    myStmt.setInt(8, 5);
                    myStmt.setInt(9, 2);
                    myStmt.addBatch();

                    int[] result = myStmt.executeBatch();

                    System.out.println(result + " were added to the database.");

    }
}
import java.sql.*;

public class Connection extends Alter {
    {
    try {

        Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();

    }

        catch (Exception ex) {

        }
    try {

        batchInsertRecordsIntoTable();

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    }
}

    final static void batchInsertRecordsIntoTable() throws SQLException {
        {
            try {

         DriverManager.getConnection(
                     "jdbc:mysql://localhost:3306/school?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
                       "root", "");

         System.out.println("Connection Successful");
}

     catch (SQLException ex) {
         ex.printStackTrace();
     }
        }
}

    public Statement createStatement() {
        return null;
    }

    public static PreparedStatement prepareStatement(String query) {
        return null;
    }
}
  • 1
    **Never** use ` catch (Exception ex) { }` atleast log exception – Jens Jan 20 '20 at 16:04
  • `Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance()` is entirely unnecessary. Even `Class.forName("com.mysql.cj.jdbc.Driver")` is not necessary in this context due to JDBC automatic driver loading. – Mark Rotteveel Jan 21 '20 at 09:51

1 Answers1

1

Your method prepareStatement returns null.Thats why you get a NPE when calling myStmt.setString(1, "Chris")

you have to store the return value of DriverManager.getConnection( in a private property and return the result of prepareStatement

 public static PreparedStatement prepareStatement(String query) {
        return myConnection.prepareStatement(query);
    }

Never use catch (Exception ex) { } atleast log exception

Jens
  • 67,715
  • 15
  • 98
  • 113