Oracle无效标识符不理解字符串
我的查询无效,我遇到了问题。 这是command
变量。
当它执行时,它应该检索具有BSc作为其学位的元组。 我已经在oracle中直接测试了这个,查询返回了这些。 它与command
语句相同。
当我打印出command
,该行看起来与在oracle中运行的命令完全相同。
SELECT distinct fname, lname, student_id FROM student where degree='BA';
然而,它应该打印到屏幕上。 这些表已经加载到oracle中了。
我一直在用这个问题绞尽脑汁,但似乎无法找到解决办法!
我一直得到的错误是: ORA-00911: invalid character
我所做的是将扫描仪的结果存储在一个字符串中。 因此,在command
变量中连接它不应该产生问题 – 查询看起来与oracle中的相同。
可能是因为它想要一个字符而不是一个字符串? 如果确实如此,那我怎么能让它把“BSc”作为一个炭? 连接字符听起来很愚蠢。
相关代码如下:
private String getDegree() { Scanner scan = new Scanner(System.in); System.out.println("Please enter degree code (either BA or BSc)"); return scan.next();
}
//get the degree name String degree = getDegree(); //get statement and execute appropriate select Statement stmt = con.createStatement(); String command = "SELECT distinct fname, lname, student_id FROM student"+ " where degree='"+ degree + "';"; System.out.println(command); ResultSet result = stmt.executeQuery(command); //determine number of columns ResultSetMetaData metadata = result.getMetaData(); int columns = metadata.getColumnCount(); //print heading System.out.println("\nFNAME LNAME STUD_ID"); System.out.println("===================================="); //loop through result and print columns while (result.next()){ for (int i=1; i <=columns; i++){ System.out.print(result.getString(i)+spaces(15-result.getString(i).length())); } System.out.println(); }
`
在JDBC中,您的SQL语句不应以分号结束。
更改
String command = "SELECT distinct fname, lname, student_id FROM student"+ " where degree='"+ degree + "';";
至
String command = "SELECT distinct fname, lname, student_id FROM student"+ " where degree='"+ degree + "'";