2 Replies Latest reply: May 25, 2009 3:17 PM by 843789 RSS

    Application connecting to JavaDB using a Datasource, what am I missing

    843789
      Dear all,

      I'm looking into java while writing an application to do some simulation on stock trading scenarios. However, I'm having trouble being able to establish a connection to the database in my application.

      I'm using Netbeans 6.5.1, and connecting to the database in the "Services window" is no problem. Furthermore, I've created the tables and fields required for testing.

      However, the following code:
      try {
      //System.setProperty("Context.INITIAL_CONTEXT_FACTORY";, "org.apache.derby.jdbc.EmbeddedDriver";);
      
      //Properties p = System.getProperties();
      //p.list(System.out);
      
      
      Context ic = new InitialContext();
      ic.addToEnvironment("Context.INITIAL_CONTEXT_FACTORY";, "org.apache.derby.jdbc.EmbeddedDriver";);
      DataSource ds = (DataSource) ic.lookup("jdbc/history");
      Connection con = ds.getConnection();
      } catch (javax.naming.NamingException e) {
      System.err.println("NamingException: " + e.getMessage() );
      } catch (java.sql.SQLException e) {
      System.err.println("SQLException: " + e.getMessage());
      }
      throws a NamingException, "Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:java.naming.factory.initial";. It looks like the exception is thrown when I call a method of ic.

      This seemed trivial in the tutorial, but I'm obviously missing something. :-) I've looked at several sites andtried a countless number of variations of the above code, but so far to no avail. Guess there is some obvious detail I'm missing, could someone please point me in the correct direction?


      Kind regards
      Karl Martin Lund
        • 1. Re: Application connecting to JavaDB using a Datasource, what am I missing
          783403
          Are you using any application server ? If yes, then the data sources should be define there.
          If no, the use the direct connections to the DB by using connection strings with the JDBC APIs.
          • 2. Re: Application connecting to JavaDB using a Datasource, what am I missing
            843789
            Thanks for your input. I've read that the DataSource is the preferred way to connect to a database over DriverManager for performance and portability reasons. But it this only true when an application server is in the picture? (It's not in my case). Anyway, in my case, I'm the only one using the application, and performance and portability are not too important.

            However, I've now changed the code to read:
                    try {
                        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                        Connection con = DriverManager.getConnection("jdbc:derby:history");
                    } catch (java.lang.ClassNotFoundException e) {
                        System.err.println("ClassNotFoundException: " + e.getMessage());
                    } catch (java.sql.SQLException e) {
                        System.err.println("SQLException: " + e.getMessage());
                    }
            Then I get ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver. Guess I need to look at my classpath?

            I also found a document called "Java DB Developer's Guide". This document states that "If your application runs on JDK 1.6 or higher, then you do not need to explicitly load the EmbeddedDriver. In that environment, the driver loads automatically." I know that my JRE is version 1.6, and I think this also apply to the JDK? Anyway, I tried to just leave out the "Class.forname..."-line, but then I got a SQLException saying "No suitable driver found for jdbc:derby:history".

            Kind regards
            Karl Martin Lund