Currently I have one application in which I am able to access .mdb or .accdb file with JdbcOdbcDriver to append some data.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
but in this, I need to configure System DSN. We need to add new Data Source (Microsoft Access Driver) and then need to provide location of .mdb file. Only then above code will work.
Suppose I want to run my application on other system then I need to do same thing to that computer. If I give my application to the client and he/she don't know how to configure .mdb file. Then my whole effort will waste. So any driver is available by which I create .mdb file by my Java Code and then append all the data into the table of .mdb file. Or is there any other way, where Java code can create .mdb file and able to access this database file.
I tried this code which append data without configuring System DNS:
public class TestMsAccess {
private static Connection con;
private static Statement stm;
private static String tableName = "EmpDetail";
private static int id_is = 2;
private static String name_is = "Employee1";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\MSAccessProject/Employee.mdb", "", "");
stm = con.createStatement();
// enter value into table
String addRow = "INSERT INTO " + tableName + " VALUES ( "
+ id_is + ", '"
+ name_is + "')";
stm.execute(addRow);
if (con != null) { con.close(); }
if (stm != null) { stm.close(); }
}
}
But the problem is, this code not create .mdb file automatically but work when I create .mbd file and table before running this code.