简体   繁体   中英

Java JDBC -> SQL Server

I'm trying to have a Java application talk to a Microsoft SQL Server database.

  • 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.
  • I manually install in local Maven repository.
  • 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.
  • The server is SQL Server 2005 on Windows 7.
  • The client is Java 7. I've tried Java 8 as well with no difference.

      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. If I comment out that, it hangs on the insert command. 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.

As soon as I quit the SQLWorkbenchJ application, my simple Java test app ran perfectly as expected. 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.

I have successfully used both the JTDS driver and the Microsoft driver (JDBC4). I have not tried the SQLWorkbenchJ , as I mostly use SQuirreL to test the drivers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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