【问题标题】:Integer won't save in SQL when inputting an integer输入整数时,整数不会保存在 SQL 中
【发布时间】:2016-07-30 06:13:56
【问题描述】:

我是一名 Java 程序员。我对 Java 很陌生。我在保存数据库时遇到问题。这是我的代码:

JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
    try{
        String query = "INSERT INTO roomlist (RoomNumber, RoomType, RoomCost, Limit, Description) values (?,?,?,?,?)";
        PreparedStatement pst = conn.prepareStatement(query);

        pst.setString(1, roomNoTextField.getText());
        pst.setString(2, roomTypeTextField.getText());
        pst.setInt(3, Integer.parseInt(roomCostTextField.getText())); //<--This is my problem
        pst.setInt(4, Integer.parseInt(limitTextField.getText()));//<--This is my problem
        pst.setString(5, descriptionTextArea.getText());

        pst.execute();

        JOptionPane.showMessageDialog(null, "Data Saved");

        }catch(Exception e){
            e.printStackTrace();
        }
    }
});
btnSave.setBounds(32, 300, 98, 26);
contentPane.add(btnSave);

这是我遇到的错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Limit, Description) values ('101','Deluxe',350,2,'')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at hotelBillingAndReservation.addRoom$2.actionPerformed(addRoom.java:115)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我的问题是当我在文本框中输入一个整数时,它不起作用。我应该使用什么代码将整数保存在数据库中?

非常感谢您的帮助。 :)

编辑:表结构在这里。

【问题讨论】:

  • 你的表结构是什么?
  • RoomNumber - TEXT,RoomType - TEXT,RoomCost - INT,Limit - INT,描述 - TEXT
  • 你的问题不在你想的地方。看看错误。它在 SQL 中,而不是在您的 Java 代码中。限制是一个保留字,你必须转义它。
  • 哦。对于那个很抱歉。感谢我的朋友提供此信息:)

标签: java mysql integer


【解决方案1】:

您的屏幕截图给出了答案 (!)

您的列名“limit”与 SQL 保留字冲突。试试这个:

INSERT INTO roomlist (RoomNumber, RoomType, RoomCost, 
                      `Limit`, Description) values (?,?,?,?,?)

(不同的SQL方言有不同的关键字转义方式,就是MySQL的方式……)

或者更好的是,更改列名。

【讨论】:

  • 我明白了。太感谢了。 :)
【解决方案2】:

正如您在屏幕截图中看到的那样,列名“限制”是一个关键字,因此它可能是一个问题。请将您的列名更改为其他名称。

【讨论】:

  • 这就是 RoomNumber - TEXT、RoomType - TEXT、RoomCost - INT、Limit - INT、Description - TEXT
  • @jhenryj09 - 你怎么知道的?
【解决方案3】:

你有一个严重的错误,

INSERT INTO roomlist (RoomNumber, RoomType, RoomCost, Limit, Description) values (?,?,?,?,?)

众所周知,我们可以使用关键字作为列,例如限制订单。所以当您插入房间列表时,列限制将被视为关键世界限制

解决这个问题的方法很简单,更改列并记住不要再使用关键字作为列

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2015-09-24
    • 2018-06-08
    相关资源
    最近更新 更多