我对使用 jTDS JDBC 驱动程序的以下代码有疑问。一切正常,查询也没问题。但如果连接失败,我不会收到错误/异常。我曾尝试输入错误的 IP、禁用本地网络连接、提供错误的端口号等,但没有运气。我真的需要知道连接何时失败。
似乎一切都停在了这一行:“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();
}
}