简体   繁体   English

尝试使用SQL插入时获取NullPointerException

[英]Getting a NullPointerException when trying to insert using SQL

I've been trying to insert values into my database using Java. 我一直在尝试使用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. 我使用testPrint方法进行了检查,以查看string命令中是否存在问题。 But that method worked fine. 但是这种方法效果很好。 The executeInsert did not. executeInsert没有。 Here is the executeInsert method i used: 这是我使用的executeInsert方法:

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 . 既然那行不通,我浏览了一些教程,并了解了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. 现在,我有一个小小的预感,就是我的Connection conn = null是问题。 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. 老实说,我已经尽力了,我仍然对Java中的SQL连接部分还是陌生的。所以你们可以提供的任何帮助或暗示都将提供很大的帮助。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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