如何在Windows XP中使用Java远程连接ODBC? [英] How can i remotely connect ODBC using Java in Windows XP?

查看:181
本文介绍了如何在Windows XP中使用Java远程连接ODBC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的远程数据库是Mysql数据库,我想要检索一些记录并放入
Microsoft Access数据库,这是一个localhost数据库

My remote database is Mysql database and i want to retreive some records and put into the Microsoft Access database which is a localhost database

我想要为远程数据库创建DSN

and i want to also create DSN for remote database

请帮助我

推荐答案

我刚刚在MATLAB中使用类似的设置,使用java连接到MySQL和Access数据库。
我使用以下方法创建了一个java类

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");}
 }

这是在内部网络上运行的,因此根据定义databaseUrl的注释,glnd2818898.network.net是MySQL服务器,它连接到数据库'matlab'

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'

Access接口类似

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());}
}

这可能不是最好的java编码,因为这是我第一次从MATLAB用户的角度尝试,但它对我有用。

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.

这篇关于如何在Windows XP中使用Java远程连接ODBC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆