我的查询不起作用,遇到问题。这是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中的查询完全相同。
可能是因为它想要一个字符而不是字符串吗?如果是这样的话,那么我该如何将"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 + "'";