从Jtable到sql数据库表插入多行

时间:2022-10-12 01:58:39

I'm trying to insert rows data from JTable to table in sql database but on runtime facing error

我正在尝试将行数据从JTable插入到sql数据库中的表中,但是在运行时面临错误

Incorrect syntax near the keyword 'Transaction'.

关键字“交易”附近的语法不正确。

Here is the code

这是代码

try {

    int rows = table.getRowCount();

    //  con.setAutoCommit(false);
    String query = "Insert into Transaction(transaction_code, transaction_date, item_code, item_name, quantity, item_price, total) values (?,?,?,?,?,?,?) ;";
    PreparedStatement pst = con.prepareStatement(query);
    for (int row = 0; row < rows; row++) {
        int t_code = (int) table.getValueAt(row, 0);
        Timestamp t_date = (Timestamp) table.getValueAt(row, 1);
        int i_code = (int) table.getValueAt(row, 2);
        String i_name = (String) table.getValueAt(row, 3);
        int quantity = (int) table.getValueAt(row, 4);
        BigDecimal i_price = (BigDecimal) table.getValueAt(row, 5);
        BigDecimal total = (BigDecimal) table.getValueAt(row, 6);
        pst.setInt(1, t_code);
        pst.setTimestamp(2, t_date);
        pst.setInt(3, i_code);
        pst.setString(4, i_name);
        pst.setInt(5, quantity);
        pst.setBigDecimal(6, i_price);
        pst.setBigDecimal(7, total);

        pst.addBatch();
    }
    pst.executeBatch();
    pst.execute(query);
    //con.commit();
} catch (Exception e1) {
    e1.printStackTrace();

}

1 个解决方案

#1


0  

Transaction is a keyword in mysql an many other dbms. Do not use it as tablename or escape it with backticks

Transaction是mysql中许多其他dbms的关键字。不要将它用作tablename或使用反引号将其转义

#1


0  

Transaction is a keyword in mysql an many other dbms. Do not use it as tablename or escape it with backticks

Transaction是mysql中许多其他dbms的关键字。不要将它用作tablename或使用反引号将其转义