How to connect java to Ms access Database -
this question has answer here:
i'm trying connect java ms access database didn't work well
and got error message this
sun.jdbc.odbc.jdbcodbcdriver
this code :
import java.sql.*; public class main{ public static void main(string[] args) { try{ class.forname("sun.jdbc.odbc.jdbcodbcdriver"); connection con = drivermanager.getconnection("jdbc:odbc:driver={microsoft access driver(*.accdb)};dbq=d:\\andries\\testdatabase.accdb"); statement st = con.createstatement(); }catch(exception ex){ system.out.println(ex.getmessage()); } } }
you can use ucanacess.jar connect ms aceess database
show example here http://www.benchresources.net/jdbc-msaccess-database-connection-steps-in-java-8/
import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class msaccessdatabaseconnectioninjava8 { public static void main(string[] args) { // variables connection connection = null; statement statement = null; resultset resultset = null; // step 1: loading or registering oracle jdbc driver class try { class.forname("net.ucanaccess.jdbc.ucanaccessdriver"); } catch(classnotfoundexception cnfex) { system.out.println("problem in loading or " + "registering ms access jdbc driver"); cnfex.printstacktrace(); } // step 2: opening database connection try { string msaccdb = "d:/workspace/test_workspace/java-jdbc/player.accdb"; string dburl = "jdbc:ucanaccess://" + msaccdb; // step 2.a: create , connection using drivermanager class connection = drivermanager.getconnection(dburl); // step 2.b: creating jdbc statement statement = connection.createstatement(); // step 2.c: executing sql & retrieve data resultset resultset = statement.executequery("select * player"); system.out.println("id\tname\t\t\tage\tmatches"); system.out.println("==\t================\t===\t======="); // processing returned data , printing console while(resultset.next()) { system.out.println(resultset.getint(1) + "\t" + resultset.getstring(2) + "\t" + resultset.getstring(3) + "\t" + resultset.getstring(4)); } } catch(sqlexception sqlex){ sqlex.printstacktrace(); } { // step 3: closing database connection try { if(null != connection) { // cleanup resources, once after processing resultset.close(); statement.close(); // , close connection connection.close(); } } catch (sqlexception sqlex) { sqlex.printstacktrace(); } } } }
Comments
Post a Comment