[英]Integer won't save in SQL when inputting an integer
我是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)
我的問題是,當我在文本框中輸入整數時,它將不起作用。 我應該使用什么代碼將整數保存在數據庫中?
非常感謝你的幫助。 :)
編輯:表結構在這里。
屏幕截圖給出了答案(!)
您的列名“ limit”與一個SQL保留字沖突。 嘗試以下方法:
INSERT INTO roomlist (RoomNumber, RoomType, RoomCost,
`Limit`, Description) values (?,?,?,?,?)
(不同的SQL方言有不同的轉義關鍵字方式。這就是MySQL的方式...)
或更妙的是,更改列名稱。
如您在屏幕快照中所見,列名“ Limit”是一個關鍵字,因此可能是一個問題。 請更改您的列名稱為其他名稱。
嘿,你有個嚴重的錯誤,
INSERT INTO roomlist (RoomNumber, RoomType, RoomCost, Limit, Description) values (?,?,?,?,?)
眾所周知,我們可以將關鍵字用作一列,例如限制順序。因此,當您插入會議室列表時,限制列將被視為關鍵世界限制
解決此問題的方法很簡單,請更改列並記住不要再將關鍵字用作列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.