How to Use Java to Connect to Access 2007
A Java database connection is a driver that connects you from an application to a database. Visual J# has a Java database connection that can connect to your Access database from your computer. To set up a Java database connection, you need to set up a data source name (DSN) in the Northwind Access database. The Northwind is a sample database included with Microsoft Access that you can use to learn database functions.
Instructions
-
Set up a Data Source Name
-
1
Click the "Office" button or "Start" on your computer, then point to "Control Panel." In Windows 7 only, click "System and Security." Point to "Administrative Tools," then click "Data Sources (ODBC)."
-
2
Click the "System DSN" tab and then click "Add."
-
-
3
Click "Driver do Microsoft Access(*.mdb)" and then click "Finish." If you are using a different method to access a database (for example, SQL Server), make sure you select the driver that is compatible with the installed program.
-
4
Type "JDBCdsn" in the "Data Source Name" box, then click "Select."
-
5
Click "Northwins.mdb" and then click "OK."
-
6
Click "With Windows NT authentication using the network login ID" on the Microsoft SQL Server DSN Configuration page that appears, then click "Next."
-
7
Click "OK."
Access the Data
-
8
Click the "Microsoft Office" button in Visual Studio, then click "Project."
-
9
Click "Console Application" and then type "JDBC2" into the "Name" box.
-
10
Delete the code in the window, then cut and paste the following code into the window:
import java.*;
import java.sql.*;
import java.util.*;
import com.ms.jdbc.odbc.JdbcOdbcDriver;
public class Connect{
private java.sql.Connection con = null;
private ResultSet rs;
// Constructor:
public Connect(){}
private void FindEmployee() throws SQLException, ClassNotFoundException
{
try
{
Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:JDBCdsn";
con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
rs = stmt.executeQuery("Select EmployeeID, LastName, Title from Employees");
while (rs.next())
{
//Column names:
System.out.println(rs.getInt("EmployeeID") + " " + rs.getString("LastName") + " " + rs.getString("Title"));
}
if(con != null)
con.close();
con = null;
}
catch (SQLException ex)
{
throw ex;
}
catch (ClassNotFoundException clex)
{
throw clex;
}
}
private void closeConnection(){
try{
if(con != null)
con.close();
con = null;
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
try
{
myDbTest.FindEmployee();
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Error Trace in getConnection() : " + ex.getMessage());
}
}
}
-
11
Press "F5" to run the code. The code will fetch the employee serial number, last name and job title from the Northwind database.
-
1
Tips & Warnings
If the code doesn't work, verify that the data source name in the code matches the one you created in Section 1. The line of the code with the data source name is String url = "jdbc:odbc: JDBCdsn."
References
- Photo Credit Pixland/Pixland/Getty Images