无法从 Java 连接到 Oracle DB - ORA-12560: TNS: 协议适配器错误

发布于 2024-09-17 17:16:08 字数 2086 浏览 6 评论 0原文

这是我创建的第一个 Java 应用程序(使用 Eclipse IDE),也是第二个基于 Oracle 的应用程序(多年来我一直是 .NET/MSSQL 人员)。我在 .NET 中编写的第一个 Oracle 应用程序没有任何问题,并且我正在尝试连接到同一服务器。

  • 我已经安装了:
    • “Java 2 平台企业版 1.4 SDK”
    • 'Java DB `10.5.3.0' -'Java(TM) 6 更新 21
    • “Java(TM) SE 开发套件 6 更新 21
    • “Oracle IRM 客户端”(11g)
    • Oracle 11g 第 2 版 JDBC 驱动程序 (ojdbc6.jar)

我的代码非常简单。在这里:

  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();

我得到以下异常:

线程“main”java.sql.SQLException中的异常:ORA-12560:TNS:协议适配器错误

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)

我已经用谷歌解决了这个问题..我尝试添加“TNS” tnsnames.ora 文件的条目'。我尝试将 '##NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)' 添加到 sqlnet.ora 文件中。我尝试过其他各种方法,但没有任何效果。

有没有人以前经历过这种情况并且有任何关于如何让它发挥作用的线索?我使用了错误的版本吗?服务器是远程的(我本地没有安装Oracle服务器,只有客户端)。也许我的 Java SDK 版本错误或 JDBC .jar 文件版本错误?我只需要连接到 Oracle 并运行一个简单的查询!非常感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2024-09-24 17:16:08

如果可以的话,尝试使用 IV 型 JDBC 驱动程序而不是 OCI。细网址看起来像这样:

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

我会尝试看起来更像这样的代码(填写驱动程序、URL、用户名和密码的默认值):

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

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;
        }
    }
梦明 2024-09-24 17:16:08

如果您想要简单的东西,您应该尝试使用 THIN 客户端而不是 OCI 客户端。
不要忘记包含正确的 jar(对于 Java 5 为 ojdbc5.jar,对于 Java 6 为 ojdbc6.jar)。

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).

晚风撩人 2024-09-24 17:16:08

您指定的 ServiceName 是您尝试连接的 Oracle 实例的服务名称吗?您确定端口正确吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文