質問

I get an error when I'm trying to add a student with one attribute, name. The error is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''name')' at line 1.

The error points out this codes,

 //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();
        }
}

The method addStudent() is called when the button add is clicked. If somebody wonders I'm using MySql.

役に立ちましたか?

解決

check this line PreparedStatement studentStatement = getConnected().prepareStatement("insert into person" + "values (?)") there is no space between person and values

do like this

PreparedStatement studentStatement = getConnected().prepareStatement("insert into person " + "values (?)") or simply

PreparedStatement studentStatement = getConnected().prepareStatement("insert into person values (?)") for no confusion about spaces

他のヒント

it should be

insert into person (name) values (?)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top