4 Replies Latest reply: Jan 11, 2012 5:50 AM by EJP RSS

    Get a tns connection through JDBC.

    878867

      Hi

      I am trying to get tns connection using jdbc to the dabatase. The following code returns an exception "SQLException: IO Error: Unknown host specified ". Most likely becuase the string is missing a host address. Does a tns connection require a host to be included? I am under the assumption, there are only 3 tns properties - username, password and a Net Service Name (e.g. ORCL) . Thanks.

      string url = jdbc:oracle:thin:@//username/password/service_name";

      try {
      Class.forName("oracle.jdbc.OracleDriver");
      } catch (java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      e.printStackTrace();
      }

      try {

      Connection connection = DriverManager.getConnection(url);
      new ConnectionInfo(connection);
      } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      }

      Edited by: 875864 on 10-Jan-2012 16:53

        • 1. Re: Get a tns connection through JDBC.
          rp0428
          The thin driver needs the host; it doesn't use the tnsnames.ora file

          Here is a simple set of code to get a connection
                  String driver   = "oracle.jdbc.OracleDriver";
                  String url      = "jdbc:oracle:thin:@host:port:sid";
                  String username = "SCOTT";
                  String password = "tiger";
          
                  try {
                      Class.forName(driver);
                      Connection con = DriverManager.getConnection(url, username, password);
                 ...
          This link shows how to use the thin or the OCI (thick) drivers. The OCI driver needs an Oracle client and will use tnsnames.ora

          http://www.orafaq.com/wiki/JDBC
          • 2. Re: Get a tns connection through JDBC.
            878867
            I am aware of how to get a basic connection. The question was about how to set up a tns connection.

            Thanks
            • 3. Re: Get a tns connection through JDBC.
              gimbal2
              So what stopped you from typing "oracle jdbc tns" into google? The first hit I got gives explicit instructions on how to do it. That is less than 5 seconds of work, it takes 5 seconds for this forum to post your question.

              http://java.sogeti.nl/JavaBlog/2008/02/03/oracle-thin-jdbc-to-tns-name/
              • 4. Re: Get a tns connection through JDBC.
                EJP
                it takes 5 seconds for this forum to post your question.
                ... with no guarantee of ever getting an answer at all ...