SQL,试图比较2 int并获得错误“Cant compare char and int”

时间:2016-02-01 01:51:45

标签: java sql jdbc jtable

我收到了这个错误:

  

“java.sql.SQLSyntaxErrorException:没有estánsomportadaslas   比较者进入'INTEGER'y'CHAR(UCS_BASIC)'。 Los tipos deben   ser可比较。 Además,los tipos decadenatambiéndeentener una   intercalación巧合。 Silaintercalación没有重合,una   可行的soluciónesconvertir los operandos para forzarlos a la   intercalaciónpordefecto(por ejemplo:SELECT tablename FROM   sys.systables WHERE CAST(tablename AS VARCHAR(128))='T1')“

我的数据库表有11个位置,一切都是varchar但是第一个,就是一个int。

iI'从jtable获取数据,除col 0之外的所有内容都是int,其余是字符串。

这是我的代码:

int colActual = jTableNombre.getSelectedRow();

private void jButtonGuardarNombreActionPerformed(java.awt.event.ActionEvent evt) {                                                     

    String nombre = (String) jTableNombre.getModel().getValueAt(colActual,1);
    String director=(String) jTableNombre.getModel().getValueAt(colActual,2);
    String año = (String) jTableNombre.getModel().getValueAt(colActual,3);
    String generos = (String) jTableNombre.getModel().getValueAt(colActual,4);
    String actores = (String) jTableNombre.getModel().getValueAt(colActual,5);
    String pais = (String) jTableNombre.getModel().getValueAt(colActual,6);
    String idioma = (String) jTableNombre.getModel().getValueAt(colActual,7);
    String doblaje = (String) jTableNombre.getModel().getValueAt(colActual,8);
    String subtitulos = (String) jTableNombre.getModel().getValueAt(colActual,9);
    String ubicacion = (String) jTableNombre.getModel().getValueAt(colActual,10);
    try {   
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement();
        String sql= "UPDATE MOVIES " + 
            "SET NOMBRE = '"+nombre+"', "+
            "DIRECTOR = '"+director+"', "+
            "AÑO = '"+año+"', "+
            "GENEROS = '"+generos+"', "+
            "ACTORES = '"+actores+"', "+
            "PAIS = '"+pais+"', "+
            "IDIOMA = '"+idioma+"', "+
            "DOBLAJE = '"+doblaje+"', "+
            "SUBTITULOS = '"+subtitulos+"', "+
            "UBICACION = '"+ubicacion+"' "+
            "WHERE ID = '"+id+"'";
        stmt.executeUpdate(sql);                           
        int i= stmt.executeUpdate(sql);
        System.out.print(i);
        if (i>0) {
            JOptionPane.showMessageDialog(null, "Movie Updated");  
        }
        else {
            JOptionPane.showMessageDialog(null, "Movie dindt update");
        }

    } 
    catch(Exception e) {
        System.out.println(e);
    }

}   

2 个答案:

答案 0 :(得分:0)

您在这段代码中遗漏了id

    "WHERE ID = '"+id+"'";

尝试添加它。

答案 1 :(得分:0)

尝试更改此行代码:

        "WHERE ID = '"+id+"'";

要:

        "WHERE ID = "+id;

应该工作。

欢迎使用Java,我建议你看一下jdbc docs:https://docs.oracle.com/javase/tutorial/jdbc/basics/,并且作为一个建议,我必须你,如果你得到了SQLXXX错误,总是继续调试,打印你的SQL命令并尝试在任何SQL客户端上执行它,可能有助于查找语法错误。

C ya。

相关问题