SQL connection...

Ok, let's say I have an access DB in the same folder as my java program...  What's the connection statement I put to get to the database?

Who is Participating?
conickConnect With a Mentor Commented:
Below is a step-by-step example on how to connect to an Access database.  As you can see you first need to establish a data source and then write the code to connect.
The statement that actually connects your java program to the data source (and in turn.. the database) is:
con = DriverManager.getConnection(url, "", "");

   Before you can communicate between JDBC and Access you need to create a data source.
   You do this by going into the Control Panel and clicking on ODBC icon. (if you dont have an ODBC icon
   you will need to reinstall Access)
   Here are the steps to create the data source once you are in the ODBC icon:
   1) click add
   2) then specify what driver to use... for this case click Access -- then finish
   3) specify the data source name
   4) if you have a mdb file already click select.. otherwise click create
   you should now have a data source for comunication and a database to send/receive info to/from.
   You may want to check the code to make sure there is a Class.forName() line in it
   below is some test code...
   it simply creates a table.. code taken from the Java Tutorial
   datasource is called "coffee"
   import java.sql.*;

   public class CreateCoffees {
       public static void main(String args[]) {
           String url = "jdbc:odbc:coffee";
           Connection con;
           String createString;
           createString = "create table COFFEES " +
                               "(COF_NAME VARCHAR(32), " +
                               "SUP_ID INTEGER, " +
                               "PRICE FLOAT, " +
                               "SALES INTEGER, " +
                               "TOTAL INTEGER)";
           Statement stmt;

           try {
           } catch(java.lang.ClassNotFoundException e) {
               System.err.print("ClassNotFoundException: ");

           try {
               con = DriverManager.getConnection(url, "", "");
               stmt = con.createStatement();

           } catch(SQLException ex) {
               System.err.println("SQLException: " + ex.getMessage());
The above code comes from the Java Tutorial (I'll give credit where credit is due).
The Java Tutorial has a JDBC tutorial.  Its far from complete but it gives you the basics.

Hi Conick,

 I think U are calm in the site for three months. I did n't find any Ur comments except for last 2 weeks since 3 months i entered. Is it???

I was completely swamped for awhile so I couldnt leave my usual wordy comments containing little helpful information.
Thanks for noticing my return. :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.