[英]Java derby sql integer query
Please take a look at my code snippet below. 请在下面查看我的代码段。 What I am trying to achieve is querying the database for any results equal to the user input.
我想要实现的是在数据库中查询等于用户输入的任何结果。 It is querying
SDS_NUMBER
column which is an integer column. 它正在查询
SDS_NUMBER
列,它是一个整数列。
When I execute the query it returns the following exception: 当我执行查询时,它返回以下异常:
java.sql.SQLSyntaxErrorException: Comparisons between 'INTEGER' and 'CHAR (UCS_BASIC)' are not supported.
java.sql.SQLSyntaxErrorException:不支持在“ INTEGER”和“ CHAR(UCS_BASIC)”之间进行比较。 Types must be comparable.
类型必须是可比较的。 String types must also have matching collation....etc.
字符串类型还必须具有匹配的排序规则。
I understand that it is saying I am trying to compare an integer to char but I have tried to cast from examples I found on the net but no luck. 我知道这是说我正在尝试将整数与char进行比较,但是我尝试从网上发现的示例中进行转换,但是没有运气。 Also I tried
parseInt
and using that value in the search but I still can't get it to work. 我也尝试了
parseInt
并在搜索中使用该值,但仍然无法正常工作。 Please advise what I am doing wrong and excuse my newbies to all of this. 请告诉我我做错了什么,请原谅我的新手。
} else if (tmp == "sdsNumber") {
try {
Integer sdsNum = Integer.parseInt(val);
String sql = "SELECT SDS_NUMBER, PRODUCT_NAME, PROPER_SHIPPING_NAME FROM APP.MASTER WHERE SDS_NUMBER = '"+sdsNum+"'";
pst = conn.prepareStatement(sql);
pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace(System.out);
JOptionPane.showMessageDialog(null, e);
}
Master table creation: 主表创建:
CREATE TABLE MASTER
(
id integer NOT NULL GENERATED ALWAYS AS (START WITH 1, INCREMENT BY 1)
, SDS_NUMBER integer UNIQUE
, PRODUCT_NAME varchar(100)
, PRODUCT_DESCRIPTION varchar(500)
, SDS_FILE_PATH varchar(50)
, USE_STATUS BOOLEAN
, DATA_UPDATED date
, PROPER_SHIPPING_NAME varchar(100)
, SIGNAL_WORD varchar(20)
, CONSTRAINT MASTER_PRIMARY_KEY PRIMARY KEY (id)
);
Remove the single quote "'"+sdsNum+"'"
to be a number 删除单引号
"'"+sdsNum+"'"
成为数字
Her ara Example by using preparedStatement
她的ara示例通过使用
preparedStatement
String sql = "SELECT SDS_NUMBER, PRODUCT_NAME, PROPER_SHIPPING_NAME FROM APP.MASTER WHERE SDS_NUMBER = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, sdsNum);
ResultSet rs = pst.executeQuery(sql);
while (rs.next()) {
int SDS_NUMBER = rs.getInt("SDS_NUMBER");
String PRODUCT_NAME= rs.getString("PRODUCT_NAME");
....
}
it seems you're wrapping an integer in quotes 似乎您在用引号括住一个整数
SDS_NUMBER = '"+sdsNum+"'"
probably what you want is 可能你想要的是
SDS_NUMBER = ?
and then set the number with your PreparedStatement instead of concatenating strings 然后使用PreparedStatement设置数字,而不是连接字符串
see http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 参见http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
and 和
http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt%28int,%20int%29 http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt%28int,%20int%29
} else if ("sdsNumber".equals(tmp)) {
after I changed the first line to this everything works. 在将第一行更改为此之后,一切正常。 I dont fully understand what difference that made and if someone could shine some light on it, I would appreciate it as I am slowly learning this.
我不完全了解这会带来什么变化,如果有人能对此有所了解,我会很感激,因为我正在慢慢学习这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.