tags:

views:

38

answers:

3

Hello,

This is my first Java application I am creating (using Eclipse IDE) and the second Oracle based app (I'm a .NET/MSSQL guy for years). The first Oracle app I wrote in .NET did not have any issues, and I'm trying to connect to the same server.

  • I have installed:
    • 'Java 2 Platform, Enterprise Edition 1.4 SDK'
    • 'Java DB `10.5.3.0' -'Java(TM) 6 Update 21
    • 'Java(TM) SE Development Kit 6 update 21
    • 'Oracle IRM Client' (11g)
    • Oracle 11g Release 2 JDBC Drivers (ojdbc6.jar)

My code is very simple. Here it is:

  OracleDataSource ods = new OracleDataSource();
            ods.setURL("jdbc:oracle:oci:@");
            ods.setUser("username");
            ods.setPassword("password");
            ods.setServerName("servername");
            ods.setPortNumber(1549);
            ods.setServiceName("foo.myservice.com");
   Connection conn = ods.getConnection();

I get below exception:

Exception in thread "main" java.sql.SQLException: ORA-12560: TNS:protocol adapter error

at oracle.jdbc.driver.T2CConnection.checkError(T2CConnection.java:737)
at oracle.jdbc.driver.T2CConnection.logon(T2CConnection.java:401)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T2CConnection.<init>(T2CConnection.java:148)
at oracle.jdbc.driver.T2CDriverExtension.getConnection(T2CDriverExtension.java:53)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:280)
at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:207)
at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:157)
at Select.GetScalar(Select.java:47)
at Job.Run(Job.java:20)
at Main.main(Main.java:19)

I have google'd the hack out of this.. I've tried adding a 'TNS entry to the tnsnames.ora file'. I've tried adding '##NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)' to the sqlnet.ora file. I've tried various other things but nothing is working.

Has anyone experienced this before and has any clue on how to get this to work?? Am I using the wrong version? Server is remote (I don't have Oracle server installed locally, just client). Maybe I have wrong version of Java SDK or the wrong version of the JDBC .jar file?? I just need to connect to Oracle and run a single simple query! Thanks much for any help.

+1  A: 

Try using the type IV JDBC driver instead of OCI if you can. The thin url looks like this:

jdbc:oracle:thin:@host[:port]/service

I'd try code that looked more like this (fill in your defaults for the driver, URL, username, and password):

    package persistence;

    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class DatabaseUtils
    {

        private static final String DEFAULT_DRIVER = "";
        private static final String DEFAULT_URL = "";
        private static final String DEFAULT_USERNAME = "";
        private static final String DEFAULT_PASSWORD = "";

        public static void main(String[] args)
        {
            String driver = ((args.length > 0) ? args[0] : DEFAULT_DRIVER);
            String url = ((args.length > 1) ? args[1] : DEFAULT_URL);
            String username = ((args.length > 2) ? args[2] : DEFAULT_USERNAME);
            String password = ((args.length > 3) ? args[3] : DEFAULT_PASSWORD);

            Connection connection = null;

            try
            {
                connection = createConnection(driver, url, username, password);
                DatabaseMetaData meta = connection.getMetaData();
                System.out.println(meta.getDatabaseProductName());
                System.out.println(meta.getDatabaseProductVersion());
            }
            catch (Exception e)
            {
                e.printStackTrace(); 
            }
            finally
            {
                close(connection);
            }
        }

        public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
        {
            Class.forName(driver);

            if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0))
            {
                return DriverManager.getConnection(url);
            }
            else
            {
                return DriverManager.getConnection(url, username, password);
            }
        }

        public static void close(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }


        public static void close(Statement st)
        {
            try
            {
                if (st != null)
                {
                    st.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        public static void close(ResultSet rs)
        {
            try
            {
                if (rs != null)
                {
                    rs.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        public static void rollback(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.rollback();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        public static List<Map<String, Object>> map(ResultSet rs) throws SQLException
        {
            List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

            try
            {
                if (rs != null)
                {
                    ResultSetMetaData meta = rs.getMetaData();
                    int numColumns = meta.getColumnCount();
                    while (rs.next())
                    {
                        Map<String, Object> row = new HashMap<String, Object>();
                        for (int i = 1; i <= numColumns; ++i)
                        {
                            String name = meta.getColumnName(i);
                            Object value = rs.getObject(i);
                            row.put(name, value);
                        }
                        results.add(row);
                    }
                }
            }
            finally
            {
                close(rs);
            }

            return results;
        }
    }
duffymo
Thanks a lot for the code sample. But where can I download the jar file for the thin driver? Or are you saying I need only change the URL?? On the above linked page it says 'thin driver' but just has a 'JavaDoc' file and a README with just instructions...
dferraro
Can you provide me with the string that should go into the 'driver' here?? this is what always confused me and why I liked the OracleConnection class. This dynamic class creation stuff just 'smells' to me...
dferraro
OracleConnection ties you to Oracle forever. That's a bigger smell to me. The driver class name is the one in your ojdbc6.jar: Looks like it's oracle.jdbc.driver.OracleDriver.
duffymo
A: 

If you want something simple, you should try using the THIN client instead of OCI client. Don't forget to include the right jar (ojdbc5.jar for Java 5, ojdbc6.jar for Java 6).

gawi
where to download thin client? or is it already inside ojdbc6.jar and I just change the URL?? I didnt see download for thin client on above oracle JDBC drivers link...
dferraro
It's part of ojdbcXX.jar. You need to change "oci" to "thin" in the URL. Note that the URL format is not the same for thin driver and OCI driver. See http://www.orafaq.com/wiki/JDBC
gawi
thanks. but why use this reflection magic (e.g. Class.forName) instead of good old static typing??
dferraro
It's just to force the OracleDriver class to be loaded. There is some static code being executed during this class initialization, the driver register itself to the DriverManager.
gawi
A: 

Is the ServiceName you specified the service name of the Oracle instance you're trying to connect to? You're sure the port is correct?

DCookie
Yes, and Yes. Thanks!
dferraro
same exact 'connection string' parameters are being used in .NET application without issues (service name, port number, etc..)
dferraro
.NET and Java aren't the same in this case. That explains why you're using OCI driver, because .NET can't use the thin driver. If you're using Java, switch to thin.
duffymo