SQL,尝试比较 2 int 并出现错误 "Cant compare char and int"
SQL, trying to compare 2 int and getting error "Cant compare char and int"
我收到此错误:
"java.sql.SQLSyntaxErrorException: No están soportadas las
comparaciones entre 'INTEGER' y 'CHAR (UCS_BASIC)'. Los tipos deben
ser comparables. Además, los tipos de cadena también deben tener una
intercalación coincidente. Si la intercalación no coincide, una
posible solución es convertir los operandos para forzarlos a la
intercalación por defecto (por ejemplo: SELECT tablename FROM
sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1')"
我的数据库 table 有 11 个位置,所有位置都是 varchar,但第一个是 int。
并且我从 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);
}
}
您在这段代码中缺少 id
:
"WHERE ID = '"+id+"'";
尝试添加。
尝试更改这行代码:
"WHERE ID = '"+id+"'";
收件人:
"WHERE ID = "+id;
应该可以。
欢迎使用 Java,我还建议你看一下 jdbc 文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/ 另外,如果你正在获取和SQLXXX 错误,始终继续调试,打印您的 SQL 命令并尝试在任何 SQL 客户端上执行它,可能有助于查找语法错误。
C ya.
我收到此错误:
"java.sql.SQLSyntaxErrorException: No están soportadas las comparaciones entre 'INTEGER' y 'CHAR (UCS_BASIC)'. Los tipos deben ser comparables. Además, los tipos de cadena también deben tener una intercalación coincidente. Si la intercalación no coincide, una posible solución es convertir los operandos para forzarlos a la intercalación por defecto (por ejemplo: SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1')"
我的数据库 table 有 11 个位置,所有位置都是 varchar,但第一个是 int。
并且我从 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);
}
}
您在这段代码中缺少 id
:
"WHERE ID = '"+id+"'";
尝试添加。
尝试更改这行代码:
"WHERE ID = '"+id+"'";
收件人:
"WHERE ID = "+id;
应该可以。
欢迎使用 Java,我还建议你看一下 jdbc 文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/ 另外,如果你正在获取和SQLXXX 错误,始终继续调试,打印您的 SQL 命令并尝试在任何 SQL 客户端上执行它,可能有助于查找语法错误。
C ya.