1

I'm trying to write a java code to connect to an Oracle9i Enterprise Edition Release 9.2.0.4.0. My machine is windows XP. The Oracle DB OS is Solaris 8. What I've done currently are:

java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

I'm not sure if the ODBC of the Oracle DB is using any username and password or not because the client did not provide me with that. The only thing provided is the Oracle DB Server Hostname. But I need to be ready in case they do have username and password. So assuming now I already have the username, password, and the hostname. How do I use these parameters to do the connection? Do I need to download any jdbc driver and where do I put it? I'm totally lost because I do not have any Solaris 8 and Oracle 9 to do testing. Because from what I've found, the connection strings/url varies for version, OS, etc.

user1409217
  • 412
  • 11
  • 29
  • 3
    Why are you using ODBC? Why not just use oracle drivers directly (`oracle.jdbc.driver.OracleDriver`)? – Aleks G Jul 13 '12 at 08:40
  • Didn't know that. I thought Java is under oracle so why wouldn't ODBC work. Anyway I'll give your solution a try. – user1409217 Jul 13 '12 at 11:25
  • ODBC _would_ work, but it's not the optimal way. Direct Oracle JDBC drivers are much more efficient. – Aleks G Jul 13 '12 at 12:28

2 Answers2

4

Rather than using ODBC to connect to the database, I would strongly recommend that you use oracle driver directly. You can download it for free from Oracle - it's a small jar file: http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html

Then you can connect to the database using this code:

Connection connection = null;
try {
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = "sun.host.name.or.ip.address";
    String portNumber = "1521";
    String sid = "dbname";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
    // Could not find the database driver
} catch (SQLException e) {
    // Could not connect to the database
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • +1. I was grepping through my code to find about the same thing but you were faster. That's the correct way to connect to Oracle. – Denys Séguret Jul 13 '12 at 08:47
0

I think there was a typo mistake. The database part of the connection string should be splitted by a slash (/) instead of colon (:). Correct code should be as below for the corresponding line:

... String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + sid; ...