java 使用 JDBC-ODBC 桥连接到 SQLServer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/295603/
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-10-29 11:47:57  来源:igfitidea点击:

Connecting to SQLServer using JDBC-ODBC Bridge

javajdbcodbc-bridge

提问by Omar Kooheji

I'm writing an applicationt hat was prototyped on MySQL and is now connecting to an Oracle database.

我正在编写一个应用程序,它是在 MySQL 上原型化的,现在正在连接到 Oracle 数据库。

All I had to do to connect to the oracle database (having built up the table structure) was change the connection string.

为了连接到 oracle 数据库(已经建立了表结构),我所要做的就是更改连接字符串。

What is the format to connect to a SQL Server DB on another machine?

连接到另一台机器上的 SQL Server DB 的格式是什么?

I've read some tutorials which tell you to use the SQL Server JDBC adaptor but I'd rather configure the application so that it's database agnostic, and just have the connection string specify the protocol etc.

我已经阅读了一些教程,这些教程告诉您使用 SQL Server JDBC 适配器,但我宁愿配置应用程序,使其与数据库无关,并且只需让连接字符串指定协议等。

Any references I've seen which tell you how to use the bridge with SQL Server require the ODBC Data Source to be installed, this is less than ideal as my app may run on Linux or windows.

我见过的任何参考资料都告诉您如何将网桥与 SQL Server 一起使用,都需要安装 ODBC 数据源,这不太理想,因为我的应用程序可能会在 Linux 或 Windows 上运行。

I'm not doing anything complicated just inserts.

我没有做任何复杂的事情只是插入。

回答by Vincent Ramdhanie

You should not use the JDBC-ODBC bridge in a production environment. It is much slower than other JDBC drivers and only necessary when a JDBC driver is not available.

您不应在生产环境中使用 JDBC-ODBC 桥。它比其他 JDBC 驱动程序慢得多,并且仅在 JDBC 驱动程序不可用时才需要。

SQL Server has a JDBC driveravailable from Microsoft. If you use it then you will get the required result.

SQL Server 具有Microsoft 提供的JDBC 驱动程序。如果您使用它,那么您将获得所需的结果。

With the ODBC bridge you have no choice but to install the ODBC driver.

使用 ODBC 桥,您别无选择,只能安装 ODBC 驱动程序。

This articledescribes the connection string you will need to use to connect to the SQL Server.

文章介绍您将需要使用连接到SQL Server的连接字符串。

回答by Joey Gibson

Do NOT use the JDBC-ODBC bridge driver. That was meant purely for testing, not for production. You can still make your application database agnostic using drivers that are optimized for the database you want to connect to. Just externalize the username, password database driver name and connect string, and don't use any DB-specific SQL and you should be fine.

不要使用 JDBC-ODBC 桥驱动程序。这纯粹是为了测试,而不是为了生产。您仍然可以使用为要连接的数据库优化的驱动程序使您的应用程序数据库不可知。只需外部化用户名、密码数据库驱动程序名称和连接字符串,不要使用任何特定于数据库的 SQL,你应该没问题。

For connecting to SQL Server, use the jTDS driver http://jtds.sourceforge.net/The connect string format looks like this:

要连接到 SQL Server,请使用 jTDS 驱动程序http://jtds.sourceforge.net/连接字符串格式如下所示:

jdbc:jtds:sqlserver://localhost/my_database

jdbc:jtds:sqlserver://localhost/my_database

There are a few other parameters you can include, separated by semicolons, but I think this is all that's required. Obviously when you connect, you'll need to supply a username and password.

您还可以包含其他一些参数,以分号分隔,但我认为这就是所需的全部内容。显然,当您连接时,您需要提供用户名和密码。

回答by Gripsoft

These days its quite easy to use Factory pattern and then load JDBC drivers to work with given databse. This architecture gives best of both worlds i.e. Flexibility and efficiency. The one downside of this is bit configuration/programming to handle dynamic loading but i hope so if you want to make it database agnostic that's the way to go.

如今,使用工厂模式非常容易,然后加载 JDBC 驱动程序以使用给定的数据库。这种架构提供了两全其美的优势,即灵活性和效率。这样做的一个缺点是处理动态加载的位配置/编程,但我希望如此,如果您想让它与数据库无关,那就是要走的路。