Java and MS Access

This is source code to connect java with ms access

//add imports

public class DatabaseAccess {
Connect to the database - you could place this method in a different class to be reused by all of your database access classes if you like
private Connection getConnection() throws Exception {
//driver: special utility which allows program to talk to database package - ODBC is used to talk to MS ACCESS
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
//Databases will usually have a url made up of the driver as well as the database location - access db must be saved in the old .mdb format for this driver
String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:
String username = "";
String password = "";
return DriverManager.getConnection(url, username, password);
public User getUser(int userID){
User userFromDB = null;
//get the connection using the above method
Connection conn = getConnection();
//Create a sql statement on the connection;
Statement st = conn.createStatement();
//For a select statement use st.eexcuteQuery method - it returns the results in a ResultSet object - for INSERT and UPDATE statements use st.execute() method
ResultSet rs = st.executeQuery("Select * FROM USER WHERE USERID=" + userID);

//loops through results until end - in this case we know there will only be one result since we are searching by primary key
userFromDB = new User();
//user rs.get.. methods to load the value of each db column from the current row
}catch(Exception ex){
return userFromDB;