Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / March 2005

Tip: Looking for answers? Try searching our database.

MS Access setup with javax.sql and DataSource objects

Thread view: 
leon - 25 Mar 2005 16:21 GMT
Hi,

I want to access/connect to MS Access database NOT via DriverManager
(the old way) but with using DataSource objects which I understand needs
system setup with JNDI...

what (if possible) does on need to do to setup for MS Access database
connectivity with DataSource objects?

(I MUST use Access - uni assignment restrictions - it sucks, but uni
seems to be VERY M$ supportive)...

Kind regards,
Leon.
Bjorn Abelli - 25 Mar 2005 22:57 GMT
"leon" wrote...

> I want to access/connect to MS Access database NOT via DriverManager
> (the old way) but with using DataSource objects which I understand needs
> system setup with JNDI...

I'm not sure what you mean by "DataSource" in this case, as it could happen
that you've mixed up two different concepts...

As the documentation for javax.sql.DataSource says, DataSources is
"typically" used through JNDI, but it is not necessary to use JNDI in order
to use DataSources.

The use of DataSources is most efficient though, when it can be used through
JNDI as "DataSource-factories", e.g. from a J2EE/EJB-container, where JNDI
is "built-in".

> what (if possible) does on need to do to setup for
> MS Access database connectivity with DataSource objects?

Well...

If you really "need" to use a DataSource through JNDI, then you have to put
up a JNDI-compliant nameserver with the possibility to manage DataSources
somewhere...

Otherwise you're really better off using the "old" way with DriverManager,
if you're using MS Access...

In what environment are you actually going to use this database?

If it is from such a container, then you have to figure out how to configure
the container to use the ordinary jdbc/odbc-bridge for MS Access, which can
be tricky, and the configuration can also differ between different
containers.

Some of them also have difficulties to configure a datasource based on the
jdbc/odbc-bridge at all...

And you'll also have a hard time to try to find some other jdbc-drivers for
MS Access that support the use of DataSources...

If you just want the "feeling" of using a DataSource, then you can roll your
own... ;-)

An example of a DataSource for MS Access I had laying around... ;-)

======================================================

package abelli;

import java.sql.SQLException;
import java.sql.Connection;
import java.io.PrintWriter;
import java.io.File;
import java.sql.DriverManager;

/**
 * AccessDataSource is a basic implementation of a DataSource
 * for a Microsoft JET/Access database, to simplify the
 * handling of such a database as a DataSource.
 *
 * NB!!! This is nothing else than a wrapper around an ordinary
 * jdbc/odbc-bridge, so don't expect any improvement in performance...
 * <p>
 *
 * @AccessDataSource.java    1.02 2002-11-01
 * @author Bj?rn Abelli
 * @see javax.sql.DataSource
 * @see java.sql.DriverManager
 * @since JDK1.4
 */

public class AccessDataSource implements javax.sql.DataSource
{
 private String url = null;

/**
*
* AccessDataSource(String path) registers the standard JdbcOdbcDriver
* with the DriverManager, and sets an internal pathvalue to the mdb-file.
*
* @AccessDataSource
*
* @param path
*
* <b>path</b> is the physical address to the mdb-file with the database.
*
*/

 public AccessDataSource (String path) throws SQLException
 {
   DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
   url = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + path;

   File file = new File(path);
   if (!file.exists())
      throw new SQLException("\nFile \"" + path + "\" not found");
 }

/**
*
* getConnection() returns a Connection based on the path
* passed at construction of this DataSource.<br>
*
* @getConnection
*
* @see java.sql.Connection
*/

 public Connection getConnection() throws SQLException
 {
   return DriverManager.getConnection (url);
 }

/**
*
* getConnection() returns a Connection based on the path
* passed at construction of this DataSource.<br>
*
* @getConnection
*
* @param username
* @param password
*
* <b>username</b> and <b>password</b> are passed to the database
* in the attempt to establish a Connection. If the database is not
* password-protected, the arguments are ignored.<br>
*
* @see java.sql.Connection
*/

 public Connection getConnection(String username, String password)
 throws SQLException
 {
   return DriverManager.getConnection (url, username, password);
 }

/**
*
* getLogWriter() returns the PrintWriter-object
* connected to the DriverManager's LogWriter.<br>
*
* @getLogWriter
*
* @see java.sql.DriverManager
* @see java.io.PrintWriter
*/

 public PrintWriter getLogWriter() throws SQLException
 {
   return DriverManager.getLogWriter();
 }

/**
*
* setLogWriter(PrintWriter out) sets the PrintWriter-object
* connected to the DriverManager's LogWriter.<br>
*
* @setLogWriter
*
* @param out
*
* @see java.sql.DriverManager
* @see java.io.PrintWriter
*/

 public void setLogWriter(PrintWriter out) throws SQLException
 {
   DriverManager.setLogWriter(out);
 }

/**
*
* setLoginTimeout() is ignored. It is not possible at
* this time to set any timeout<br>
*
* @setLoginTimeout
*
*/

 public void setLoginTimeout(int seconds) throws SQLException
 {
 }

/**
*
* getLoginTimeout() returns the default value of 0<br>
*
* @getLoginTimeout
*
*/

 public int getLoginTimeout()
 {
   return 0;
 }
}

=========================================

So, now you can just instantiate it like this:

import abelli.*;
...

DataSource mdb = new AccessDataSource ("path\\foobar.mdb");

Connection conn = mdb.getConnection("", "");

...

Good luck...

// Bjorn A
leon - 26 Mar 2005 05:24 GMT
> Otherwise you're really better off using the "old" way with DriverManager,
> if you're using MS Access...
>
> In what environment are you actually going to use this database?

well - i would like to use distributed transation concept (which in java
tutorials was alluded to work with DataSource objects) to acccess
multiple MS Access databases (those databases would probably be accessed
via jdbc-odbc link - uni assignment)...

I don't mind using DriverManager for as long as i can use the
distributed transations feature ( i was under impression that one needed
 javax.sql to do that... i guess i was wrong?)  if so - any code
samples on using distributed transaction with DriverManager?

java tut describes those as:
"..A DataSource class that supports distributed transactions—produces
Connection objects that can be used in a distributed transaction, that
is, a transaction that accesses two or more DBMS servers..."

and the code to do the above with DataSource is as follows (from the
same tut) i've hacked for simpler readibility...:

      Context ctx = new InitialContext();
      DataSource ds = (DataSource)ctx.lookup("jdbc/distCoffeesDB");
      Connection con;
      PreparedStatement pstmt;
      con = ds.getConnection("webLogin", "webPassword");

      pstmt = con.prepareStatement("UPDATE COFFEES " +
         "SET TOTAL = TOTAL + ? WHERE COF_NAME = ?");

      pstmt.setInt(1, 5);
      pstmt.setString(2, "blah blah"
      pstmt.executeUpdate();

      stmt.close();

      con.close();
Lee Fesperman - 26 Mar 2005 10:26 GMT
> > Otherwise you're really better off using the "old" way with DriverManager,
> > if you're using MS Access...
[quoted text clipped - 15 lines]
> Connection objects that can be used in a distributed transaction, that
> is, a transaction that accesses two or more DBMS servers..."

You can't do that. Distributed transactions (XA/JTA) requires that the database backend
have builtin support for them. I don't believe MS Access does. In addition, ODBC does
not provide that capability. There is no way to build a wrapper that emulates
distributed transactions for a backend that requires ODBC or doesn't support XA.

For plain datasources (without distributed transactions), most container software, such
as application servers, provide wrappers that will turn Driver Manager style drivers
into datasources. There also may be freeware wrappers for containers that don't.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.