Paladini
Paladini

Reputation: 4572

MySQL syntax error with commas? - Java

I'm trying to insert some data in a DB but I'm having some troubles. Currently I can't add data to my table and I don't know why.

I'm using some generic methods that you don't need to understand, this works in all my projects and don't have any error in any of this projects.

Following is my method in the EscolaDAO:

public boolean adicionarTurno(String turno) {
        String sql = "INSERT IGNORE INTO Turno (descricao) values ?;";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(turno);
        operacaoEscrita(sql, params);
        return true;
}

The method "adicionarTurno" is called from my Controller:

EscolaDAO modelo = new EscolaDAO();

public void adicionarHorario(Escola escola){

       for(Turno temp : escola.getTurnos()){
           System.out.println("Controle Turno: " + temp.getNome());
           modelo.adicionarTurno(temp.getNome());
        }
}

And here's my Escola entity:

public class Escola {

    private String nomeEscola;
    private ArrayList<Turno> turnos = new ArrayList<Turno>();
    private ArrayList<Dia> dias = new ArrayList<Dia>();

    public Escola() { }

    public Escola(String nomeEscola) {
        this.nomeEscola = nomeEscola;
    }

    public ArrayList<Dia> getDias() {
        return dias;
    }

    public void setDias(ArrayList<Dia> dias) {
        this.dias = dias;
    }

    public String getNomeEscola() {
        return nomeEscola;
    }

    public void setNomeEscola(String nomeEscola) {
        this.nomeEscola = nomeEscola;
    }

    public ArrayList<Turno> getTurnos() {
        return turnos;
    }

    public void setTurnos(ArrayList<Turno> turnos) {
        this.turnos = turnos;
    }

}


When I try to insert some values in arrayList and try to insert them in the DB (using EscolaController), I got the following error message (the first line is the "toString" of what I try to insert):

Controle Turno: Vespertino
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 ''Vespertino'' at line 1

Controle Dia: Segunda-feira
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 ''Segunda-feira'' at line 1

Controle Dia: Terça-feira
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 ''Terça-feira'' at line 1

Controle Dia: Quarta-feira
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 ''Quarta-feira'' at line 1

Controle Dia: Quinta-feira
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 ''Quinta-feira'' at line 1

What can I be doing wrong?

Upvotes: 0

Views: 386

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

The correct syntax is:

insert ignore into Turno values (?)

Note that:

  • you need parentheses arount the values
  • you must not have a trailing semicolon.

You'd better specify the column names, to make your code clearer and safer:

insert ignore into Turno (name_of_the_column) values (?)

Reading the documentation saves hours:

http://dev.mysql.com/doc/refman/5.7/en/insert.html

Upvotes: 1

Related Questions