Oracle invalid identifier doesnt understand string

自古美人都是妖i 提交于 2019-12-02 10:31:00

问题


I'm having an issue with my query not working. This is the command variable.

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.

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';

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!

The error I keep getting is: ORA-00911: invalid character

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.

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.

Relevant code below:

 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();
        }

`


回答1:


In JDBC your SQL statement should not be terminated by semicolon.

Change

String command = "SELECT distinct fname, lname, student_id FROM student"+
       " where degree='"+ degree + "';";

to

String command = "SELECT distinct fname, lname, student_id FROM student"+
       " where degree='"+ degree + "'";


来源:https://stackoverflow.com/questions/25958116/oracle-invalid-identifier-doesnt-understand-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!