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:
DocumentsJavaloginTest//dbTest.mdb";
String username = "";
String password = "";
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public User getUser(int userID){
User userFromDB = null;
try{
//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
while(rs.next()){
userFromDB = new User();
//user rs.get.. methods to load the value of each db column from the current row
userFromDB.setName(rs.getString("FULLNAME"));
userFromDB.setUsername(rs.getString("USERNAME"));
}
}catch(Exception ex){
}
return userFromDB;
}
}