简体   繁体   中英

Getting a NullPointerException when trying to insert using SQL

I've been trying to insert values into my database using Java. Here is the code that i first tried.

Connection conn = null;
        if(action.getSource() == btnAddItem) 
        {
            String command =
                    "INSERT INTO item VALUES (" + itemNo + ", " 
                            + itemName + ", " 
                            + costprice + ", " 
                            + sellingprice + ", " 
                            + stock + ")";

            try 
            {
                this.executeInsert(conn, command); //works
                //testPrint(command);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }

        }

I checked saperately using the testPrint method to see if there was an issue in the string command. But that method worked fine. The executeInsert did not. Here is the executeInsert method i used:

public boolean executeInsert(Connection connection, String command) throws SQLException
{
        try
        {
          Statement st = connection.createStatement();
          st.executeUpdate(command);
          connection.close();
        }
        catch (Exception e)
        {
          System.err.println("Got an exception!");
          System.err.println(e.getMessage());
          e.printStackTrace();
        }
        return false;


    }   
}

Since that didn't work i went through a few tutorials and found out about PreparedStatement . I tried using that as follows (without any other methods):

Connection conn = null;
        if(action.getSource() == btnAddItem) 
        {
            try 
            {
            String command = "INSERT INTO item VALUES (?, ?, ?, ?, ?)";


             PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement(command);
             preparedStmt.setString (1, itemNo);
             preparedStmt.setString (2, itemName);
             preparedStmt.setDouble (3, costprice);
             preparedStmt.setDouble (4, sellingprice);
             preparedStmt.setInt    (5, stock);


             preparedStmt.execute();
            // conn.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }

        }

Now, i have a small hunch that my Connection conn = null is the problem. But i cannot figure out what else to set it to. I've honestly tried everything i can at this point, and im still new to SQL connection part in java.. so any help or hint you guys can provide is of great help.

Thank you.

EDIT: Here is the connection method.

public Connection getConnection() throws SQLException 
{
    Connection conn = null;
    Properties connectionProps = new Properties();
    connectionProps.put("user", this.userName);
    connectionProps.put("password", this.password);

    conn = DriverManager.getConnection("jdbc:mysql://"
            + this.serverName + ":" + this.portNumber + "/" + this.dbName,
            connectionProps);

    return conn;
}

You have missed to open Database connection initialization step.

For example,

// JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

//  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

// Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(DB_URL,USER,PASS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM