我的查询无法正常工作。这是command变量。
当它执行时,应该检索具有BSc作为其学位的元组。我已经在oracle中直接测试过了,查询返回了这些。它与command声明相同。
当我打印输出时command,该行看起来与在oracle中工作的命令完全相同。
SELECT distinct fname, lname, student_id FROM student where degree='BA';
但是,它应该打印到屏幕上。该表已被加载到oracle中。
我一直在为这个问题绞尽脑汁,但似乎找不到解决方法!
我不断得到的错误是: ORA-00911: invalid character
我要做的是将degree扫描器的结果存储在字符串中。因此,将其串联在command变量中应该不会造成问题-
查询看起来与在oracle中起作用的查询相同。
可能是因为它想要一个char而不是一个字符串吗?如果是这样,那么我将如何使其成为字符型“ 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();
}
`