Java 中的 ODBC 连接设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2504961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:35:44  来源:igfitidea点击:

ODBC Connection Setup in Java

javaodbc

提问by Jessica

I want to write a Java program that automates the work that the ODBC Data Source Administrator does in Windows.

我想编写一个 Java 程序来自动执行 ODBC 数据源管理器在 Windows 中所做的工作。

That is, given an ODBC connection name and a path to the database on the hard drive, I want it to create the connection.

也就是说,给定 ODBC 连接名称和硬盘驱动器上数据库的路径,我希望它创建连接。

I really have no idea where to even begin with this. I looked at thisbut it said it was for C and I don't think that's very helpful. If anyone could point me in the right direction for this at all, I would appreciate it.

我真的不知道从哪里开始。我看着这个,但它说它是用于 C 的,我认为这不是很有帮助。如果有人能指出我正确的方向,我将不胜感激。

(I realize this question is REALLY vague, but that's all the information I was given.)

(我意识到这个问题真的很模糊,但这就是我得到的所有信息。)

回答by Jeremy

You will want to look into using JDBC.

您将需要研究使用JDBC

回答by Kevin Crowell

回答by Micha? Niklas

All ODBC configuration is in Windows registry or odbc.iniin Linux (I haven't used ODBC on other platforms). At first you must create such configuration using ODBC manager, then check what was saved in configuration and write program that do the same. If you work with Windows 32 bit, then check registry at HKEY_LOCAL_MACHINE\SOFTWARE\ODBC. Windows 64 bit have different configurations for 32 bit apps and 64 bit apps (just look for odbc.inistring in registry).

所有 ODBC 配置都在 Windows 注册表或odbc.iniLinux 中(我没有在其他平台上使用 ODBC)。首先,您必须使用 ODBC 管理器创建此类配置,然后检查配置中保存的内容并编写执行相同操作的程序。如果您使用的是 Windows 32 位,请检查注册表HKEY_LOCAL_MACHINE\SOFTWARE\ODBC。Windows 64 位对 32 位应用程序和 64 位应用程序有不同的配置(只需odbc.ini在注册表中查找字符串)。

I think Java is not the best language to change something in Windows registry, but with Java you can create .regtext file that can be imported by regedit.exe, or you can use other language like Python with win32 extensions (Active Python has it by default).

我认为Java是不会改变的东西在Windows注册表中最好的语言,但与Java您可以创建.reg可通过导入文本文件regedit.exe,或者您可以使用其他语言如Python的Win32扩展(主动Python的默认有它)。

回答by djangofan

The answer to the question is that you don't need a registered DSN.

问题的答案是您不需要注册 DSN。

Here is an example of using a ODBC connection (not JDBC) from Java, using the system ODBC driver. Instead of editing the registry to create a registered DSN, your alternative is to use a un-registered DSN, like so:

下面是使用系统 ODBC 驱动程序从 Java 使用 ODBC 连接(不是 JDBC)的示例。您可以使用未注册的 DSN,而不是编辑注册表来创建注册的 DSN,如下所示:

Driver=sun.jdbc.odbc.JdbcOdbcDriver
Source=jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Dir/DB/MyDB.mdb;

Or, using SQL Server:

或者,使用 SQL Server:

Driver=sun.jdbc.odbc.JdbcOdbcDriver
Source=jdbc:odbc:Driver={SQL Server};SERVER=127.1;DATABASE=MyDB;UID=sa;PWD=mypass

回答by Subhamay

Check this one out.. Java Database Connectivity (JDBC) supports ODBC-based databases and provides a independent database.

看看这个.. Java 数据库连接 (JDBC) 支持基于 ODBC 的数据库并提供一个独立的数据库。

 Connection connection = null;
     try {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connection = DriverManager.getConnection("connection string", "userName", "password" );
    } catch (SQLException e) {


    }   

     return connection;

回答by asamir

String driver ="sun.jdbc.odbc.JdbcOdbcDriver"
String url = "jdbc:odbc:Driver={Microsoft Access Text Driver (*.txt, *.csv)};DBQ=C:/CSVFolder

query = select * from myfile.csv 

回答by 0x0BADC0DE

I've never had to connect to MS SQL Server before. I've always used DB2 or Derby, MYSQL and everything was always the same for creating a connection. This is what I had to do for SQL Server.

我以前从未连接到 MS SQL Server。我一直使用 DB2 或 Derby、MYSQL,创建连接时一切都是一样的。这就是我必须为 SQL Server 做的事情。

private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

Class.forName(driver);

url="jdbc:odbc:;DRIVER={SQL Server};SERVER="+server+","+port+";DATABASE="+dbName;
connection = DriverManager.getConnection(url,user,password);