gpt4 book ai didi

java - MySQL语法错误,逗号? - java

转载 作者:行者123 更新时间:2023-11-29 13:26:44 26 4
gpt4 key购买 nike

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

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

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

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

这是我的 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 中插入一些值并尝试将它们插入到数据库中(使用 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

我可能做错了什么?

最佳答案

正确的语法是:

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

关于java - MySQL语法错误,逗号? - java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20024546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com