Home » Developer & Programmer » JDeveloper, Java & XML » Using IDE Database Connections in Java code (JDeveloper, 12.2.1.2.0, Windows Server 2012)
Using IDE Database Connections in Java code [message #668366] Mon, 19 February 2018 16:21 Go to next message
CMcKinstry
Messages: 3
Registered: February 2018
Junior Member
I can find plenty of examples of how to create Database Connections in JDeveloper.

What I cannot find is how to make use of any connections in actual code.

Is it even possible?

E.g. I've created a connection called testConnection and I want to use it.

This certainly doesn't work:

Connection conn = DriverManager.getConnection(testConnection);

My basic underlying problem is I need to run a Java app once a day that communicates with a bunch of databases (Oracle and other types). This app is to be automated and run from Windows. I really do not to expose passwords for this, and these Database Connections looked like a clever way of getting around this problem. I potentially need to run it from several Window Servers.

If it is possible, how do I access these Database Connections?

Thanks,
Re: Using IDE Database Connections in Java code [message #668367 is a reply to message #668366] Mon, 19 February 2018 18:05 Go to previous messageGo to next message
BlackSwan
Messages: 26640
Registered: January 2009
Location: SoCal
Senior Member
Below is a working example of how to use Java to query Oracle DB
bcm@bcm-laptop:~$ cat Conn.java
import java.sql.*;
class Conn {
  public static void main (String[] args) throws Exception
  {
   Class.forName ("oracle.jdbc.OracleDriver");
   Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//localhost:1521/XE", "scott", "tiger");
                        // @//machineName:port/SID,   userid,  password
   try {
     Statement stmt = conn.createStatement();
     try {
       ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
       try {
         while (rset.next())
           System.out.println (rset.getString(1));   // Print col 1
       } 
       finally {
          try { rset.close(); } catch (Exception ignore) {}
       }
     } 
     finally {
       try { stmt.close(); } catch (Exception ignore) {}
     }
   } 
   finally {
     try { conn.close(); } catch (Exception ignore) {}
   }
  }
}
bcm@bcm-laptop:~$ javac Conn.java
bcm@bcm-laptop:~$ export CLASSPATH=/u01/app/oracle/product/11.2.0/dbhome_1/owb/wf/lib/ojdbc14.jar:.
bcm@bcm-laptop:~$ java Conn
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
bcm@bcm-laptop:~$
Re: Using IDE Database Connections in Java code [message #668368 is a reply to message #668367] Mon, 19 February 2018 18:10 Go to previous messageGo to next message
CMcKinstry
Messages: 3
Registered: February 2018
Junior Member
Thanks, except I'm trying to avoid having to pass in a password. JDeveloper has this feature which allows you to have Database Connections (there's plenty of how-tos on that). This allows the person to input the username and password as part of the connection.

However, making use of it is another matter entirely. This is what I'm after.

Cheers,
Re: Using IDE Database Connections in Java code [message #668369 is a reply to message #668368] Mon, 19 February 2018 19:23 Go to previous messageGo to next message
BlackSwan
Messages: 26640
Registered: January 2009
Location: SoCal
Senior Member
https://docs.oracle.com/cd/B28359_01/appdev.111/b28765/getconn.htm#CACBBFHB
Re: Using IDE Database Connections in Java code [message #668386 is a reply to message #668369] Tue, 20 February 2018 12:47 Go to previous messageGo to next message
CMcKinstry
Messages: 3
Registered: February 2018
Junior Member
Thanks, that doesn't use the named database connection (testConnection). I'd still have to use the JDBC string along with username and password.

E.g.:

Note:
The login variables have been set to null to secure the application. At this point in the guide, application login functionality is yet to be built into the application. Therefore, to test the application until login functionality is built in, you can set values in the login variables as follows:
Set the jdbcUrl variable to the connect string for your database.
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
Set the variables userid and password to hr as follows:
String userid = "hr";
String password = "hr";
Make sure you reset these to null as soon as you finish testing.
For more information on security features and practices, refer to Oracle Database Security Guide and the vendor-specific documentation for your development environment.

What I'd like to do is use the Database Connection (i.e. testConnection). I'm trying to do this as a way of not making the password visible - I'm trying to write an application that is run automatically once a day. I cannot put in my batch file (that will call the Java app) the password, nor do I want to store the password within a Java class or file where it could be gotten at.

I've looked at hashing (e.g. SHA-512), which as far I can tell, worthless for connecting to a database (the password will be successfully hashed, but I cannot then use it to establish a connection, since the hashed password cannot be "un-hashed".

Thanks,

I'm open to ideas where an automated job (called once a day) can successfully have the password passed "un-exposed" in Windows.

Re: Using IDE Database Connections in Java code [message #668387 is a reply to message #668386] Tue, 20 February 2018 19:32 Go to previous messageGo to next message
BlackSwan
Messages: 26640
Registered: January 2009
Location: SoCal
Senior Member
https://stackoverflow.com/questions/7634196/what-is-correct-jdbc-url-syntax-if-oracle-wallets-are-used/9772728
Re: Using IDE Database Connections in Java code [message #669212 is a reply to message #668366] Mon, 09 April 2018 01:39 Go to previous message
rupajohn
Messages: 1
Registered: April 2018
Junior Member
Very useful information about Oracle.
Previous Topic: How Can I Call Rdf Report,and 11g form through Jsp Pages in Oracle Jdeveloper
Next Topic: Concat XML data in a loop
Goto Forum:
  


Current Time: Sun Dec 08 08:17:09 CST 2019