Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert into mysql with preparedstatement not working

I’m trying to insert data into my mysql table using java, but I really can’t.
I’m using PreparedStatement class to do my insertion and it seems like the ‘?’ is not beeing changed by my variable or something like that.

So follow my code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.lang.Exception;
import java.time.LocalDate;
import java.time.LocalTime;

public class db{
    static String driverJDBC = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://localhost/database";
    static String user = "root";
    static String senha = "root";

    public static void InsereVin(db banco,String vin){
        Connection conexao = null;
        Connection con = conexao;
        
        //String instrucaoSQL = "insert into authentication (senha) values (\"senhaMaster\");";
        String instrucaoSQL = "INSERT INTO authentication(senha) VALUES(?);";

        try{
            System.out.println("Carregando o driver jdbc...");
            Class.forName(driverJDBC);
            System.out.println("Driver carregado com sucesso");

            conexao = DriverManager.getConnection(url,user,senha);
            
            PreparedStatement preparedStmt = conexao.prepareStatement(instrucaoSQL);
            preparedStmt.setString(1, vin);
            /*preparedStmt.setInt       (2, contador);
            preparedStmt.setDate      (3, java.sql.Date.valueOf(date));
            preparedStmt.setTime      (4, java.sql.Time.valueOf(time));
            preparedStmt.setString    (5, impressao);
            */
            System.out.println("Inserindo ...");
            
            preparedStmt.execute(instrucaoSQL);
            System.out.println("Inserido com sucesso");
            preparedStmt.close();
            conexao.close();
            
        }
        catch(Exception e){
            System.out.println("Erro");
            e.printStackTrace();    
        }
    }

    public static void main(String[] args){
        db banco = new db();
        InsereVin(banco,"12345678912345678");
    }
    
}

and the error I’m getting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

java.sql.SQLSyntaxErrorException: 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 '?)' at line 1
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
        at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763)
        at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
        at db.InsereVin(db.java:39)
        at db.main(db.java:53)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:419)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)

I really can’t understand what is going wrong with my code and I’ll be very thankfull if someone could help me.

>Solution :

This line is causing the error:

preparedStmt.execute(instrucaoSQL);

Instead of calling PreparedStatement#execute, you are actually calling a method that PreparedStatement inherits from Statement. See the javadoc for the method you are calling here. See the javadoc for the method you want to call here.

Upon reading the documentation, it should become clear to you that you need to change preparedStmt.execute(instrucaoSQL); to preparedStmt.execute(); in your code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading