0

I am using eclipse and java to connect to an oracle 12c database. But every time I run through the process, I get a connection fail error that reads:

java.sql.SQLException: Invalid Oracle URL specified

Here is what my code looks like:

import java.sql.*;

public class JdbcConnection {
    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:xe", "username", "password");
            Statement statement = con.createStatement();
            String sql = "select * from employees";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next())
                System.out.println(rs.getInt(3) + " " + rs.getString(2));
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

I am using a windows 7 sp1 Ultimate (64 bit)
CPU: i5 2.85 ghz
RAM: 8GB

Oracle is installed in a separate hard drive (in the same computer).

EDIT:
I have tried the first suggestion from this post: How do you find out the Oracle database's URL? and I get an error that says:

ORA-00928: SELECT KEYWORD MISSING...

This error occurs when I run the oracle "SQL DEVELOPER" application on the startup menu and type the suggestions from post above. Note: I want to connect to the database from eclipse and query it programatically. Is there a way to find out what the correct connection URL is? Please help.

Keale
  • 3,924
  • 3
  • 29
  • 46
JayRod
  • 157
  • 1
  • 13
  • 2
    If you're getting that error, I think you've connected to Oracle. What's your real query? Also, it's a bad idea to rely on `*` always evaluating to the hard-coded columns you have currently; what are columns `3` and `2`? And, you haven't needed `Class.forName("oracle.jdbc.driver.OracleDriver");` since [JDBC 4](http://stackoverflow.com/a/5484254/2970947). – Elliott Frisch Apr 15 '16 at 14:26
  • Ah Yes, so let me clarify. The first part of the post is referring to the code I am running in eclipse. On the other hand, the ORA-00928 error, I get when I run the oracle application and I type in what that post suggested suggested. – JayRod Apr 15 '16 at 14:32
  • 2
    Above the code sample, you give one error, and below it you give another. Elliot is right -- based on the error given below the code, you're connecting to Oracle. ORA-00928 is an Oracle-generated error message saying that your query is not valid. Since there's nothing wrong with "select * from employees", I'm assuming what you've posted here is a dumbed-down example. Can you clarify the question? – JakeRobb Apr 15 '16 at 14:33
  • I'm not sure I understand the difference between "running in eclipse" and "run the oracle application". You've given one piece of sample code, and there is nothing wrong with it that would cause either of the errors. – JakeRobb Apr 15 '16 at 14:37
  • I mean that the error "ORA-00928" happens when I run the ORACLE "SQL DEVELOPER" application that appears on my start up menu when I installed Oracle. On the other hand, the invalid java.sql.SQLException: Invalid Oracle URL specified error happens when I use the code above in eclipse. I Thought that I'd go to the SQL Developer application in order to find out what server and ports it is using. – JayRod Apr 15 '16 at 14:43
  • The first part is a trivial typo missing a colon before the @ symbol: `jdbc:oracle:thin:@localhost:1521:xe`. But you should check your SQL Developer connection to see if it's using the SID or service name; you may want it to end with `/xe` instead of `:xe`. For the second part you haven't shown the query you're running; the post you linked to doesn't have a query, and the one you do show above can't get that error.. – Alex Poole Apr 15 '16 at 14:57
  • Basically I am trying to find out the connection URL (if possible). I ran the suggestion from the post thinking that it would give me the connection URL or at least the information needed to put it together eg severname, port number, SID etc. – JayRod Apr 15 '16 at 15:08
  • 1
    I don't understand what you ran - there are no queries in that linked post, so what suggestion are you referring to? You can see how SQL Developer is connecting by hovering over the connection you're using the connections panel on the left. That might not be in the format you expect but will show the host, port and service name/SID. Or right-click on the connection and choose Properties. – Alex Poole Apr 15 '16 at 15:22
  • Thanks Alex poole!
    That solved my question upon following your directions, I found out that I was using the wrong SID. The :xe had to be changed to /orcl It works now.
    – JayRod Apr 15 '16 at 21:05

0 Answers0