Link to home
Start Free TrialLog in
Avatar of pshortland
pshortlandFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Full code to connect from Java to Access on same machine

Going mad here !

Can anyone post some exact and complete code that connects from Java to an Access database ?
I have a db called mydb.mdb and an ODBC setup as mydsn.
Then a table called mytable with a column called mycolumn.

All i need is the simplest code possible that merely shows one result from that column.

Anyone help ?
Avatar of Mayank S
Mayank S
Flag of India image

Sorry we can't write full working code unless we see some code from you - its against site rules. But you will find full sample code here, as a matter of fact:

http://www.exampledepot.com/egs/java.sql/GetRsData.html
That's assuming you know how to connect. If you don't, then look at the code here:

http://www.exampledepot.com/egs/java.sql/ConnectOracle.html

Change the URL to "jdbc:odbc:mydsn" and the driver to "sun.jdbc.odbc.JdbcOdbcDriver" and you should be good to go....
There is a sample right at the start of the JDBC tutorial as well:

http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html
Avatar of pshortland

ASKER

My code is below but will not work, says it cant find the main class...

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;

public class DatabaseConnection
{
     public static void main(String [] arguments)
     {
          Connection connection = null;
          Statement statement = null;
          ResultSet rs = null;

          try
          {
               // Load the database drievrs. An ODBC: JDBC bridge
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               System.out.println("drivers loaded");

               // Obtain connection to the database.
               connection = DriverManager.getConnection("jdbc:odbc:mydb", "", "");
               System.out.println("Connection established");

               // Cerate statement to read and write to/from database.
               statement = connection.createStatement();
               System.out.println("Statement created");

               rs = statement.executeQuery("SELECT * FROM mytable");

               // Get next row.
               rs.next();
               System.out.println("data is: " + rs.getString("ename"));

               // Close statement and conenction.
               statement.close();
               connection.close();
          }
          catch (ClassNotFoundException cnf)
          {
               System.out.println("Could not find class: " + cnf);
          }
          catch (SQLException sqle)
          {
               System.out.println("SQL problem: " + sqle);
          }
     }
}
The code looks correct. To make it better, you can move the closing of the statement and connection to a finally block after the try blocks:

finally
{
  try
  {
    if ( connection != null )
      connection.close () ;
  }
  catch ( Exception e )
  {
  }
}

If the class is not found, you need to set the classpath. Try:

java -cp . DatabaseConnection
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the ODBC name of the database is mydsn, then that is the name you should use in your JDBC-ODBC connection:

               connection = DriverManager.getConnection("jdbc:odbc:mydsn", "", "");

It will have to be defined as a System DSN to be accessed by your program (not User DSN).
>> "jdbc:odbc:mydsn"

Already mentioned :)

>> It will have to be defined as a System DSN to be accessed by your program (not User DSN)

I think it works with User DSN too - at least for J2SE applications running under your logged-in account....