2015-10-06 180 views
0

我試圖更新我的JTable(變化值,按回車鍵),我得到的錯誤。由於大小錯誤未滿。我認爲剩下的部分只是從c3p0工具生成的無關信息,用於彙集連接。MySQLSyntaxErrorException神祕

推定起初,我認爲這可能是c3p0的問題,原因是爲了將它用於準備好的語句,我將不得不編輯配置文件/更改設置。但是我使用DriverManager的傳統get連接方式來發現它與它無關。

另一種假設 - 是一些數據類型的問題。因爲我輸入3.00並且錯誤顯示爲''Flat' = '3.0' where ID = '4'' at line 1 ImportData方法中的文本測試行未執行。所以這意味着它在執行SQL語句時崩潰。在我的數據庫表中除ID和日期以外的列的類型是float(9,2),這意味着總數位= 9;數字「之後點」= 2。我有DecimalFormatRenderer類可能與此有關。

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 ''Flat' = '3.0' where ID = '4'' 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:377) 
at com.mysql.jdbc.Util.getInstance(Util.java:360) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062) 
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105) 
at components.TableWithBottomLine$ImportData.<init>(TableWithBottomLine.java:174) 
at components.TableWithBottomLine.tableChanged(TableWithBottomLine.java:147) 
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source) 
at javax.swing.table.AbstractTableModel.fireTableCellUpdated(Unknown Source) 
at components.TableWithBottomLine$MyTableModel.setValueAt(TableWithBottomLine.java:251) 
at javax.swing.JTable.setValueAt(Unknown Source) 
at javax.swing.JTable.editingStopped(Unknown Source) 
at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source) 
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source) 
at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source) 
at javax.swing.JTable$GenericEditor.stopCellEditing(Unknown Source) 
at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(Unknown Source) 
at javax.swing.JTextField.fireActionPerformed(Unknown Source) 
at javax.swing.JTextField.postActionEvent(Unknown Source) 
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source) 
at javax.swing.SwingUtilities.notifyAction(Unknown Source) 
at javax.swing.JComponent.processKeyBinding(Unknown Source) 
at javax.swing.JComponent.processKeyBindings(Unknown Source) 
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(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$400(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$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.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$1.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) 

從我的程序中的代碼摘錄:

public void tableChanged(TableModelEvent e) { 
    int row = e.getFirstRow(); 
    int col = e.getColumn(); 
    model = (MyTableModel) e.getSource(); 
    String colName = model.getColumnName(col); 
    Object cellValue = model.getValueAt(row, col);   
    Object cell_Id = model.getValueAt(row, 0); 

    try { 
     new ImportData(colName, cellValue, cell_Id); 
     bottomLabel.setText(textForLabel()); 
    } catch (ClassNotFoundException e1) { 
     e1.printStackTrace(); 
    } catch (SQLException e1) { 
     e1.printStackTrace(); 
    } 
} 

public class ImportData {  

    public ImportData(String a, Object b, Object c) 
      throws ClassNotFoundException, SQLException { 
     Connection connection = null; 
     PreparedStatement prepStmt = null;  

     try { 
      connection = ConnectionManager.getConnection(); 
      String colName = a;    
      String cellValue = b.toString();     
      String cell_Id = c.toString();      

      String updateString = "update finance.fin " + "set ? = ? " + "where ID = ? "+ ";"; 
      prepStmt = connection.prepareStatement(updateString); 
      prepStmt.setString(1, colName); 
      prepStmt.setString(2, cellValue); 
      prepStmt.setString(3, cell_Id);    
      prepStmt.executeUpdate(); 

      System.out.println("Text test line out of ImportData() method"); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      closeAll(null, null, prepStmt, connection); 
     } 
    } 
} 

回答

0

羅埃爾德從http://coderanch.com 奈斯回答了這個問題。 說實話,我發現你的代碼有3個問題。首先也是最重要的:我不認爲你可以在準備好的聲明中做這樣的事情"set ? = ? "我認爲列名不能用佔位符替換。嘗試使用"set Flat = ? "並查看它是否有效。如果是這樣,你知道是什麼導致了這個問題。 另外兩個是你將參數設置爲String,而它們不是字符串,但可能是double和int。因此,更改代碼

prepStmt.setString(1, colName); prepStmt.setString(2, cellValue); prepStmt.setString(3, cell_Id);

// column name not dynamic for test prepStmt.setDouble(1, cellValue); prepStmt.setInt(2, cell_Id);

,看看這是否正常工作。你當然應該確保你的變量有適當的類型。 PS。不建議在變量名中使用下劃線,根據命名約定,它應該是camelCase。下面的工作代碼:

public class ImportData {  

    public ImportData(String a, Object b, Object c) 
      throws ClassNotFoundException, SQLException { 
     Connection connection = null; 
     PreparedStatement prepStmt = null;   
     try { 
      connection = ConnectionManager.getConnection(); 
      String colName = a;    
      // String cellValue = b.toString(); 
      // String cellId = c.toString(); 
      float cellValue = (float) b; 
      int cellId = (int) c;    
      String updateString = "update finance.fin " + "set " + colName + "= ? " + "where ID = ? "+ ";"; 
      prepStmt = connection.prepareStatement(updateString); 
      prepStmt.setFloat(1, cellValue); 
      prepStmt.setInt(2, cellId);    
      prepStmt.executeUpdate(); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      closeAll(null, null, prepStmt, connection); 
     } 
    } 
}