當我嘗試添加具有一個屬性名稱的學生時出現錯誤。 錯誤是:嘗試將學生添加到數據庫時出錯
您的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