我使用java swing制作了一个库存系统。 我的代码工作正常,直到我将这些代码行添加到我的SAVE按钮中:
这应该是:
从表中获取GEt DATA并将其保存到数据库
将这些数据插入我的表IncomingTransactionITems
int rowCount=jTable3.getRowCount();
String[] ItemList=new String[6];
for(int rowctr=1;rowctr<rowCount;rowctr++){
ItemList[0]= String.valueOf(rowctr); // Incoming Transaction_Number
ItemList[1]= this.jTextField1.getText(); // Transaction ID
ItemList[2]=this.jTable3.getModel().getValueAt(rowctr, 0).toString(); // Item Number
ItemList[3]=this.jTable3.getModel().getValueAt(rowctr, 2).toString(); // Quantity
ItemList[4]=this.jTable3.getModel().getValueAt(rowctr, 3).toString(); // Unit Price
ItemList[5]=this.jTable3.getModel().getValueAt(rowctr, 4).toString(); // Total Price
// Save onto IncomingTransactionItems Table in Database
String sqlItems="INSERT INTO IncomingTransactionItems(Incoming_Transaction_Number, Transaction_ID,Item_Number, Quantity, Unit_Price,Total_Price)values('"
+ ItemList[0]+"','" // Incoming Transaction_Number
+ ItemList[1]+"','" // Transaction ID
+ ItemList[2]+"','" // Item Number
+ ItemList[3]+"','" // Quantity
+ ItemList[4]+"','" // Unit Price
+ ItemList[5]+"')"; // Total Price
try{
pst= conn.prepareStatement(sqlItems);
pst.execute();
this.UpdateJTable();
}
catch(SQLException | HeadlessException e){
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, e);
}
但是当我运行程序时它会抛出一个异常:
Inventory_System.Encoding_Incoming_Batch cmdADDMouseReleased
SEVERE: null
java.sql.SQLException: General error
我认为我的数据库可能是只读的,所以我检查了系统的其他功能(即ADD ITEMS,LOGS,Update PriceList等) 仍然工作正常。
我检查了我的表的传入事务编号 - 主键,如果我可能要复制一个重复的值,但它不是因为程序获取表中的值数并将其增加一个以获得新记录。< / p>
此行是问题的错误点
pst= conn.prepareStatement(sqlItems);
我错过了什么,或者我做错了什么?
修改
发布按钮的全部代码
if(cmdADD.isEnabled()){
//==================UPDATE ITEM COUNT and PRICE===================
int rowCountloop=jTable3.getRowCount();
float PreviousPrice;
float NewPrice;
for(int rowctr=1;rowctr<rowCountloop;rowctr++){
// get Item Count
int ItemQuantity=Integer.valueOf(this.jTable2.getModel().getValueAt(rowctr,1).toString());
int IncomingQuantity=Integer.valueOf(this.jTable3.getModel().getValueAt(rowctr, 2).toString());
// add incoming Items
int ItemTotal=ItemQuantity+IncomingQuantity;
PreviousPrice=(Float.valueOf(this.jTable2.getModel().getValueAt(rowctr,2).toString()));
NewPrice=(Float.valueOf(this.jTable3.getModel().getValueAt(rowctr, 3).toString()));
float newTotalPrice=((float) ((PreviousPrice*ItemQuantity)+(NewPrice*IncomingQuantity))/ItemTotal);
// convert values to 2 decimal
double PreviousPrice2 = Math.round(PreviousPrice*100.0)/100.0;
double NewPrice2 = Math.round(NewPrice*100.0)/100.0;
double newTotalPrice2 = Math.round(newTotalPrice*100.0)/100.0;
// Update Value of Quantity and Price
// String sqlUpdate= "Update allItems set Quantity='"+ItemTotal+"',Unit_Cost='"+NewPrice2+"',Total_Cost='"+newTotalPrice2+"' where Item_Number='"+this.jTable3.getModel().getValueAt(rowctr, 0).toString()+"'";
String sqlUpdate= "Update allItems set Quantity=?,Unit_Cost=?,Total_Cost=? where Item_Number='"+this.jTable3.getModel().getValueAt(rowctr, 0).toString()+"'";
try {
PreparedStatement pstUpdate= conn.prepareStatement(sqlUpdate);
pstUpdate.setInt(1, ItemTotal);
pstUpdate.setDouble(2, NewPrice2);
pstUpdate.setDouble(2, newTotalPrice2);
pstUpdate.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Encoding_Inventory_Batch.class.getName()).log(Level.SEVERE, null, ex);
}
//==================SAVE Price Change to PriceList==========================================
String sqlPriceChange="insert into PriceList(PriceID,ItemNumber,NewPrice,PreviousPrice,DateAndTime)values(?,?,?,?,?)";
try (PreparedStatement pstPriceChange = conn.prepareStatement(sqlPriceChange)){
pstPriceChange.setString(1, "PL"+ (PriceIDtxt.getText())+"-"+rowctr);
pstPriceChange.setString(2,this.jTable3.getModel().getValueAt(rowctr, 0).toString());
pstPriceChange.setDouble(3,NewPrice2);
pstPriceChange.setDouble(4,PreviousPrice2);
pstPriceChange.setString(5, (dateFormatwithtime.format(date)));
pstPriceChange.executeUpdate();
}
catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
}// end of loop
//==========================================================================================
int RowCounter=jTable3.getRowCount();
if(RowCounter<=1){
JOptionPane.showMessageDialog(this,"Please Input Data on Incoming List","System Message", JOptionPane.ERROR_MESSAGE);
}else{
int reply = JOptionPane.showConfirmDialog(null, "Confirm and SAVE?", "System Message", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (reply == JOptionPane.YES_OPTION) {
int answer = JOptionPane.showConfirmDialog(null, "Data Cannot be modified once it is SAVED, Continue?", "System Message", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
int rowCount=jTable3.getRowCount();
String[] ItemList=new String[6];
String sqlItems="INSERT INTO IncomingTransactionItems(Incoming_Transaction_Number, Transaction_ID,Item_Number, Quantity, Unit_Price,Total_Price)values(?,?,?,?,?,?)";
for(int rowctr=1;rowctr<rowCount;rowctr++){
ItemList[0]= String.valueOf(rowctr); // Incoming Transaction_Number
ItemList[1]= this.jTextField1.getText(); // Transaction ID
ItemList[2]=this.jTable3.getModel().getValueAt(rowctr, 0).toString(); // Item Number
ItemList[3]=this.jTable3.getModel().getValueAt(rowctr, 2).toString(); // Quantity
ItemList[4]=this.jTable3.getModel().getValueAt(rowctr, 3).toString(); // Unit Price
ItemList[5]=this.jTable3.getModel().getValueAt(rowctr, 4).toString(); // Total Price
// Save onto IncomingTransactionItems Table in Database
try (PreparedStatement pstInsert = conn.prepareStatement(sqlItems)){
// You could do this with a for-loop, I've done it long
// to demonstrate the use of PreparedStatement
pstInsert.setString(1, ItemList[0]);
pstInsert.setString(2, ItemList[1]);
pstInsert.setString(3, ItemList[2]);
pstInsert.setString(4, ItemList[3]);
pstInsert.setString(5, ItemList[4]);
pstInsert.setString(6, ItemList[5]);
pstInsert.executeUpdate();
}catch(SQLException | HeadlessException e){
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, e);
}
String sql="INSERT INTO Incoming_Transactions (Transaction_ID, Date_Delivered, Ref_No, PO_No,JO_No, Supplier_Code,Notes, Status, Time_Stamp )\n" +
"VALUES (?,?,?,?,?,?,?,?,?)";
try (PreparedStatement pstTransactions = conn.prepareStatement(sql)){
pstTransactions.setString(1, (this.TransactionNotxt.getText()));
pstTransactions.setString(2, this.DateDeliveredChooser.getDate().toString());
pstTransactions.setString(3,this.ReferenceTxt.getText());
pstTransactions.setString(4,this.POtxt.getText());
pstTransactions.setString(5, this.JOtxt.getText());
pstTransactions.setString(6, this.SupplierCodetxt.getText());
pstTransactions.setString(7, this.NoteTextArea.getText());
pstTransactions.setString(8, "Processed");
pstTransactions.setString(9, dateFormatwithtime.format(date));
pstTransactions.execute();
}
catch(SQLException | HeadlessException e){
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, e);
}
// update Table to show changes
this.UpdateJTable();
}
JOptionPane.showMessageDialog(null, "Transaction Saved");
this.cmdADD.setEnabled(false);
this.cmdDELETE.setEnabled(false);
CountDataPriceList(); // Increment Price List number
}
}
}
}
这是完整错误
run:
Jul 16, 2015 1:19:31 PM Inventory_System.Encoding_Incoming_Batch cmdADDMouseReleased
SEVERE: null
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6993)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3156)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:215)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPreparedStatement.java:137)
at Inventory_System.Encoding_Incoming_Batch.cmdADDMouseReleased(Encoding_Incoming_Batch.java:1240)
at Inventory_System.Encoding_Incoming_Batch.access$3600(Encoding_Incoming_Batch.java:44)
at Inventory_System.Encoding_Incoming_Batch$30.mouseReleased(Encoding_Incoming_Batch.java:915)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Jul 16, 2015 1:19:31 PM Inventory_System.Encoding_Incoming_Batch cmdADDMouseReleased
SEVERE: null
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3156)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:215)
at Inventory_System.Encoding_Incoming_Batch.cmdADDMouseReleased(Encoding_Incoming_Batch.java:1278)
at Inventory_System.Encoding_Incoming_Batch.access$3600(Encoding_Incoming_Batch.java:44)
at Inventory_System.Encoding_Incoming_Batch$30.mouseReleased(Encoding_Incoming_Batch.java:915)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD STOPPED (total time: 1 minute 45 seconds)
答案 0 :(得分:1)
PreparedStatement
与位置标记(?
)一起用于实际值,这有助于防止SQL注入攻击,但也允许您少考虑传递不同类型所需的编码值(例如int
,double
和Date
)。
因此,您不是在sqlItems
中指定实际值,而是提供带占位符的抽象查询,例如......
String sqlItems="INSERT INTO IncomingTransactionItems(Incoming_Transaction_Number, Transaction_ID,Item_Number, Quantity, Unit_Price,Total_Price)values(?,?,?,?,?, ?)"
然后&#34;绑定&#34;语句的每一列的值......
pst.setString(1, ItemList[0]);
1
表示第一个通配符,2
表示第二个通配符,依此类推等等(请记住,这不是0
索引,而是1
索引,其中1
是可能的最低值。
所以,它可能看起来更像......
int rowCount=jTable3.getRowCount();
String[] ItemList=new String[6];
String sqlItems="INSERT INTO IncomingTransactionItems(Incoming_Transaction_Number, Transaction_ID,Item_Number, Quantity, Unit_Price,Total_Price)values(?,?,?,?,?, ?)"
for(int rowctr=1;rowctr<rowCount;rowctr++){
ItemList[0]= String.valueOf(rowctr); // Incoming Transaction_Number
ItemList[1]= this.jTextField1.getText(); // Transaction ID
ItemList[2]=this.jTable3.getModel().getValueAt(rowctr, 0).toString(); // Item Number
ItemList[3]=this.jTable3.getModel().getValueAt(rowctr, 2).toString(); // Quantity
ItemList[4]=this.jTable3.getModel().getValueAt(rowctr, 3).toString(); // Unit Price
ItemList[5]=this.jTable3.getModel().getValueAt(rowctr, 4).toString(); // Total Price
try (PreparedStatement pst = conn.prepareStatement(sqlItems)){
// You could do this with a for-loop, I've done it long
// to demonstrate the use of PreparedStatement
pst.setString(1, ItemList[0]);
pst.setString(2, ItemList[1]);
pst.setString(3, ItemList[2]);
pst.setString(4, ItemList[3]);
pst.setString(5, ItemList[4]);
pst.setString(6, ItemList[5]);
pst.executeUpdate();
}
catch(SQLException | HeadlessException e){
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, e);
}
}
this.UpdateJTable();
这也意味着你可以做更像......的事情。
pst.setLong(1, rowctr);
pst.setString(2, (String)this.jTable3.getModel().getValueAt(rowctr, 0));
pst.setInt(3, (int)this.jTable3.getModel().getValueAt(rowctr, 1));
pst.setInt(4, (int)this.jTable3.getModel().getValueAt(rowctr, 2));
pst.setDouble(5, (double)this.jTable3.getModel().getValueAt(rowctr, 3));
pst.setDouble(6, (double)this.jTable3.getModel().getValueAt(rowctr, 4));
使用所需的数据类型为列值设定种子(我在这里猜测实际类型)
您还应该调用executeUpdate
而不是execute
,它可以为您提供有关操作的更多信息(例如受影响的行数)
有关详细信息,请查看Using Prepared Statements
我检查了我的表的传入事务编号 - 主键,如果我可能要复制一个重复的值,但它不是因为程序获取表中的值数并将其增加一个以获得新记录。< / p>
如果可能的话,使用自动增量键并且不在insert语句中包含主键是明智的,这可以克服可能的线程竞争条件
根据Java General Error On Insert...???中的信息,您可能在当前连接中有未提交的更改,这会导致后续更新失败的问题
假设Connection
设置为autoCommit
,您只需使用try-with-resources
块自动关闭连接(以及PreparedStatement
)
try (Connection conn = ) {
String query = "...";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
if (stmt.executeUpdate() == 0) {
System.err.println("Possible error with update, did not update any rows");
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
否则您应手动commit
更新
try (Connection conn = ) {
String query = "...";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
if (stmt.executeUpdate() == 0) {
System.err.println("Possible error with update, did not update any rows");
} else {
conn.commit();
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
答案 1 :(得分:0)
基于MadProggrammer's我使用适当的准备语句重新创建了我的代码,我还使用了在JDBC中批量插入
经过多次试验和错误后,我发现此行会导致常规错误问题:
pst.setString(1, this.jTextField1.getText());
我将数据保存在 SAME Incoming_Transaction_Number中,这是IncomingTransactionNumber表的主键。这会导致一般错误。
所以我将Incoming_Transaction_Number列更改回自动编号,让数据库为我编号。
感谢MadProggrammer帮助我的人。所以我会接受他的答案。并发表了我的答案以澄清。
public void saveData() throws SQLException{
//==========================================================================================
// save Incoming Transaction Items based on Transaction ID
conn=Database.getConnection();
String sqlItems="INSERT INTO IncomingTransactionItems( Transaction_ID,Item_Number, Quantity, Unit_Price,Total_Price)values(?,?,?,?,?)";
// Save onto IncomingTransactionItems Table in Database
pst = conn.prepareStatement(sqlItems);
try{
int rowctr=jTable3.getRowCount();
for(int row=1;row<rowctr;row++){
// pst.setString(1, this.jTextField2.getText()); // removed this line and set autonumber in my database
pst.setString(1, (this.TransactionNotxt.getText()));
pst.setString(2, this.jTable3.getModel().getValueAt(row, 0).toString());
pst.setString(3, this.jTable3.getModel().getValueAt(row, 2).toString());
pst.setString(4, this.jTable3.getModel().getValueAt(row, 3).toString());
pst.setString(5, this.jTable3.getModel().getValueAt(row, 4).toString());
pst.addBatch();
this.jTextField2.setText(String.valueOf(Integer.valueOf(this.jTextField2.getText())+1));
}
pst.executeBatch();
}catch(SQLException | HeadlessException e){
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, e);
}finally{
try {
pst.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Encoding_Incoming_Batch.class.getName()).log(Level.SEVERE, null, ex);
}
}
}