java com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“where 子句”中的“Smith”列未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16328935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:39:42  来源:igfitidea点击:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Smith' in 'where clause'

javamysqlsyntax

提问by greedybuddha

hope you can help me. I get the error

希望您能够帮助我。我收到错误

"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Smith' in 'where clause'".

“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:‘where 子句’中的未知列‘Smith’”。

I have a database called "aspirante" and it has a table (I already filled it) named "datos". Here I show you how I created the table:

我有一个名为“aspirante”的数据库,它有一个名为“datos”的表(我已经填写了它)。在这里,我向您展示我是如何创建表格的:

CREATE TABLE `datos` (
  `id_asp` int(11) NOT NULL AUTO_INCREMENT,
  `ficha` mediumint(4) NOT NULL,
  `apellido1` varchar(30) NOT NULL,
  `apellido2` varchar(30) NOT NULL,
  `nombre` varchar(50) NOT NULL,
  `genero` char(1) NOT NULL,
  `telefono1` varchar(20) NOT NULL,
  `telefono2` varchar(20) NOT NULL,
  `carrera` varchar(50) NOT NULL,
  `promedio_sec` varchar(5) NOT NULL,
  PRIMARY KEY (`id_asp`)
)

The method looks like this.

该方法看起来像这样。

void BuscarAspiranteEditar(String id) {
    String sSQL = "";
    String ap1 = "", ap2 = "", nom = "", gen = "", tel1 = "", tel2 = "", car = "", prom = "";

    ConexionMySQL mysql = new ConexionMySQL();
    Connection cn = mysql.Conectar();

    sSQL = "SELECT id_asp, ficha, apellido1, apellido2, nombre, genero, telefono1, telefono2, promedio_sec FROM datos " +
            "WHERE id_asp = "+id;

    try {
        Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery(sSQL);

        while (rs.next()) {
            ap1 = rs.getString("apellido1");
            ap2 = rs.getString("apellido2");
            nom = rs.getString("nombre");
            gen = rs.getString("genero");
            tel1 = rs.getString("telefono1");
            tel2 = rs.getString("telefono2");
            car = rs.getString("carrera");
            prom = rs.getString("promedio_sec");
            id_actualizar = id;
        }

        txtPrimerApellido.setText(ap1);
        txtSegundoApellido.setText(ap2);
        txtNombre.setText(nom);
        txtPrimerTelefono.setText(tel1);
        txtSegundoTelefono.setText(tel2);
        txtCarrera.setText(car);
        txtPromedio.setText(prom);

    }
    catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

I am not into Java so I hope you can hel me guys, I'd appreciate it. Thanks in advance!

我不喜欢 Java,所以我希望你们能帮助我,我会很感激。提前致谢!

回答by

where is your method that passes the parameter id? if your idis a string you must enclosed it with quotes 'id'in like Where id_asp='"+ id +"'"

您传递参数的方法在哪里id?如果你id是一个字符串,你必须用引号括起来它'id'Where id_asp='"+ id +"'"

回答by greedybuddha

So it looks like your problem is the id you are passing for the where clause is incorrect. You are passing in a last name "Smith" instead of the asp id.

所以看起来你的问题是你为 where 子句传递的 id 不正确。您传入的是姓氏“Smith”而不是asp id。

As a note about programming with SQL, to be safe you really should be writing your queries with the mysql "?". This allows mysql to put in the appropriate quotes if needed, and do checks for sql injection.

作为使用 SQL 编程的注意事项,为了安全起见,您确实应该使用 mysql“?”编写查询。这允许 mysql 在需要时放入适当的引号,并检查 sql 注入。

sSQL = "SELECT id_asp, ficha, apellido1, apellido2, nombre, genero, telefono1, telefono2, promedio_sec FROM datos WHERE id_asp = ?";
PreparedStatement ps = connection.prepareStatement(sSQL);
ps.setObject(1, my_id);
ResultSet rs = ps.executeQuery();

It's a bit safer and you can pass in variables directly without knowing what needs to be wrapped in quotes etc.

它更安全一点,您可以直接传入变量,而无需知道需要用引号等包装什么。