Connecting with MS Access Data source from java code

﹥>﹥吖頭↗ 提交于 2019-12-24 22:29:44

问题


I have created a .mdb file using MS Access. I have created a User DSN in windows. now i want to connect to this data source using java code ? how can i do that ?


回答1:


Emm... As I can remember, you have to create DataSource (see image)

... then use jdbc to access it; Here is a good example of how to do this;

EDIT : In case of remote datasource request you may use this instructions which describe how to create the bridge;

put attention at this snippets :

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver) ;

       // setup the properties 
       java.util.Properties prop = new java.util.Properties();
       prop.put("charSet", "Big5");
       prop.put("user", username);
       prop.put("password", password);

       // Connect to the database
       con = DriverManager.getConnection(url, prop);

and this one :

...
    sun.jdbc.odbc.ee.DataSource ds = new sun.jdbc.odbc.ee.DataSource();

                // Provide user credentials and database name

                ds.setUser("scott");
                ds.setPassword("tiger");
                ds.setDatabaseName("dsn1");
                ds.setCharSet("..."); // optional property
                ds.setLoginTimeout(100); // optional property

                // Establish initial context and bind to the datasource target

                InitialContext ic = new InitialContext();
                ic.bind("jdbc/OdbcDB1",ds);
...

...which is showing of how to set datasource name in case of any url


Please do comment if you have more details

Good luck :)



来源:https://stackoverflow.com/questions/28939128/connecting-with-ms-access-data-source-from-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!