3 Replies Latest reply: Mar 31, 2014 2:51 AM by Jani Rautiainen-Oracle RSS

    JDBC connectivity on oracle database cloud

    43bdb272-cf53-4f63-bb03-02cee2df5adb

      I want to connect have a jdbc connection form my application on java cloud to database cloud. Can anyone please provide me with the connectivity string that I should write.

        • 1. Re: JDBC connectivity on oracle database cloud
          Jani Rautiainen-Oracle

          I don't think you can use JDBC URL to directly connect to the DB, instead you can use JNDI to get handle on the data source, see following example:

          import java.sql.Connection;

           

          import java.sql.ResultSet;

          import java.sql.Statement;

           

          import java.util.Hashtable;

           

          import javax.naming.Context;

          import javax.naming.InitialContext;

           

          ...

              public void test() {

                  Context ctx = null;

                  Hashtable ht = new Hashtable();

                  ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

                  Connection conn = null;

                  Statement stmt = null;

                  ResultSet rs = null;

           

                  try {

                      ctx = new InitialContext(ht);

                      javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("<database>");

                      conn = ds.getConnection();

                      stmt = conn.createStatement();

                      stmt.execute("select count(1) TEST_COUNT from <some_table>");

                      rs = stmt.getResultSet();

                      rs.next();

                      int count = rs.getInt("TEST_COUNT");

                      stmt.close();

                      stmt = null;

                      conn.close();

                      conn = null;

                  } catch (Exception e) {

                      throw new RuntimeException(e);

                  } finally {

                      try {

                          ctx.close();

                          if (rs != null)

                              rs.close();

                          if (stmt != null)

                              stmt.close();

                          if (conn != null)

                              conn.close();

                      } catch (Exception e) {

                          throw new RuntimeException(e);

                      }

                  }

              }

          }

           

          Where "<database>" is the reference to your database (available e.g. from the java console) and the "<some_table>" is the name of a table that exists in your database.

          --

          Jani Rautiainen

          Fusion Applications Developer Relations

          https://blogs.oracle.com/fadevrel/

          • 2. Re: JDBC connectivity on oracle database cloud
            43bdb272-cf53-4f63-bb03-02cee2df5adb

            Sorry but i have one problem, what to put in "your databse name" , i have my database as "javatrial5196db" in java console , but when i put this up , it keeps giving me errors.

            • 3. Re: JDBC connectivity on oracle database cloud
              Jani Rautiainen-Oracle

              Your DB name looks like correct format, as long as the name is the one associated with your JCS it should work. What is the error that you see ? You did also change the value of "<some_table>" to a name of a table that exists in your database ?

              --

              Jani Rautiainen

              Fusion Applications Developer Relations

              https://blogs.oracle.com/fadevrel/