我正在尝试通过JDBC对Oracle DB执行查询。我已经验证它在SQL Developer中有效。 但是当我通过JDBC运行它时,我在
上得到一个“null”值ps = con.prepareStatement(sql.toString());
我尝试过不同的语句和SQL查询,所有这些都在SQL开发人员的工作中完成。另外,我没有得到任何堆栈跟踪来调试..
查询1
sql.append("SELECT * FROM Tabel WHERE (ID1=" + id + " AND ORG=" + org + ") OR (ID2=" + id + " AND ORG=" + org + ")");
QUERY2
sql.append("SELECT * FROM Tabel WHERE (ID1=" + id + " OR ID2=" + id + ") AND ORG=?" + org);
QUERY3
sql.append("SELECT * FROM Tabel WHERE (ID1=? OR ID2=?) AND ORG=?");
ps = con.prepareStatement(sql.toString());
ps.setString(1, id);
ps.setString(2, id);
ps.setString(3, org);
代码
try {
log.debug("Creating Query");
StringBuffer sql = new StringBuffer();
sql.append("SELECT * FROM Table WHERE (ID1=? OR ID2=?) AND ORG=?");
log.debug("Query created: " + sql.toString()); //Don't get here
ps = con.prepareStatement(sql.toString());
ps.setString(1, id);
ps.setString(2, id);
ps.setString(3, org);
log.debug("Query Created!!!!");
con = getConnection();
} catch (Exception e) {
log.error("Could not create statement in methodXXX");
//Returns null in e.getMessage()
throw new Exception("Could not create query: " + e.getMessage());
}
//亨利克
答案 0 :(得分:1)
只需使用以下方式
ps = con.prepareStatement("SELECT * FROM Tabel WHERE (ID1=? OR ID2=?) AND ORG=?");
ps.setString(1, id);
ps.setString(2, id);
ps.setString(3, org)
ResultSet rs=ps.executeQuery();
while(rs.next())
{
//use your code to retrieve like rs.getString(1);
}