英文:
Why does my code not fill a table in database?
问题
以下是翻译好的部分:
第一段代码:
这段代码可以正常工作:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class ItemGroupInputFromConsole extends Parser {
public ItemGroupInputFromConsole() {
}
@Override
public void visit(ItemGroup itemGroup) throws SQLException {
System.out.println("ItemGroup String id, String group_name");
super.readRawInput();
String[] rawinput = super.getRawInput();
itemGroup.setId(rawinput[0]);
itemGroup.setGroup_name(rawinput[1]);
String SQLinsert = "INSERT INTO \"Item_Group\"(id,group_name) "
+ "VALUES(?,?)";
PreparedStatement pstmt = super.connection.prepareStatement(SQLinsert, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, itemGroup.getId());
pstmt.setString(2, itemGroup.getGroup_name());
pstmt.executeUpdate();
}
第二段代码:
当我尝试几乎相似的代码时,会产生运行时错误:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class UserInputFromConsole extends Parser {
public UserInputFromConsole(){
}
@Override
public void visit(User user) throws SQLException {
System.out.println("User String id, String name, int age, String adress, int account, String bad_behaviour");
super.readRawInput();
String[] rawinput = super.getRawInput();
user.setId(rawinput[0]);
user.setName(rawinput[1]);
user.setAge(rawinput[2]);
user.setAdress(rawinput[3]);
user.setAccount(rawinput[4]);
user.setBad_behaviour(rawinput[5]);
String SQLinsert = "INSERT INTO user(id,name,age,adress,account,bad_bahaviour) "
+ "VALUES(?,?,?,?,?,?)";
PreparedStatement pstmt = super.connection.prepareStatement(SQLinsert, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getAge());
pstmt.setString(4, user.getAdress());
pstmt.setString(5, user.getAccount());
pstmt.setString(6, user.getBad_behaviour());
pstmt.executeUpdate();
}
}
关于错误问题:
第一个代码工作正常,第二个代码在 pstmt.executeUpdate();
这一行产生错误。您需要在哪里调试以找到这个错误呢?也许是在数据库吗?
为什么会发生这种情况呢?错误是 "user" 中的语法错误,但第一个代码中的问题在 pstmt.executeUpdate();
这一行。
英文:
The following code works:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class ItemGroupInputFromConsole extends Parser{
public ItemGroupInputFromConsole() {
}
@Override
public void visit(ItemGroup itemGroup) throws SQLException{
System.out.println("ItemGroup String id, String group_name");
super.readRawInput();
String[] rawinput = super.getRawInput();
itemGroup.setId(rawinput[0]);
itemGroup.setGroup_name(rawinput[1]);
String SQLinsert = "INSERT INTO \"Item_Group\"(id,group_name) "
+ "VALUES(?,?)";
PreparedStatement pstmt = super.connection.prepareStatement(SQLinsert, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, itemGroup.getId());
pstmt.setString(2, itemGroup.getGroup_name());
pstmt.executeUpdate();
}
When I try almost similar code it produces a runtime error:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class UserInputFromConsole extends Parser {
public UserInputFromConsole(){
}
@Override
public void visit(User user) throws SQLException {
System.out.println("User String id, String name, int age, String adress, int account, String bad_behaviour");
super.readRawInput();
String[] rawinput = super.getRawInput();
user.setId(rawinput[0]);
user.setName(rawinput[1]);
user.setAge(rawinput[2]);
user.setAdress(rawinput[3]);
user.setAccount(rawinput[4]);
user.setBad_behaviour(rawinput[5]);
String SQLinsert = "INSERT INTO user(id,name,age,adress,account,bad_bahaviour) "
+ "VALUES(?,?,?,?,?,?)";
PreparedStatement pstmt = super.connection.prepareStatement(SQLinsert, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getAge());
pstmt.setString(4, user.getAdress());
pstmt.setString(5, user.getAccount());
pstmt.setString(6, user.getBad_behaviour());
pstmt.executeUpdate();
}}
The error is on the line pstmt.executeUpdate(); in the second code. The first code works, the second produces an error.
Where do I have to debug, in order to find this error?
Maybe in the database?
Why is this happening?
The error is syntax error in user but the first one is on line with
pstmt.executeUpdate().
答案1
得分: 3
"user"是一个保留关键字,您需要使用双引号 - 或者最好找一个不同的名称:
String SQLinsert = "INSERT INTO \"user\" (id,name,age,address,account,bad_behavior) "
+ "VALUES(?,?,?,?,?,?)";
英文:
user
is a reserved keyword, you need to use double quotes - or better find a different name:
String SQLinsert = "INSERT INTO \"user\" (id,name,age,adress,account,bad_bahaviour) "
+ "VALUES(?,?,?,?,?,?)";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论