嗨,我的java代码中的语法有问题。我有一个tableview
从SQL数据库获取其数据。我在数据库book
,customer
,order
中创建了3个表。当我单击一个按钮时,我想要选择书籍并将它们添加到order
表中。
这是主程序的代码(从db调用方法):
if(table.getSelectionModel().getSelectedItems().iterator().hasNext()) {
db.insertOrder(new Bestellung(customerid,table.getSelectionModel().getSelectedItems().iterator().next()));
表book
是固定的。只是另外两张桌子customer
,order
是动态的。
问题:
我像这样在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
中更正我的语法?有没有人有任何想法?
INSERT INTO ORDER(CUSTOMER_ID,ISBN), VALUES(?,?)
^
逗号是多余的。另外,在你的CREATE TABLE
... + "ISBN, CHAR(13) " + ...
^
这个逗号也是无关紧要的。
如果要将保留字/关键字用作表名,则应该:
select * from 'Order'
select * from "Order"
但这是不好的做法,尝试更改您的表名。
你有另一个由@Jim Garrison回答的错误。