SQL在Java中不返回结果,尽管它总是在Postgres中返回结果
在Postgres数据库中运行此查询时:SQL在Java中不返回结果,尽管它总是在Postgres中返回结果,java,sql,postgresql,jdbc,Java,Sql,Postgresql,Jdbc,在Postgres数据库中运行此查询时: select iid from original where accid=1000 and msg_number=1669; 它总是在Postgres DB中返回一个结果,但当我在Java中运行它时,它不会返回任何结果 这是原始的表格: iid: int4 accid: int4 msg_number: int4 这是我的Java代码: public String getIID(String msgNr) { String accid=nul
select iid from original where accid=1000 and msg_number=1669;
它总是在Postgres DB中返回一个结果,但当我在Java中运行它时,它不会返回任何结果
这是原始的表格:
iid: int4
accid: int4
msg_number: int4
这是我的Java代码:
public String getIID(String msgNr) {
String accid=null;
try {
Statement accidStmt = conn.createStatement();
String accidQuery = "select accid from accounts where name='" + clientname + "'";
ResultSet accidResult = accidStmt.executeQuery(accidQuery);
while (accidResult.next())
accid = accidResult.getString("accid");
accidResult.close();
accidStmt.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
String iid = null;
try {
String iidQuery = "select iid from original where accid=" + accid + " and msg_number=" + msgNr;
// String iidQuery = "select iid from original where accid=" + Integer.parseInt(accid) + " and msg_number=" + Integer.parseInt(msgNr);
// String iidQuery = "select iid from original where accid=1000 and msg_number=1669";
Statement iidStmt = conn.createStatement();
ResultSet iidResult = iidStmt.executeQuery(iidQuery);
while (iidResult.next())
iid = iidResult.getString("iid");
iidResult.close();
iidStmt.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return iid;
}
获取accid
的第一个查询有效。但是获取iid
的第二个查询不起作用,结果总是0行。正如我的代码中所显示的,我已经尝试将accid
转换为整数,甚至尝试了一个硬值,但此查询没有返回结果。而且它不会进入catch内部,因为查询已成功执行。所以我不知道怎么了
非常感谢您的帮助。var“accid”是字符串对象
accid = accidResult.getString("accid");
您可能应该始终使用准备好的语句。请参阅下面更新的功能:
public int getIID(String msgNr) {
int accid=0;
try {
Statement accidStmt = conn.createStatement();
String accidQuery = "select accid from accounts where name='" + clientname + "'";
ResultSet accidResult = accidStmt.executeQuery(accidQuery);
while (accidResult.next())
accid = accidResult.getInt("accid");
accidResult.close();
accidStmt.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
int iid = 0;
try {
String iidQuery = "select iid from original where accid=? and msg_number=?";
PreparedStatement pst=conn.prepareStatement(iidQuery);
pst.setInt(1,accid);
pst.setInt(2,Integer.parseInt(msgNr));
ResultSet iidResult=pst.executeQuery();
while (iidResult.next())
iid = iidResult.getInt("iid");
iidResult.close();
iidStmt.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return iid;
}
您应该在这里使用准备好的语句,但也就是说,如果where子句中的两列是整数,那么我认为您的查询应该可以工作。您确定accid在该块中得到值1000,而msgNumber是1669吗?您的查询看起来很适合SQL注入攻击。请使用
PreparedStatement
,而不要使用常规的语句。使用try with resources
以实现最佳的资源关闭,当列为int
s时,不要使用getString()
。也许我对sql api不太了解,但您是否立即调用next()
跳过第一行,因此while
永远不会执行?@daniu Nope。如果该列是数字,则不会。但是准备好的语句可以解决这个问题。尽管它是一个字符串并不重要,因为他正在将它连接到查询。嗨,但是“String accid=accidResult.getString(“accid”);”accid类型是整数,但他是getString,accid=>nulls它不是这样工作的。它将数字转换为字符串。没问题,您应该始终使用准备好的语句。准备好的语句可确保攻击者无法更改查询的意图