-1

错误是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''3)' 附近使用正确的语法

代码是:

fecha = +jFecha.getCalendar().get(Calendar.YEAR) + "-" + (jFecha.getCalendar().get(Calendar.MONTH) + 1) + "-" + jFecha.getCalendar().get(Calendar .DAY_OF_MONTH);
        System.out.println(fecha);
        String parte = cmbParte.getSelectedItem().toString();
        System.out.println(parte);
        尝试 {

            Conexion.ins.execute("插入到 registros (id_registro, job, ip, id_parte, fecha, id_inspector, id_descripcion, piezasrech, piezasinsp, id_disposicion, id_area, id_responsable, id_estacion)"
                    + "值(0,'" + txtOrder.getText() + "','" + txtIP.getText() + "','" + id_parte
                    + "','" + fecha + "','" + id_inspector + "','" + id_descripcion
                    + "','" + txtPR.getText() + "','" + txtPI.getText() + "','" + id_disposicion
                    + "','" + id_area + "','" + id_responsable + "','" + id_estacion + ");");

            JOptionPane.showMessageDialog(this, "Registro agregado con exito");

        } 捕捉(SQLException e){
            JOptionPane.showMessageDialog(this, "Error al intentar agregar el Registro");
            System.out.println(e);
        }
    }
4

4 回答 4

2

我建议您放弃该Conexion.ins.execute方法的当前实现,并将其替换为使用 a的方法,这样PreparedStatement您就可以摆脱这种难以维护的连接问题,更糟糕的是,容易出现SQL Injection

PreparedStatement这是使用参数和 vararg 参数的实现示例:

public void execute(String query, Object ... params) throws SQLException {
    //assumes you already have your Connection
    PreparedStatement pstmt = con.prepareStatement(query);
    int i = 1;
    for(Object param : params) {
        pstmt.setObject(i++, param);
    }
    pstmt.executeUpdate();
    pstmt.close();
}

使用此实现,您可以以对您和您的同事更易读的方式调用该方法:

String insertSQL = "insert into registros(id_registro, job, ip, id_parte, fecha,"
    + "id_inspector, id_descripcion, piezasrech, piezasinsp, "
    + "id_disposicion, id_area, id_responsable, id_estacion) "
    + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
Conexion.ins.execute(insertSQL,
    txtOrder.getText(), txtIP.getText(), id_parte, fecha,
    id_inspector, id_descripcion, txtPR.getText(), txtPI.getText(),
    id_disposicion, id_area, id_responsable, id_estacion);
于 2013-07-04T05:08:50.840 回答
1

在 id_estacion in 之后的右括号之前似乎缺少一个 <'> id_responsable + "','" + id_estacion + ");");,因此没有括起来id_estacion 。

应该id_responsable + "','" + id_estacion + '");");

于 2013-07-04T05:01:45.497 回答
0

您的查询使用此字符串执行

......,'3);

这是错误的,因为您'缺少 a 所以要么删除它,要么'在最后添加匹配。

你的字符串:

id_responsable + "','" + id_estacion + ");");

只需将其更改为:

id_responsable + "'," + id_estacion + ");");

或者

id_responsable + "','" + id_estacion + "');");
于 2013-07-04T05:02:40.683 回答
0
 + "','" + id_area + "','" + id_responsable + "','" + id_estacion + '");");

更改最后一行

于 2013-07-04T05:05:11.130 回答