日期:2014-05-19 浏览次数:20791 次
JDBC connect successful! java.sql.SQLException: Unable to get information from SQL Server: localhost. at net.sourceforge.jtds.jdbc.MSSqlServerInfo.<init>(MSSqlServerInfo.java:97) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:276) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at com.jdbc.Test.JTDS(Test.java:51) at com.jdbc.Test.<init>(Test.java:18) at com.jdbc.Test.main(Test.java:13)
package com.jdbc; import java.sql.Connection; public class Test{ /** * @param args */ public static void main(String[] args) { new Test(); } public Test() { JDBC(); JTDS(); } private void JDBC() { Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1243;DatabaseName=test;instance=test1"; conn = DriverManager.getConnection(url, "test", "test"); if (conn != null) { System.out.println("JDBC connect successful!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } private void JTDS() { Connection conn = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver()); String url = "jdbc:jtds:sqlserver://localhost:1243;DatabaseName=test;instance=test1"; conn = DriverManager.getConnection(url, "test", "test"); if (conn != null) { System.out.println("JTDS connect successful!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }