简体   繁体   English

MySQL语法错误和逗号? -Java

[英]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: 以下是我在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: 从我的控制器调用方法“ 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());
        }
}

And here's my Escola entity: 这是我的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;
    }

}


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): 当我尝试在arrayList中插入一些值并尝试将它们插入数据库(使用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 

What can I be doing wrong? 我做错了什么?

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 http://dev.mysql.com/doc/refman/5.7/zh-CN/insert.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM