当我尝试添加具有一个属性名称的学生时出现错误。 错误是:尝试将学生添加到数据库时出错
您的SQL语法错误;检查对应于你的MySQL服务器版本在第1行
错误指出此代码使用近“”名“)”正确的语法手册,
//Class DAL
public int addStudent(String name)throws SQLException{
PreparedStatement studentStatement = getConnected().prepareStatement("insert into person" + "values (?)") ;
studentStatement.setString(1,name);
//This is highlighted by the error
int rSetStudent = studentStatement.executeUpdate();
return rSetStudent;
}
//Class controller
public int addStudent(String name) throws SQLException{
//This is highlighted
return dal.addStudent(name);
}
//and this in class MainFrame where i use the add method from controller class
//this is highlighted
private void addStudent(){
try{
//and this
int a = controller.addStudent(nameTxtField.getText().trim());
}catch(SQLException e){
e.printStackTrace();
}
}
的方法addStudent()
当按钮添加被点击时被调用。 如果有人想知道我在使用MySql。
这也工作得很好,看起来更正确。我将把这标记为一个答案。 – Eskipo
@Eskipo我建议你使用第二种方式 – SpringLearner
“i”+“”+“would”+“”+“选择”+“”+“”+“”+“+”+“”“方式” +“:D” – Andy