Connecting java app. (JBuilder) with .jds database?

So I'm starting with an application I have to make for a certain class in college and I already stumble onto a large problem at the start of my project. I'm planning on getting it to work in JBuilder, even though I hate that program. I made a database in JDatastore and I'm trying to connect to it by using a connection pool I made. (more like "jacked from the book")

This is the driver I'm trying to use and also the name of the database:

Code:
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
       pool.setURL("MuseumDatabase.jds");

Which will be implemented in:

Code:
Class.forName(driver);

And..

Code:
con = DriverManager.getConnection(url,
      username, password);



This is EXACTLY how the book does it, yet it doesn't work for me. They used a database created in Access though (.mdb), but mine was made in JDataStore(.jds).

I test the connection pool in a JSP files I made and I run it on a Tomcat 4.0 server, which isn't the latest version, but this is the exact output I'm getting:

No suitable driver


If anyone could help me with whatever I'm doing wrong, I would be very grateful.




I'll also append all my code to this post:

PooledConnection.java (the problem isn't here)

Code:
package asa_prototype;

import java.sql.*;
import java.io.*;

public class PooledConnection implements Serializable{

  // Real JDBC Connection
  private Connection connection = null;
  // boolean flag used to determine if connection is in use
  private boolean inuse = false;

  // Constructor that takes the passed in JDBC Connection
  // and stores it in the connection attribute.
  public PooledConnection(Connection value) {

    if ( value != null ) {

      connection = value;
    }
  }

  // Returns a reference to the JDBC Connection
  public Connection getConnection() {

    // get the JDBC Connection
    return connection;
  }

  // Set the status of the PooledConnection.
  public void setInUse(boolean value) {

    inuse = value;
  }

  // Returns the current status of the PooledConnection.
  public boolean inUse() {

    return inuse;
  }

  // Close the real JDBC Connection
  public void close() {

    try {

      connection.close();
    }
    catch (SQLException sqle) {

      System.err.println(sqle.getMessage());
    }
  }
}

ConnectionPool.java (the problem shouldn't be here either)

Code:
package asa_prototype;

import java.io.*;
import java.sql.*;
import java.util.*;

public class ConnectionPool implements Serializable{

  // JDBC Driver Name
  private String driver = null;
  // URL of database
  private String url = null;
  // Initial number of connections.
  private int size = 0;
  // Username
  private String username = new String("");
  // Password
  private String password = new String("");
  // Vector of JDBC Connections
  private Vector pool = null;

  public ConnectionPool() {

  }

  // Set the value of the JDBC Driver
  public void setDriver(String value) {

    if ( value != null ) {

      driver = value;
    }
  }

  // Get the value of the JDBC Driver
  public String getDriver() {

    return driver;
  }

  // Set the URL Pointing to the Datasource
  public void setURL(String value ) {

    if ( value != null ) {

      url = value;
    }
  }

  // Get the URL Pointing to the Datasource
  public String getURL() {

    return url;
  }

  // Set the initial number of connections
  public void setSize(int value) {

    if ( value > 1 ) {

      size = value;
    }
  }

  // Get the initial number of connections
  public int getSize() {

    return size;
  }

  // Set the username
  public void setUsername(String value) {

    if ( value != null ) {

      username = value;
    }
  }

  // Get the username
  public String getUserName() {

    return username;
  }

  // Set the password
  public void setPassword(String value) {

    if ( value != null ) {

      password = value;
    }
  }

  // Get the password
  public String getPassword() {

    return password;
  }

  // Creates and returns a connection
  private Connection createConnection() throws Exception {

    Connection con = null;

    // Create a Connection
    con = DriverManager.getConnection(url,
      username, password);

    return con;
  }

  // Initialize the pool
  public synchronized void initializePool() throws Exception {

    // Check our initial values
    if ( driver == null ) {

      throw new Exception("No Driver Name Specified!");
    }
    if ( url == null ) {

      throw new Exception("No URL Specified!");
    }
    if ( size < 1 ) {

      throw new Exception("Pool size is less than 1!");
    }

    // Create the Connections
    try {

      // Load the Driver class file
      Class.forName(driver);

      // Create Connections based on the size member
      for ( int x = 0; x < size; x++ ) {

        Connection con = createConnection();

        if ( con != null ) {

          // Create a PooledConnection to encapsulate the
          // real JDBC Connection
          PooledConnection pcon = new PooledConnection(con);
          // Add the Connection to the pool.
          addConnection(pcon);
        }
      }
    }
    catch (Exception e) {

      System.err.println(e.getMessage());
      throw new Exception(e.getMessage());
    }
  }

  // Adds the PooledConnection to the pool
  private void addConnection(PooledConnection value) {

    // If the pool is null, create a new vector
    // with the initial size of "size"
    if ( pool == null ) {

      pool = new Vector(size);
    }
    // Add the PooledConnection Object to the vector
    pool.addElement(value);
  }

  public synchronized void releaseConnection(Connection con) {

    // find the PooledConnection Object
    for ( int x = 0; x < pool.size(); x++ ) {

      PooledConnection pcon =
        (PooledConnection)pool.elementAt(x);
      // Check for correct Connection
      if ( pcon.getConnection() == con ) {

        System.err.println("Releasing Connection " + x);
        // Set its inuse attribute to false, which
        // releases it for use
        pcon.setInUse(false);
        break;
      }
    }
  }

  // Find an available connection
  public synchronized Connection getConnection()
    throws Exception {

    PooledConnection pcon = null;

    // find a connection not in use
    for ( int x = 0; x < pool.size(); x++ ) {

      pcon = (PooledConnection)pool.elementAt(x);

      // Check to see if the Connection is in use
      if ( pcon.inUse() == false ) {

        // Mark it as in use
        pcon.setInUse(true);
        // return the JDBC Connection stored in the
        // PooledConnection object
        return pcon.getConnection();
      }
    }

    // Could not find a free connection,
    // create and add a new one
    try {

        // Create a new JDBC Connection
        Connection con = createConnection();
        // Create a new PooledConnection, passing it the JDBC
        // Connection
        pcon = new PooledConnection(con);
        // Mark the connection as in use
        pcon.setInUse(true);
        // Add the new PooledConnection object to the pool
        pool.addElement(pcon);
    }
    catch (Exception e) {

      System.err.println(e.getMessage());
      throw new Exception(e.getMessage());
    }
    // return the new Connection
    return pcon.getConnection();
  }

  // When shutting down the pool, you need to first empty it.
  public synchronized void emptyPool() {

    // Iterate over the entire pool closing the
    // JDBC Connections.
    for ( int x = 0; x < pool.size(); x++ ) {

      System.err.println("Closing JDBC Connection " + x);

      PooledConnection pcon =
        (PooledConnection)pool.elementAt(x);

      // If the PooledConnection is not in use, close it
      if ( pcon.inUse() == false ) {

        pcon.close();
      }
      else {

        // If it is still in use, sleep for 30 seconds and
        // force close.
        try {

          java.lang.Thread.sleep(30000);
          pcon.close();
        }
        catch (InterruptedException ie) {

          System.err.println(ie.getMessage());
        }
      }
    }
  }
}

Catalog.jsp (I believe the problem is in here :()

Code:
 <html>
 <head>
  <title>
   Catalog
  </title>
 </head>
 <body>
  <%@ page import= "java.io.*" %>
  <%@ page import= "java.util.*" %>
  <%@ page import= "java.sql.*" %>

<!-- Instatiate the ConnectionPool bean with id of "pool" -->
  <jsp:useBean id="pool"
   scope="application"
   class="asa_prototype.ConnectionPool"
  />

  <%
      Connection con = null;

    try{
     if(pool.getDriver() == null){
       //Initialize pool
       pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
       pool.setURL("MuseumDatabase.jds");
       pool.setSize(5);
       pool.setUsername("wjvs");
       pool.setPassword("boink");
       pool.initializePool();
     }

     //Get a connection from the ConnectionPool
     con = pool.getConnection();

     //Create the statement
     Statement statement = con.createStatement();

     //SQL query SELECTs DATA
     //FROM the Artifacts Table
     ResultSet rs = statement.executeQuery("SELECT * " + "FROM ARTIFACTS");
     //Iterate over results

  %>

  <!-- Add a n HTML table to format the results -->
  <center>
   <table border="1" cellspacing="0" cellpadding="2" width="500">
    <tr>
     <th>BLA</th></tr>
  <%

   while (rs.next()){
     //print names
     out.println("<tr>\n<td>" + rs.getString("NAME") + "</td>\n</tr>");
   }

   //Close the Resultset
    rs.close();
    out.println("</table></center>");
   }
   catch (IOException ioe){
     out.println(ioe.getMessage());
   }
   catch (SQLException sqle){
     out.println(sqle.getMessage());
   }
   catch (ClassNotFoundException cnfe){
     out.println(cnfe.getMessage());
   }
   catch (Exception e){
     out.println(e.getMessage());
   }
   finally{
     try{
       if(con != null){
         //release the connection
         pool.releaseConnection(con);
       }
     }
   catch(Exception e){
    out.println(e.getMessage());
   }
  }
  %>

  </table>
 </center>
</body>
</html>
 
Well, there may be more to this little bug but I think the problem is you are using the JDBC driver for a Access database, but you aren't using an access database.

You need
Code:
pool.setDriver ("com.borland.datastore.jdbc.DataStoreDriver");
for a JDatastore. [EDIT: why is vBulletin putting those two spaces in there? ignore them]

If this still doesn't work, I'd say check if you can get database connectivity in a smaller application. Catching a ClassNotFoundException means you don't have the drivers in your classpath, so extra debug lines in that catch block might help narrow it down more.

(I'm more of a J2SE developer so I hope this helps)
 
Thanks for your help with my problem, but I still can´t get it to work. I already tried your advice by using that other driver before, but then it still wouldn´t work. I added some extra println() code to those catch phrases and I found out that either way a normal Exception is thrown, not the CNF Exception.

I'll show you the output I get with both drivers:

sun.jdbc.odbc.JdbcOdbcDriver

No suitable driver Normal Exception thrown!!!

com.borland.datastore.jdbc.DataStoreDriver

com.borland.datastore.jdbc.DataStoreDriver Normal Exception thrown!!!


This final one doesn't surprise me though, because JBuilder usually has a dropdown box showing whenever I start typing a phrase, and when I type "com." a dropdown box with only "sun." shows up, not "borland.". So I believe it's safe to assume that the program simply doesn't know of the existence of that driver. Maybe you know how I can import that some way?

If not I'll try it with an access database or something, but that's a bit annoying since I already put it in SQL code and I couldn't find a way to let Access create tables from SQL input.
 
Well that class (usually encapsulated in a jar) needs to sit on your CLASSPATH. The easiest place to put it is your "classes" directory for that project, or set the CLASSPATH correctly in the JBuilder settings. When you do Class.forName you are dynamically loading that class so the JBuilder auto-complete won't pick it up.

Borland should provide it somewhere, so all I can say is read the documentation. You may already have it but just not know where it is.

Also a quick google led me to this, which appears to be drivers (http://www.minq.se/products/dbvis/drivers.html#jdatastore) if you can't find the ones Borland provides.
 
Thanks for your advice, I looked it up and I included the required library to the project, so now it does show up. At least one error is solved now, but the next exception already shows up.

Now the next problem is in this line of code, which I changed to:

Code:
pool.setURL("jdbc:borland:dslocal:MuseumDatabase.jds");

And this is the message I get when running the project:

MuseumDatabase.jds JDataStore not found or fileName property set to null.

In which folder should I place this database? I have had it in my project main file all the time, but maybe that isn't the right one? I tried placing it in several ones as well. The documentation with JBuilder isn't helping me much with that, and neither is the are either books I have.

By the way, sorry for being such a pain, I feel dumb for not getting something simple like this to work... but that's just the way it is. :D
 
No problem man, not a pain at all.

I don't know where it should be, but I know how you can fully specify the path:
Code:
pool.setURL("jdbc:borland:dsremote://localhost/c:/<local directory>/MuseumDatabase.jds")
 
skip0110
No problem man, not a pain at all.

I don't know where it should be, but I know how you can fully specify the path:
Code:
pool.setURL("jdbc:borland:dsremote://localhost/c:/<local directory>/MuseumDatabase.jds")

Thanks for your help once again, but now it's telling me "No suitable driver" again. This is exactly why I hate JBuilder... impossible to find out what I'm doing wrong.
 
Holy hell! Man, you got me stumped. Might want to take this to a Java-specific developer forum--more people=better chance of getting an answer.
 
skip0110
Holy hell! Man, you got me stumped. Might want to take this to a Java-specific developer forum--more people=better chance of getting an answer.

Yeah, I expected to get this to work, but now I'll first try and see whether it works if I implement an access database. I don't like the idea that my application relies on borland drivers anyway, so let's see whether I'll do a better job with that sun driver.


Thanks for your help, even though I didn't get it to work, I at least now realize that it isn't just me having trouble with this aspect and will have to tackle the problem another way.
 
I GOT IT TO WORK!!!!! :D:D:D:D


/me *does a dance*



Doh.



Because the url of the database gave me an error because of the "\"s in it, I changed them to "/" .... dumb. I expected I did it correct that way, but I forgot that "\" is a special java character to show that for example a " should be included in the text. So of course now it does work if I replace "\" with "\\".


It works with the driver you mentioned. (for some reason I couldn't get my MS Access to convert the database to an odbc database either, couldn't find any drivers to do so)
 
Back