Java JDBC Derby语法错误

问题描述 投票:-1回答:2

嗨,我的java代码中的语法有问题。我有一个tableview从SQL数据库获取其数据。我在数据库bookcustomerorder中创建了3个表。当我单击一个按钮时,我想要选择书籍并将它们添加到order表中。 这是主程序的代码(从db调用方法):

if(table.getSelectionModel().getSelectedItems().iterator().hasNext()) { 
                    db.insertOrder(new Bestellung(customerid,table.getSelectionModel().getSelectedItems().iterator().next())); 

book是固定的。只是另外两张桌子customerorder是动态的。

问题:

我像这样在order表中创建值

String ct = "CREATE TABLE Order (" + "Order_Id integer generated always as identity, " + "CUSTOMER_ID BIGINT" + "ISBN, CHAR(13) " + "PRIMARY KEY(Order_Id))";

等等...

我像这样插入order表。 (这是String i中的语法问题。这是编译器说它不起作用的位置..)

    String i = "INSERT INTO ORDER(CUSTOMER_ID,ISBN), VALUES(?,?)";
    Connection conn = null; 
    PreparedStatement stmt = null; 
    try {
        conn = DriverManager.getConnection(connString);
        stmt = conn.prepareStatement(i);
        conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        stmt.setLong(1, order.getCustomerId());
        stmt.setString(2, order.getBuch().getISBN());
        stmt.executeUpdate();  
    } catch (SQLException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    finally {

        try {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

这是我得到的语法错误

语法错误:在第1行第13列遇到“ORDER”。

那么如何在string i中更正我的语法?有没有人有任何想法?

java sql
2个回答
1
投票
INSERT INTO ORDER(CUSTOMER_ID,ISBN), VALUES(?,?)
                                   ^

逗号是多余的。另外,在你的CREATE TABLE

... + "ISBN, CHAR(13) " + ...
           ^

这个逗号也是无关紧要的。


0
投票

如果要将保留字/关键字用作表名,则应该:

  1. MySQL:使用',如:select * from 'Order'
  2. Oracle,PostgreSQL:使用“,如:select * from "Order"

但这是不好的做法,尝试更改您的表名。

你有另一个由@Jim Garrison回答的错误。

© www.soinside.com 2019 - 2024. All rights reserved.