5 Replies Latest reply: Aug 26, 2013 5:13 PM by Russell M RSS

    How do you use a database connection in java application

    Russell M

      Hi,

       

      In verion 10 of jdeveloper we used a datasource object  and the following code to connect to our database.

            Context ic = new InitialContext();

            DataSource ods = (DataSource) ic.lookup("jdbc/mps");

            con = ods.getConnection();

       

      our datasource defines  mps as the name.  The ic.lookup finds it in the jdbc tree.

       

      Now in version 11 we created a database connection and referenced it in the ic.lookup call but it does not work.  we cannot seem to find how to connect to the database connection  from the application.  How is this done now?  I also don't see a way to create a datasource entry via the scripts.

       

      Thanks for your help.

       

      Russell

        • 2. Re: How do you use a database connection in java application
          Russell M

          Hi,

           

          Thanks for the reply.  I have seen this document but it does not address what I am trying to do.  This application needs to run in several different environments.  I need to be able to create a datasource external to the application and then find it via the ic.lookup method.  The document you listed describes how to create a datasource via java code and then look it up with the ic.lookup method.  I need to create the datasource in jdeveloper in such a way that I can look it up with the ic.lookup method.  That way I can change the datasouce externally and the code does not need to change.

          Does the connection creation script also create a datasource?  If so what is the look up path?  (it does not appear to be jdbc/myName as it used to be).

           

          Thanks,

          • 3. Re: How do you use a database connection in java application
            aksarben

            We have the exact problem you do: Connecting to different instances for dev, test & prod. In our case, the dev, test & prod servers each maintains a Java system variable indicating the environment (e.g., runtime.environment). So, in a Spring configuration file, we have an entry like this:

             

            <bean id="dataSource" class="my.datasource.class.name">

                    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />

                    <property name="url" value="jdbc:oracle:thin:@${db_instance}.oracle.ourcompany.com:1234:${db_instance}" />

                    <property name="username" value="${db_user}" />

                    <property name="password" value="${db_password}" />

                </bean>

             

            The properties for db_instance, db_user & db_password are defined in a separate file for each instance. We tell Spring to read in the environment specific property file something like this:

             

            <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:db-${runtime.environment}.properties</value>
            </property>

                </bean>

             

            There are, of course, other ways to skin this cat.

            • 4. Re: How do you use a database connection in java application
              Russell M

              Hi,

               

              I have some additional information about what we have used in the past.

               

              In the tools menu of Jdeveloper 10.xxx there is an "embedded oc4j preferences..." item.    Within that menu there is a dataSources option that allows you to create a dataSources.xml file with information about your database connections and can be accessed and used via JNDI ie the ic.lookup() method of the context.

              That option does not seem to be available in jdev  version 11.  So the question is what is the procedure to accomplish this task in jdev version 11?

               

              Thanks,

               

              Russell

              • 5. Re: How do you use a database connection in java application
                Russell M

                HI,

                 

                Just for everyone's edification, we found the corresponding process for jdev version11.  Since version 11 uses weblogic instead of the oc4j server, the datasource must be defined in weblogic.  You login to the weblogic server console and in the domain section there is a catagory called data source.  Using the datasource tab you can enter the jndi name and the database information.  This is then referenced using the ic.lookup("jndiName"); exactly as before.

                 

                Thanks,

                Russell