Solved

Connect to MS Access in Java

Posted on 2006-07-17
12
418 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
Comment Utility
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
Comment Utility
0
 
LVL 12

Expert Comment

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

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 125 total points
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Expert Comment

by:riaancornelius
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It's allright, i should have read the post before more carefully. No harm done here.
0
 
LVL 9

Expert Comment

by:riaancornelius
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 68
What is the latest versions eclipse neon 2 116
create a gui in perl 3 42
Java Server Faces parameter pass? 6 18
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

744 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