我正在尝试将用户输入到jtextfield的数据插入到msaccess数据库中。当我尝试执行我的sql语句时,我在INSERT INTO语句中收到一条错误,指出语法错误。 我检查了我的sql语句并尝试了一些不同的东西,但似乎无法找到任何语法错误。
conn = Connect.ConnectDB();
String sql = "insert into Team ("
+"TeamID,"
+"TeamCity,"
+"TeamMascot,"
+ "values("+txtid.getText()+ ",'"+txtname.getText()+"','"+txtaddress.getText()+"')" ;
try{
pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Entry " + txtid.getText() + " Saved");
UpdateJTable();
//conn.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
答案 0 :(得分:0)
错误是额外逗号,并且在关键字values
之前没有右括号
String sql = "insert into Team ("
+"TeamID,"
+"TeamCity,"
+"TeamMascot," // <<== HERE, change comma into closing parenthesis
顺便说一句,您的陈述容易被SQL Injection
攻击。如果参数化了值,则可以避免使用它。例如,
String sql = "insert into Team (TeamID,TeamCity,TeamMascot) values (?, ?, ?, ?)"
pst = conn.prepareStatement(sql);
pst.setString(1, txtid.getText());
pst.setString(2, txtname.getText());
pst.setString(3, txtaddress.getText());