Java connection to MS Access
-
I am running code to connect to a database that stores theme data for a video game. This is the code I am running: import java.sql.*; //import all the JDBC classes import java.sql.*; import java.io.IOException; public class GetAccessToDatabase { static Connection conn; final String URL = "game.mdb"; public boolean OpenConnection() throws SQLException, IOException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.out.println("Could not load the driver."); e.printStackTrace(); return false; } try { conn = DriverManager.getConnection ("jdbc:odbc:" + URL, "", ""); return true; } catch (Exception e) { System.err.println("Problems with get connection sent to "+URL+ ": "+e.getMessage()); } return false; } public void CloseConnection() throws SQLException { conn.close(); } public static void main(String args[]) { try { GetAccessToDatabase connect = new GetAccessToDatabase(); if(connect.OpenConnection()) { Statement stmt = conn.createStatement(); /* stmt.executeUpdate("INSERT INTO student " + "(name, studentNumber, class, major) " + "VALUES ('Smith', '17', 1, 'CS')"); */ // more database statements connect.CloseConnection(); } } catch (Exception e) { System.err.println("problems with SQL sent from "+"static main function"+ ": "+e.getMessage()); } } } And this is the message I receive from the exception thrown: Problems with get connection sent to game.mdb: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified Basically, it doesn't see the game.mdb which I have placed in the local directory. I believe it is expecting some type of table in SQL, but how do I do that in MS Windows from Access? Any and all help would be greatly appreciated. Thank you, Red Sunday ----------------- http://www.zachcalvert.com
-
I am running code to connect to a database that stores theme data for a video game. This is the code I am running: import java.sql.*; //import all the JDBC classes import java.sql.*; import java.io.IOException; public class GetAccessToDatabase { static Connection conn; final String URL = "game.mdb"; public boolean OpenConnection() throws SQLException, IOException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.out.println("Could not load the driver."); e.printStackTrace(); return false; } try { conn = DriverManager.getConnection ("jdbc:odbc:" + URL, "", ""); return true; } catch (Exception e) { System.err.println("Problems with get connection sent to "+URL+ ": "+e.getMessage()); } return false; } public void CloseConnection() throws SQLException { conn.close(); } public static void main(String args[]) { try { GetAccessToDatabase connect = new GetAccessToDatabase(); if(connect.OpenConnection()) { Statement stmt = conn.createStatement(); /* stmt.executeUpdate("INSERT INTO student " + "(name, studentNumber, class, major) " + "VALUES ('Smith', '17', 1, 'CS')"); */ // more database statements connect.CloseConnection(); } } catch (Exception e) { System.err.println("problems with SQL sent from "+"static main function"+ ": "+e.getMessage()); } } } And this is the message I receive from the exception thrown: Problems with get connection sent to game.mdb: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified Basically, it doesn't see the game.mdb which I have placed in the local directory. I believe it is expecting some type of table in SQL, but how do I do that in MS Windows from Access? Any and all help would be greatly appreciated. Thank you, Red Sunday ----------------- http://www.zachcalvert.com
You need to register an ODBC datasource and pass the name of that to the getConnection() method rather than the name of the access file.
Store your favourite bookmarks online: my-faves.co.uk