[CONJ-507] hi want to insert data to an table with prepared statement, but its talking about syntax error Created: 2017-07-19  Updated: 2017-07-19  Resolved: 2017-07-19

Status: Closed
Project: MariaDB Connector/J
Component/s: question
Affects Version/s: None
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: cyborg Assignee: Diego Dupin
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

MariaDB 10.0.30



 Description   

private void insertDataInSql(){
        try {
            Class.forName(dbDriver).newInstance();
            Connection connection = DriverManager.getConnection(connectString,user,pw);
 
            String query = "insert into task (description) values (?)";
 
            PreparedStatement preparedStatement = connection.prepareStatement(query);
//            preparedStatement.setString(1,this.textFieldName.getText());
            preparedStatement.setString(1,this.textAreaDescription.getText());
            preparedStatement.executeUpdate(query);
            connection.close();
            System.out.println("Inserted!");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

The Error: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?)' "



 Comments   
Comment by Diego Dupin [ 2017-07-19 ]

executeUpdate(sql) is inherit from Statement (see javadoc). This really execute the sql in parametre so "insert into task (description) values " in your case

Just replace "executeUpdate(query);" by "executeUpdate();"
executeUpdate(); will execute the prepared query with the parameter.

Comment by cyborg [ 2017-07-19 ]

awesome! its running thanks!

Generated at Thu Feb 08 03:16:10 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.