Oracle无效标识符不理解字符串 [英] Oracle invalid identifier doesnt understand string
问题描述
我的查询无法解决问题。这是命令
变量。
I'm having an issue with my query not working. This is the command
variable.
当它执行时,它应该检索具有BSc作为其学位的元组。我已经在oracle中直接测试了这个,查询返回了这些。它与命令
语句相同。
When it executes it should be retrieving the tuples that have BSc as their degree. I have tested this in oracle directly and the query returns these. It is identical to the command
statement.
当我打印出命令时
,该行与我在oracle中运行的命令完全相同。
When I print out command
, the line looks exactly the same as my command that worked in oracle.
SELECT distinct fname,lname,student_id FROM student where degree ='BA';
然而,它应该打印到屏幕上。这些表已经加载到oracle中了。
Yet, it should be printing out to the screen. The tables are loaded into oracle already.
我一直在用这个问题绞尽脑汁但似乎无法找到解决办法!
I've been racking my brain with this issue but can't seem to find a fix!
我一直得到的错误是:
ORA-00911:无效字符
The error I keep getting is:
ORA-00911: invalid character
我所做的是将度
存储在扫描仪的结果中,这是一个字符串。因此,在命令
变量中连接它不应该产生问题 - 查询看起来与oracle中的相同。
What I do is I store in degree
the result from scanner which is a string. So concatenating it in the command
variable shouldn't make an issue -- the query looks identical to what works in oracle.
可能是因为它想要一个字符而不是一个字符串?如果确实如此,那我怎么能让它把BSc作为一个炭?连接字符听起来很愚蠢。
Could it be because it wants a char instead of a string? If it does, then how would I get it to make "BSc" as a char? Concatenating chars sounds dumb.
下面的相关代码:
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语句不应以分号结束。
In JDBC your SQL statement should not be terminated by semicolon.
更改
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 + "'";
这篇关于Oracle无效标识符不理解字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!