Search code examples
javaoracle-databasejdbcodbcsolaris

How to connect to Oracle DB in Solaris OS using JDBC?


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.


Solution

  • 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
    }