Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to compile connection pool sample java code

stuartuDec 1 2010 — edited Dec 6 2010
I recently bought David Knox's "Effective Oracle Database 10g Security by Design", and have been working through his examples with client identifiers and how to leverage database security with anonymous connection pools.

The database side of things I totally understand and have set up, but I now want to compile/run the java code examples, but don't know what is required to compile this.

I'm running Oracle 10.2.0.4 (64-bit) Enterprise Edition on a Linux (RHEL 5) PC. Java version is 1.6.0_20. Relevant .bash_profile environment variables are as follows:
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/jre:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin

When I try to compile, I get:
oracle:DB10204$ java FastConnect.java
Exception in thread "main" java.lang.NoClassDefFoundError: FastConnect/java
Caused by: java.lang.ClassNotFoundException: FastConnect.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: FastConnect.java. Program will exit.

The java source code of one of the examples is below. Is someone able to point me in the right direction as to how I get this to compile? Do I just have a syntax and/or environment configuration modification to do, or is there more to it than that to get this example working? Any help much appreciated.
oracle:DB10204$   cat FastConnect.java 
package OSBD;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

public class FastConnect
{
  public static void main(String[] args)
  {
    long connectTime=0, connectionStart=0, connectionStop=0;
    long connectTime2=0, connectionStart2=0, connectionStop2=0;
    ConnMgr cm = new ConnMgr();
    // time first connection. This connection initializes pool.
    connectionStart = System.currentTimeMillis();
    Connection conn = cm.getConnection("SCOTT");
    connectionStop = System.currentTimeMillis();
    String query = "select ename, job, sal from person_view";
    try {
      // show security by querying from View
      Statement stmt = conn.createStatement();
      ResultSet rset = stmt.executeQuery(query);
      while (rset.next()) {
        System.out.println("Name:   " + rset.getString(1));
        System.out.println("Job:    " + rset.getString(2));
        System.out.println("Salary: " + rset.getString(3));
    }
      stmt.close();
      rset.close();
      // close the connection which resets the database session
      cm.closeConnection(conn);
      // time subsequent connection as different user
      connectionStart2 = System.currentTimeMillis();
      conn = cm.getConnection("KING");
      connectionStop2 = System.currentTimeMillis();
      // ensure database can distinguish this new user
      stmt = conn.createStatement();
      rset = stmt.executeQuery(query);
      while (rset.next()) {
        System.out.println("Name:   " + rset.getString(1));
        System.out.println("Job:    " + rset.getString(2));
        System.out.println("Salary: " + rset.getString(3));
      }
      stmt.close();
      rset.close();
      cm.closeConnection(conn);
    } catch (Exception e)    { System.out.println(e.toString()); }
    // print timing results
    connectTime = (connectionStop - connectionStart);
    System.out.println("Connection time for Pool: " + connectTime + " ms.");
    connectTime2 = (connectionStop2 - connectionStart2);
    System.out.println("Subsequent connection time: " +
                        connectTime2 + " ms.");
  }
}
Code download is at: http://www.mhprofessional.com/getpage.php?c=oraclepress_downloads.php&cat=4222
I'm looking at Chapter 6.
This post has been answered by EJP on Dec 5 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2011
Added on Dec 1 2010
12 comments
923 views