用逗号表示MySQL语法错误? - Java

时间:2013-11-16 22:09:22

标签: java mysql sql syntax

我正在尝试在数据库中插入一些数据,但我遇到了一些麻烦。目前我无法将数据添加到我的表中,我不知道为什么。

我正在使用一些您不需要理解的通用方法,这适用于我的所有项目,并且在任何这些项目中都没有任何错误。

以下是我在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;
}

从我的控制器调用“adicionarTurno”方法:

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

这是我的Escola实体:

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;
    }

}

<小时/> 当我尝试在arrayList中插入一些值并尝试将它们插入到DB中时(使用EscolaController),我收到以下错误消息(第一行是我尝试插入的“toString”):

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

我能做错什么?

1 个答案:

答案 0 :(得分:1)

正确的语法是:

insert ignore into Turno values (?)

请注意:

  • 你需要括号来理解值
  • 你不能有一个尾随的分号。

您最好指定列名,以使您的代码更清晰,更安全:

insert ignore into Turno (name_of_the_column) values (?)

阅读文档可节省数小时:

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