2014-09-21 39 views
1

我遇到問題,查詢無法正常工作。這是command變量。Oracle無效標識符does not understand字符串

當它執行它應該是檢索具有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(); 
     } 

`

+0

你肯定不包括在'degree'字符串中的任何空白/換行符?請通過調試器和「equals」驗證查詢是否相同。 – Smutje 2014-09-21 10:16:27

回答

2

在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 + "'";