【问题标题】:Insert multiple rows from Jtable to sql database table从Jtable插入多行到sql数据库表
【发布时间】:2017-08-15 10:41:25
【问题描述】:

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

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

这是代码

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();

}

【问题讨论】:

  • 事务为保留字
  • “事务”这个词在大多数SQL数据库中是一个保留字,你必须引用它,比如"Transaction"...
  • 您使用的是什么 DBMS?
  • 我正在使用 microsoft sql server YCF_L 但它现在可以工作了,因为我从 Transaction 更改了表名

标签: java sql sql-server jdbc jtable


【解决方案1】:

Transaction 是 mysql 和许多其他 dbms 中的 keyword。不要将其用作表名或使用反引号将其转义

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2012-05-26
    • 1970-01-01
    • 2011-07-25
    • 2021-08-24
    相关资源
    最近更新 更多