【发布时间】:2021-06-15 07:03:59
【问题描述】:
我总是收到错误提示,sql 语句中的 arraylist 是 varchar 而不是 int。我试图在一个整数变量中解析数组列表,但它一直给我同样的错误。
代码:
try {
String s = getIDUsuario().toString();
String s1 = s.split(" ")[1];
int id = Integer.parseInt(s1.charAt(2) + "");
Statement st = dataBase.conexionBD().createStatement();
ResultSet rs = st.executeQuery(" select * from Serveis where id_servei = " + id + "");
while (rs.next()) {
bytes = rs.getBytes("imatge_servei");
image = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
try {
listaObjeto.add(new InfoServicio(getNombre(), rs.getString("titol"), rs.getString("descripcio"), getImagenPerfil(), image));
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
}
【问题讨论】:
-
那是因为当你在查询中说它是一个字符串(varchar)时,当你用撇号'id'包围它时。删除它们并重试。但你真的应该使用带参数的prepared statement
-
我做了,但还是一样
-
你为什么要问
ArrayList?您的代码中没有ArrayList,我没有得到相关性? -
即使您省略了单引号,您如何确定数据库将其检测为
varchar?另外,您使用的是什么品牌的数据库引擎,数据库中id_servei的数据类型是什么,查询是否返回所需的行或其他内容,如果是,那是什么? -
使用sql server,id_servei type int。