簡體   English   中英

在JAVA中使用帶有MS Access 2010數據庫的Update語句

[英]Using Update statement with MS Access 2010 database in JAVA

嗨,我正在開發一個保險領域的小應用程序。 我在程序中使用update語句時收到錯誤。

錯誤是net.ucanaccess.jdbc.UcanaccessSQLException:意外令牌:HALF java.lang.NullPointerException

代碼是

btnUpdate = new JButton("UPDATE");
btnUpdate.setMnemonic('U');
btnUpdate.setFont(new Font("Times New Roman", Font.BOLD, 11));
GridBagConstraints gbc_btnUpdate = new GridBagConstraints();
gbc_btnUpdate.insets = new Insets(0, 0, 5, 5);
gbc_btnUpdate.gridx = 3;
gbc_btnUpdate.gridy = 3;
contentPane.add(btnUpdate, gbc_btnUpdate);
btnUpdate.setVisible(false);
btnUpdate.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        Statement stmt = null;
        ResultSet rset = null;
        Calendar currcal = Calendar.getInstance();
        SimpleDateFormat df;
        df = new SimpleDateFormat("dd-MM-yyyy");
        Date getcurrdate = currcal.getTime();
        String currdate = df.format(getcurrdate);       
        System.out.println(getModeID + "," + getModeofPaymentDescription + "," + getModeofPaymentType + "," + currdate);
        try {
            getModeofPaymentDescription=txt_Mode_Of_Payment_Description.getText().toUpperCase();
            getModeofPaymentType=txt_Mode_Of_Payment_Type.getText().toUpperCase();

            stmt = dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//ProjectJAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").createStatement();
            stmt.executeUpdate("update Mode_Of_Payment_Profile set Mode_Of_Payment_Profile_Type='" + getModeofPaymentType + "'"
                            + "',Mode_Of_Payment_Profile_Description='" + getModeofPaymentDescription + "',Mode_Of_Payment_Profile_Creation_Date='" + currdate + "'"
                            + " where Mode_Of_Payment_Profile_ID='" + getModeID + "'");
        } catch (Exception e) {
            //JOptionPane.showMessageDialog(null, "Database Error", "Error Message", JOptionPane.OK_OPTION);
            System.out.println(e);
        }

        txt_Mode_Of_Payment_Description.setText("");
        txt_Mode_Of_Payment_Type.setText("");
        btnAdd.setEnabled(true);
        btnModify.setVisible(true);
        btnUpdate.setVisible(false);
        txt_Mode_Of_Payment_Description.requestFocus();

        try {
            stmt.close();
            rset.close();
            dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
});

在構建SQL字符串的行中有拼寫錯誤。

在您輸入ModeofPaymentType值的行末尾有一個引號:

"update Mode_Of_Payment_Profile set Mode_Of_Payment_Profile_Type='"+getModeofPaymentType+"'"

以下行以。開頭

+ "',Mode_Of_Payment_Profile_Description='"

導致在modeofPaymentType值之后插入額外的單引號。 生成的SQL看起來像

update Mode_Of_Payment_Profile set Mode_Of_Payment_Profile_Type='mode'',Mode_Of_Payment_Profile_Description='FOO'
,Mode_Of_Payment_Profile_Creation_Date='15-07-2015'
where Mode_Of_Payment_Profile_ID='someid'

兩個相鄰的單引號被視為轉義單引號,因此解析器認為文字字符串是“mode”,Mode_Of_Payment_Profile_Description =“,然后它認為下一個標記是您為Mode_Of_Payment_Profile_Description傳入的任何值。

如果你更換使用java.sql.Statementjava.sql.PreparedStatement那么你的更新可能是這樣的:

String connectParams = "//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb";
Connection connection = dbcon.DB_Connection(connectParams);
PreparedStatement ps = connection.prepareStatement(
    "update Mode_Of_Payment_Profile set" 
    + " Mode_Of_Payment_Profile_Type=?"
    + ", Mode_Of_Payment_Profile_Description=?"
    + ", Mode_Of_Payment_Profile_Creation_Date=?"
    + " where Mode_Of_Payment_Profile_ID=?");
ps.setString(1, getModeofPaymentType);
ps.setString(2, getModeofPaymentDescription);
ps.setString(3, currdate); 
ps.setString(4, getModeID);

這樣,您就不必執行容易出錯的操作,例如使用字符串連接插入參數並自行處理引用。 它減少了SQL注入的機會(因為PreparedStatement將查找嵌入的轉義字符)並且更具可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM