bazbazbaz
asked on
Connect to MS Access database from java code
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.od bc.JdbcOdb cDriver");
String dbLocation = "F:\\DCom4\\4ThYrProject";
String PWD = null;
String userName = null;
connection = DriverManager.getConnectio n("jdbc:od bc: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.cre ateSQLExce ption(Jdbc Odbc.java: 6879)
at sun.jdbc.odbc.JdbcOdbc.sta ndardError (JdbcOdbc. java:7036)
at sun.jdbc.odbc.JdbcOdbc.SQL DriverConn ect(JdbcOd bc.java:30 28)
at sun.jdbc.odbc.JdbcOdbcConn ection.ini tialize(Jd bcOdbcConn ection.jav a:323)
at sun.jdbc.odbc.JdbcOdbcDriv er.connect (JdbcOdbcD river.java :174)
at java.sql.DriverManager.get Connection (DriverMan ager.java: 512)
at java.sql.DriverManager.get Connection (DriverMan ager.java: 171)
at dbconversion.accessTosql.m ain(access Tosql.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.
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.od
String dbLocation = "F:\\DCom4\\4ThYrProject";
String PWD = null;
String userName = null;
connection = DriverManager.getConnectio
}
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.cre
at sun.jdbc.odbc.JdbcOdbc.sta
at sun.jdbc.odbc.JdbcOdbc.SQL
at sun.jdbc.odbc.JdbcOdbcConn
at sun.jdbc.odbc.JdbcOdbcDriv
at java.sql.DriverManager.get
at java.sql.DriverManager.get
at dbconversion.accessTosql.m
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.JdbcOdbcDri ver" );
connect = DriverManager.getConnectio n( url );
JOptionPane.showMessageDia log(null," Connection Successful !",
"Database . . .",JOptionPane.PLAIN_MESSA GE);
}
catch ( ClassNotFoundException cnfex ) {
cnfex.printStackTrace();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
catch ( Exception ex ) {
ex.printStackTrace();
}
}
}
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.JdbcOdbcDri
connect = DriverManager.getConnectio
JOptionPane.showMessageDia
"Database . . .",JOptionPane.PLAIN_MESSA
}
catch ( ClassNotFoundException cnfex ) {
cnfex.printStackTrace();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
catch ( Exception ex ) {
ex.printStackTrace();
}
}
}
Here is the answer for your second question :
MySQL® Connector/ODBC:
http://www.mysql.com/products/myodbc/index.html
Hope this helps . . .
JAVATM
MySQL® Connector/ODBC:
http://www.mysql.com/products/myodbc/index.html
Hope this helps . . .
JAVATM
If the codes above did not work it means that you really need
the odbc connector. :)
the odbc connector. :)
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.getConnectio n("jdbc:od bc:Driver= {Microsoft Access Driver (*.mdb)};DBQ="+"dbLocation ","usernam e","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.
Try changing your code to this:
connection = DriverManager.getConnectio
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.
ASKER
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.
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.
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
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
Thanks :)
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.JdbcOdbcDri
connect = DriverManager.getConnectio
JOptionPane.showMessageDia
"Database . . .",JOptionPane.PLAIN_MESSA
}
catch ( ClassNotFoundException cnfex ) {
cnfex.printStackTrace();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
catch ( Exception ex ) {
ex.printStackTrace();
}
}
}