Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing MS -Access database using JAVA 1

Status
Not open for further replies.

ringd

Programmer
Jul 11, 2000
35
GB
Hello!

I am thinking of writing a small JAVA application (a simple library booking system). I intend to hold the book data in a local Access database.

Can anyone give me any pointers to anywhere I can get information on doing this? (tutorials on websites covering such topics etc).

I'm not asking for someone else to write this for me, just point this JAVA learner in the right direction so I can find out for myself.

Cheers,
Dave.
mailto:dave.ring@barclays.co.uk
 
start with this FAQ faq269-1460

also see javasoft.com -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
here is another good place to start...

this Tutorial is old but it teaches you the basics about JDBC on MS Access.

JDBC basics

Advanced Tutorial

Advanced Tutorial by IBM

if these links don't help...try going to google and typing in jdbc Tutorial, or jdbc Tutorial for Microsoft access.

hope this helps..
 
All,

Thanks for the advice above.

I still have one query with this (which could be a show stopper!). It regards the ability to code the path name of the .mdb file within the JAVA code, rather than registering the database name through ODBC. By coding the full path of the database, all of my colleagues will be able to use it. If the ODBC route is used, the database name has to be 'configured' on every machine who wishes to use it - not very flexible!

I'm using "connection = DriverManager.getConnection(url);"
where 'url; is a string. I've tried various permutations for 'url'; here's one...
String url = "jdbc:eek:dbc:Microsoft Access Driver (*.mdb);DBQ=H:\\FolderA\\SubFolder\\SubSubFolder\\dbfile.mdb";

Is what I'm asking possible?

Cheers,
Dave.
 
You can dynamically set the DSN up, you were on the right track. Below is a sample of what you want to do..however this option is kind of slow...If you have the option of spending money on this project..you can try using a a third party JDBC connection to Access, rather than the JDBC-ODBC bridge, one opt is Let me know if this doesn't help you...or isn't the response you were looking for...




Code:
import java.sql.*;
import java.io.*;

public class DBSample
{

    public static void main(String args[])
    {

        Connection aConnection; // Create a connection  object.

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            //make sure that this is a space between "MicroSoft Access Driver" and "(*.mdb)"
            //After the "DBQ=C:/testDB.mdb" you have the option to send a user name and password..

          	aConnection = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/testDB.mdb","","");

            Statement stmt = aConnection.createStatement();

            ResultSet 	rs = stmt.executeQuery ("Select * FROM aTable;");
            while (rs.next())
            {
				String 	s1 = rs.getString("Field1");
				String 	s2 = rs.getString("Field2");
				String 	s3 = rs.getString("Field3");
				int 	  s4 = rs.getInt("Field4");


				System.out.println("Value of Field1 is:" + s1);
				System.out.println("Value of Field2 is:" + s2);
				System.out.println("Value of Field3 is:" + s3);
				System.out.println("Value of Field4 is:" + s4);

			}
        }

        catch(SQLException e)
        {

            System.out.println(e);
        }
        catch (ClassNotFoundException c)
        {
            System.out.println(c);
        }
    }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top