我第一次尝试使用PreparedStatements,但由于某种原因,我无法让它工作。不使用PreparedStaement的相同请求可以正常工作。
简单的工作版本:
String artNum = "'" + artikelNummer.toLowerCase() + "'";
String query =
"SELECT a.artnr, a.bezeichnung, "
+ "(SELECT verfuegbar_bestand "
+ "FROM getbestand(a.uid)) AS bestand_verfuegbar "
+ "FROM article AS a "
+ "WHERE lower(a.artnr) = " + artNum;
ResultSet results = statement.executeQuery(query);
PreparedStatement版本:
String artNum = "'" + artikelNummer.toLowerCase() + "'";
String query =
"SELECT a.artnr, a.bezeichnung, "
+ "(SELECT verfuegbar_bestand "
+ "FROM getbestand(a.uid)) AS bestand_verfuegbar "
+ "FROM article AS a "
+ "WHERE lower(a.artnr) = ? ";
prepStatement = con.prepareStatement(query);
prepStatement.setString(1, artNum);
ResultSet results = prepStatement.executeQuery();
任何帮助/想法都会得到很好的接受。
Ps是artNum是一个String而不是int,例如A1110
答案 0 :(得分:2)
如果您正在使用PreparedStatements,则无需将字符串括在引号中,如下所示:
String artNum = "'" + artikelNummer.toLowerCase() + "'"
这需要:
String artNum = artikelNummer.toLowerCase();
答案 1 :(得分:1)
您仍在引用文章编号 - 您不再需要这样做,因为您使用参数直接传递值。只需使用:
String query =
"SELECT a.artnr, a.bezeichnung, "
+ "(SELECT verfuegbar_bestand FROM getbestand(a.uid))"
+ " AS bestand_verfuegbar "
+ "FROM article AS a "
+ "WHERE lower(a.artnr) = ?";
prepStatement = con.prepareStatement(query);
prepStatement.setString(1, artikelNummer.toLowerCase());
ResultSet results = prepStatement.executeQuery();
(对两个字符串进行下限以执行不区分大小写的查询通常不是一个好主意,但这是一个单独的问题。)