Solved

Connect to MS Access database from java code

Posted on 2003-10-21
10
1,460 Views
Last Modified: 2010-03-31
Hi all,

Here is my code:

import java.sql.*;
import java.io.*;
import javax.swing.*;
import javax.sql.*; etc...

public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

      String dbLocation = "F:\\DCom4\\4ThYrProject";
      String PWD = null;
      String userName = null;

      connection = DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ="+dbLocation+"","+PWD+","+userName+");

}

That's the basics of it anyway. When I open the database I don't need a username and password so I left them null above.

This code ran when I ran it from a java console using javac etc, but now I'm using JBuilder 9.  There is an error on the connection = DriverManager.... line.

These are the errors generated:
               at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6879)

      at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7036)

      at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3028)

      at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)

      at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)

      at java.sql.DriverManager.getConnection(DriverManager.java:512)

      at java.sql.DriverManager.getConnection(DriverManager.java:171)

      at dbconversion.accessTosql.main(accessTosql.java:85)

What am I doing wrong? Also what code do I need to connect to mySQL? Do I have to install connectorJ, or can I use the ODBC bridge?

Thanks in advance.


0
Comment
Question by:bazbazbaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 125 total points
ID: 9593955
Try this from my sample :

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Database {

public static void main(String args[]) {

  try {

  // change the Yourdatabase with your database
  url = "jdbc:odbc:Yourdatabase";

  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  connect = DriverManager.getConnection( url );
  JOptionPane.showMessageDialog(null,"Connection Successful !",
  "Database . . .",JOptionPane.PLAIN_MESSAGE);    
  }
  catch ( ClassNotFoundException cnfex ) {
  cnfex.printStackTrace();
  }
  catch ( SQLException sqlex ) {
  sqlex.printStackTrace();
  }
  catch ( Exception ex ) {
  ex.printStackTrace();
  }

}
}

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9594005
Sorry forgot to initialize connect;
It should be :

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Database {

public static void main(String args[]) {

  Connection connect;

  try {

  // change the Yourdatabase with your database
  url = "jdbc:odbc:Yourdatabase";

  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  connect = DriverManager.getConnection( url );
  JOptionPane.showMessageDialog(null,"Connection Successful !",
  "Database . . .",JOptionPane.PLAIN_MESSAGE);    
  }
  catch ( ClassNotFoundException cnfex ) {
  cnfex.printStackTrace();
  }
  catch ( SQLException sqlex ) {
  sqlex.printStackTrace();
  }
  catch ( Exception ex ) {
  ex.printStackTrace();
  }

}
}
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9594045
Sorry I'm on my work now, forgot to initialize url.
Try this code with no errors:

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Database {

public static void main(String args[]) {

  Connection connect;
  String url;

  try {

  // change the Yourdatabase with your database
  url = "jdbc:odbc:Yourdatabase";

  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  connect = DriverManager.getConnection( url );
  JOptionPane.showMessageDialog(null,"Connection Successful !",
  "Database . . .",JOptionPane.PLAIN_MESSAGE);    
  }
  catch ( ClassNotFoundException cnfex ) {
  cnfex.printStackTrace();
  }
  catch ( SQLException sqlex ) {
  sqlex.printStackTrace();
  }
  catch ( Exception ex ) {
  ex.printStackTrace();
  }

}
}
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 15

Expert Comment

by:Javatm
ID: 9594122
Here is the answer for your second question :

MySQL® Connector/ODBC:
http://www.mysql.com/products/myodbc/index.html

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9594156
If the codes above did not work it means that you really need
the odbc connector. :)
0
 
LVL 1

Expert Comment

by:sre23
ID: 9595127
I think what bazbazbaz is trying to do is create the odbc connection on the fly which works well so you don't have to set up odbc connections ahead of time.

Try changing your code to this:

connection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+"dbLocation","username","PWD");


Your main problem I think is that you were just missing a few brackets and also make sure you add ".mdb" to the end of your dblocation string.

you might also try setting username and PWD = "" instead of null.

If you still have problems post your code again so I can see your changes.
0
 

Author Comment

by:bazbazbaz
ID: 9598231
Hi again,

Just after posting the question I realised that I had forgotten the actual database name in the path, clever me. It's working now. I'll try your code for the mySQL connection and get back to you.

Thanks very much.
0
 
LVL 92

Expert Comment

by:objects
ID: 10181906
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Javatm's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

objects
EE Cleanup Volunteer
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10198436
Thanks :)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

623 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