How can i remotely connect ODBC using Java in Windows XP?

穿精又带淫゛_ 提交于 2019-12-01 14:39:50

I've just got a similar set up working in MATLAB which uses java to connect to MySQL and Access databases. I created a java class with the following method

/**
 * Open a connection to a MySQL database
 * @param userName      registered user on the MySQL database.
 * @param userPassword  MySQL database password for the named user.
 * @param databaseUrl   database name eg. 'jdbc:mysql://glnd2818898.network.net/matlab'
 */
 public void openMySQLConnection(String userName, String userPassword, String databaseUrl){
    try {
        Class.forName ("com.mysql.jdbc.Driver").newInstance ();
        conn = DriverManager.getConnection (databaseUrl, userName, userPassword);

    }catch (SQLException e) {System.err.println ("Cannot connect to database server");}
 }

This runs over an internal network, so as per the comments defining the databaseUrl glnd2818898.network.net is the MySQL server and it connects to the database 'matlab'

The Access interface is similar

    private static final String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
    private static final String accessDBURLSuffix = ";READONLY=true}";

    /**
     * Open a connection to a Access database
     * @param userName      registered user on the Access database.
     * @param userPassword  Access database password for the named user.
     * @param databaseUrl   database name eg. 'pathname/accessName.mdb'
     */
public void openConnAccess(String userName, String userPassword, String databaseUrl){
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    String dbUrl = accessDBURLPrefix + databaseUrl + accessDBURLSuffix;
        conn = DriverManager.getConnection (dbUrl, userName, userPassword);
    }catch (SQLException e) {System.err.println ("Cannot connect to database server  :" + e.getMessage());}
}

It's probably not the neatest java coding as it was my first attempt from a MATLAB users point of view, but it works for me.

You want to use the JDBC ODBC Bridge to connect to your ODBC database.

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