我使用jTDS JDBC驅動程序下面的代碼有問題。一切正常,查詢不成問題。但是如果連接失敗,我不會收到錯誤/異常。我試圖輸入一個錯誤的IP,禁用本地網絡連接,提供假端口號等,但沒有運氣。我真的需要知道連接何時失敗。jTDS JDBC沒有拋出異常,當它應該
看來,一切都停在行:「con = java.sql.DriverManager.getConnection(url,id,pass);」 (但只有當它真的應該拋出異常...)
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
java.sql.Connection con = null;
String url= "jdbc:jtds:sqlserver://x.x.x.x/DATABASE";
String id= "seret";
String pass = "secret";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
System.out.println("Connecting to database...");
con = java.sql.DriverManager.getConnection(url, id, pass);
System.out.println("Connected?")
//Program never gets here, but does not close either.
if(con.isValid(1000)) System.out.println("Does not work either...");
if(con!=null) con.close();
}
}