24

I am making Java 1.6-JDBC-Oracle 11 code. I created a table called employee with id,name and age. I am getting the error - ORA-00911: invalid character. How can I fix this ?

Here is my code-

import java.sql.*;
import java.util.Properties;
import java.io.IOException;
import java.io.FileInputStream;

public class HelloOracle {

    static String query =
        "SELECT emp_id, emp_name, emp_age " +
        "FROM employee;";

    public static void main(String[] args) {
        String username = "";
        String password = "";
        Properties prop = new Properties();
        try {
            FileInputStream fis = new FileInputStream("Login.properties");
            prop.load(fis);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        username = prop.getProperty("username").trim();
        password = prop.getProperty("password").trim();

        try {
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", username, password);

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                System.out.print(rs.getString("emp_id"));
                System.out.print("  ,  ");
                System.out.print(rs.getString("emp_name"));
                System.out.print("  ,  ");
                System.out.print(rs.getString("emp_age"));
                System.out.println();
            }
        } catch (SQLException e) {
            System.out.println("Exception: " + e);
        }

    }

}

Unfortunately, oracle error messages are not as informative as mysql or mssql and I am not able to trouble shoot them easily. I am also not able to see which line of code caused the exception.

Enigo
  • 3,685
  • 5
  • 29
  • 54
sweet dreams
  • 2,094
  • 13
  • 32
  • 46

3 Answers3

52

Try removing the semi colon from the end of your SQL statement.

ie

static String query = "SELECT emp_id, emp_name, emp_age " +
    "FROM employee"; // no trailing ";" in the SQL
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

remove the ; from inside the query

Mehdi
  • 1,340
  • 15
  • 23
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13636895) – xenteros Sep 11 '16 at 10:16
  • It does provide a clear and simple answer to the question. – Mehdi Sep 11 '16 at 21:35
0

Bohemian is exactly right. I don't see why this was so hard. If you pop the message into Google, you'll get this:

http://www.dba-oracle.com/sf_ora_00911_invalid_character.htm

The semi-colon is the first problem noted.

Another recommendation: Don't do this.

catch(SQLException e){System.out.println("Exception: " + e);}

Do this instead:

catch(SQLException e){
    e.printStackTrace(); // better yet, log it.
}

It'll give you lots more information.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Please upvote the question from -1 to 0. The ; works for create table statement too. I thought that its better to use it for select also. – sweet dreams Aug 08 '12 at 06:57
  • I down voted the question because cutting & pasting the error message into a search engine brought an answer back. It's your lack of initiative that I find worthy of the down vote, not the use of the semi-colon. – duffymo Aug 08 '12 at 09:21
  • 1
    I upvoted the question because `ORA-00911: invalid character jdbc` in Google returns this question as the first link. – Justin Skiles Jan 29 '13 at 21:29
  • Says less about the question's value and more about Page Rank and SEO. – duffymo Jan 29 '13 at 23:56
  • Can't imagine why this was voted down four years after the fact. True then; true today. – duffymo May 26 '16 at 19:33