//Author: ^-^Veerle^-^ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class databaseConnection //Static class for connecting to a database { private static String strDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //Only can do MS Access at the momment public static Connection getConnection (String strFileName, String strUserName, String strPassWord) throws Exception { Connection databaseConnection; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Asumming it tests if the driver is supported strDriver += strFileName.trim() + ";DriverID=22;READONLY=true}"; //Adds to the driver the database address and extra stuff databaseConnection = DriverManager.getConnection(strDriver, strUserName, strPassWord); //Create the connection } catch (ClassNotFoundException ex) //Thrown if driver is not reconised { throw new Exception("\nError loading database access driver.\nPossible reason is that you do not have MS Access"); } catch (SQLException ex) //Thrown if database can not be found { throw new Exception("\nError connecting to database\n" + System.getProperty("user.dir") + "\\" + strFileName + "\nPlease make sure the database exists"); } return databaseConnection; } }