Solved

Connect to MS Access in Java

Posted on 2006-07-17
12
421 Views
Last Modified: 2012-06-22
What are the other ways of connecting Java and MS Access except ODBC
0
Comment
Question by:sree032397
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 125 total points
ID: 17120714
You can connect by using a type 4 driver also, like you would do to any other database. The only thing you have to do is to find such a driver. Have a look here: http://developers.sun.com/product/jdbc/drivers
0
 
LVL 9

Assisted Solution

by:riaancornelius
riaancornelius earned 125 total points
ID: 17120719
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17121414
Use Hibernate (or other persistence framework). You can also use Entity EJBs.
0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 125 total points
ID: 17121679
How to communicate with a MS Access database from a Java program.

First Register the database with the Windows Naming Service

1.    Click Start|Settings|Control Panel You will see an icon which sets up an ODBC data source within the control panel. This is a simple naming system which allows your Java programs to communicate with a Microsoft database. The name of the icon will be different on each Windows version, for example it will be marked ODBC Data Sources on NT4 and be found in a folder named Administrative tools and called Data Sources (ODBC) in Windows 2000.
2.    Double click this icon.
3.    Click the add button on the window that appears.
4.    On the next window double click the line starting Microsoft Access Driver.... A new window should then appear.
5.    In the field marked Data Source Name enter a ‘nickname’ for your database i.e."dbNickName", then click the Select button.
6. Navigate to the file containing the actual database; you will know that you have reached it when the name of the file (yourDatabase.mdb) appears in the leftmost text area.
7. Select the file by clicking it and then click OK.
8. Get rid off all the windows that you have opened by clicking OK on all of them.
The database file can now be referred to as “dbNickName” within your Java programs.

How to establish a connection to the database  (import java.sql.*;)

1. Load a compatible driver. (Inside a Try – Catch block)
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

2. Create a connection object. (Try – Catch ) Supplying a protocol & DB name, userName, and password.

Connection connect;
connect = DriverManager.getConnection(“jdbc:odbc: dbNickName”, “userName”, “password”);

3. Create your SQL statement

String sqlString = “select * from aTable”;

4.    Create a Statement object and associate it with the Connection object.

Statement stat = connect.createStatement();

5. Execute the SQL statement and store the result in a ResultSet object.

ResultSet res = stat.executeQuery(sqlString);

6.    Extract the data record(s) from the ResultSet object using code inside a while(res.next()) loop.  If your table contains three columns each holding a String, String and Integer respectively then the data can be retrieved by using res.getString(1), res.getString(2) and res.getInt(3).  Where the arguments represent the position of each column in the database table (the actual table names can also be used if preferred).
        You will probably want to store the retreived data as objects in a vector and create an Enumeration object from this vector in order to manipulate the data.

7.    Close the Statement, Connection and ResultSet objects
    stat.close();
    connect.close();
    res.close();


More links

http://www.dbmdata.com/dev/connectToAccess.html

http://forum.java.sun.com/thread.jspa?threadID=200603&messageID=669121

0
 
LVL 6

Assisted Solution

by:valipotor
valipotor earned 125 total points
ID: 17122725
For a comprehensive list of MS ACCESS drivers check the  Sun's JDBC driver database:
http://industry.java.sun.com/products/jdbc/drivers .

Select MS ACCESS in the Supported DBMS(s) and click search.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Expert Comment

by:riaancornelius
ID: 17127863
kaliyugkaarjun, look at the question:
>> What are the other ways of connecting Java and MS Access **except ODBC**

valipotor:
girionis gave that link in his first post...
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17127923
riaancornelius:

Yes, I can see that the URL's are different but the page is the same. Honest mistake.

sree:

Here is another link, hopefully no one posted before :

http://jackcess.sourceforge.net/faq.html

Jackcess ia a pure Java library for reading from and writing to MS Access databases. It is not an application. There is no GUI. It's a library, intended for other developers to use to build Java applications.

Also, you can check thins library too:

http://mdbtools.sourceforge.net/

The MDB Tools project is a effort to document the MDB file format used in Microsoft's Access database package, and to provide a set of tools and applications to make that data available on other platforms.

But if the reason for not using ODBC is that you are interested in using an MS Access database in linux, you must know that ODBC drivers for linux are available too.
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17127949
Sorry valipotor, I didn't see that the URLS were different, I read them as the same thing :)
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17127958
It's allright, i should have read the post before more carefully. No harm done here.
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17362569
kaliyugkaarjun doesn't really answer the question asked, but I guess it is a good answer, so unless sree wants to gives us some more info, I'm happy with your suggestion Girionis:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 77
Java and GPO 11 70
Java SE 8u111  Lot of stuff broke 11 54
even odd program using while loop 3 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now