简体   繁体   English

Java JDBC-> SQL Server

[英]Java JDBC -> SQL Server

I'm trying to have a Java application talk to a Microsoft SQL Server database. 我正在尝试让Java应用程序与Microsoft SQL Server数据库对话。

  • I downloaded sqljdbc_4.0.2206.100_enu.tar.gz from the Microsoft web site as this isn't hosted in the normal Maven-style Java library repositories. 我从Microsoft网站下载了sqljdbc_4.0.2206.100_enu.tar.gz,因为它不在正常的Maven风格的Java库存储库中。
  • I manually install in local Maven repository. 我手动安装在本地Maven存储库中。
  • I use the following super simple test code: 我使用以下超级简单的测试代码:
  • I can access the database in SQLWorkbenchJ which is a Java based SQL query tool and get normal, instant responses to these SQL commands. 我可以使用基于Java的SQL查询工具SQLWorkbenchJ来访问数据库,并获得对这些SQL命令的正常即时响应。
  • The server is SQL Server 2005 on Windows 7. 该服务器是Windows 7上的SQL Server 2005。
  • The client is Java 7. I've tried Java 8 as well with no difference. 客户端是Java7。我也尝试过Java 8,没有任何区别。

      public static void superSimpleJdbcTest(String jdbcUrl, String user, String password) throws SQLException { try (Connection dbConnection = DriverManager.getConnection(jdbcUrl, user, password)) { System.out.println("opened connection"); try (Statement statement = dbConnection.createStatement()) { System.out.println("created statement"); System.out.println("executing create table command..."); statement.execute("create table something (id int primary key, name varchar(100))"); System.out.println("completed create table command"); System.out.println("executing insert command..."); statement.executeUpdate("insert into something (id, name) values (1, 'Brian')"); System.out.println("insert command completed"); } } } 

It sporadically works, but usually hangs on the create table command. 它偶尔会起作用,但通常会挂在create table命令上。 If I comment out that, it hangs on the insert command. 如果我将其注释掉,它将挂在insert命令上。 I can't imagine what is wrong. 我无法想象出什么问题了。

When the SQLWorkbenchJ application was running, my simple Java test app, would open the connection and just hang forever on any SQL command without getting any errors. 当SQLWorkbenchJ应用程序运行时,我的简单Java测试应用程序将打开连接,并永久挂在任何SQL命令上,而不会出现任何错误。

As soon as I quit the SQLWorkbenchJ application, my simple Java test app ran perfectly as expected. 退出SQLWorkbenchJ应用程序后,我的简单Java测试应用程序就按预期运行了。 That was really weird and I eventually stumbled on the answer. 那真的很奇怪,我最终迷失了答案。

I switched over to using RazorSQL as an SQL console and that doesn't seem to have any issues like this. 我转而使用RazorSQL作为SQL控制台,这似乎没有任何问题。

I have successfully used both the JTDS driver and the Microsoft driver (JDBC4). 我已经成功使用了JTDS驱动程序和Microsoft驱动程序(JDBC4)。 I have not tried the SQLWorkbenchJ , as I mostly use SQuirreL to test the drivers. 我没有尝试过SQLWorkbenchJ ,因为我主要使用SQuirreL来测试驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM