簡體   English   中英

嘗試使用SQL插入時獲取NullPointerException

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

我一直在嘗試使用Java將值插入數據庫中。 這是我第一次嘗試的代碼。

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

        }

我使用testPrint方法進行了檢查,以查看string命令中是否存在問題。 但是這種方法效果很好。 executeInsert沒有。 這是我使用的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;


    }   
}

既然那行不通,我瀏覽了一些教程,並了解了PreparedStatement 我嘗試如下使用(沒有任何其他方法):

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

        }

現在,我有一個小小的預感,就是我的Connection conn = null是問題。 但是我不知道還要設置什么。 老實說,我已經盡力了,我仍然對Java中的SQL連接部分還是陌生的。所以你們可以提供的任何幫助或暗示都將提供很大的幫助。

謝謝。

編輯:這是連接方法。

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

您錯過了打開數據庫連接初始化步驟。

例如,

// 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