当 运行 到 java class 时出现 ORA-00936 错误
ORA-00936 error when running through java class
我需要将 OracleDataSource 用于学校项目。我已经在 Oracle Developer 上创建并测试了我的查询,我得到了正确的输出。当我尝试通过 java 代码执行查询时,出现以下错误:java.sql.SQLSyntaxErrorException: ORA-00936: missing expression
。我在网上做了一些挖掘,人们说这可能是由于 WHERE 子句。所以我在执行之前打印出查询。查询是这样的: SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (47,46,43) GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC
如果我在 Developer 中输入相同的查询,它会工作,但是当我 运行 我的 java class 我得到 ORA 错误。我以这种方式形成我的查询:
Statement stmt = conn.createStatement();
String query = "SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (";
PreparedStatement ps = conn.prepareStatement(query);
for(int i = 0; i < args.length; i++) {
//int psVar = i + 1;
//ps.setInt(psVar, Integer.parseInt(args[i]));
if(i == args.length - 1) {
query += args[i] + ")";
} else {
query += args[i] + ",";
}
}
query += " GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC";
//Execute the query
System.out.println(query);
ResultSet rset = ps.executeQuery();
我迷路了,谢谢你的帮助
您的问题是您在完成查询之前准备查询。将最后一个赋值后的 PreparedStatement ps = conn.prepareStatement(query);
移动到 query
,并为每个参数使用占位符:
Statement stmt = conn.createStatement();
String query = "SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (";
for(int i = 0; i < args.length; i++) {
if(i == args.length - 1) {
query += "?)";
} else {
query += "?, ";
}
}
query += " GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC";
PreparedStatement ps = conn.prepareStatement(query);
for(int i = 0; i < args.length; i++) {
int psVar = i + 1;
ps.setInt(psVar, Integer.parseInt(args[i]));
}
ResultSet rset = ps.executeQuery();
我需要将 OracleDataSource 用于学校项目。我已经在 Oracle Developer 上创建并测试了我的查询,我得到了正确的输出。当我尝试通过 java 代码执行查询时,出现以下错误:java.sql.SQLSyntaxErrorException: ORA-00936: missing expression
。我在网上做了一些挖掘,人们说这可能是由于 WHERE 子句。所以我在执行之前打印出查询。查询是这样的: SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (47,46,43) GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC
如果我在 Developer 中输入相同的查询,它会工作,但是当我 运行 我的 java class 我得到 ORA 错误。我以这种方式形成我的查询:
Statement stmt = conn.createStatement();
String query = "SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (";
PreparedStatement ps = conn.prepareStatement(query);
for(int i = 0; i < args.length; i++) {
//int psVar = i + 1;
//ps.setInt(psVar, Integer.parseInt(args[i]));
if(i == args.length - 1) {
query += args[i] + ")";
} else {
query += args[i] + ",";
}
}
query += " GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC";
//Execute the query
System.out.println(query);
ResultSet rset = ps.executeQuery();
我迷路了,谢谢你的帮助
您的问题是您在完成查询之前准备查询。将最后一个赋值后的 PreparedStatement ps = conn.prepareStatement(query);
移动到 query
,并为每个参数使用占位符:
Statement stmt = conn.createStatement();
String query = "SELECT b.ISBN, b.TITLE, COUNT(*) FROM BOOKS b JOIN BOOK_SUBJECT bs ON bs.ISBN = b.ISBN WHERE bs.SUBJECT_ID IN (";
for(int i = 0; i < args.length; i++) {
if(i == args.length - 1) {
query += "?)";
} else {
query += "?, ";
}
}
query += " GROUP BY b.ISBN, b.TITLE ORDER BY COUNT(*) DESC";
PreparedStatement ps = conn.prepareStatement(query);
for(int i = 0; i < args.length; i++) {
int psVar = i + 1;
ps.setInt(psVar, Integer.parseInt(args[i]));
}
ResultSet rset = ps.executeQuery();