有人可以解释为什么以下程序在3秒钟后超时,而我将其设置为
12秒我故意关闭了mysql服务器,以测试无法访问mysql服务器的情况.
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author dhd
*/
public class TestMysql {
static Thread trd;
public static void main(String[] argv) {
keepTrack();
try {
DriverManager.setLoginTimeout(12);
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/driving", "root", "");
} catch (Exception ex) {
System.err.println(ex.getMessage());
trd.stop();
}
}
public static void keepTrack() {
trd = new Thread(new Runnable() {
@Override
public void run() {
int i = 1;
while (true) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (Exception ex) {
}
i++;
}
}
});
trd.start();
}
}
输出为:
run:
1
2
3
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
BUILD SUCCESSFUL (total time: 3 seconds).
从netbeans运行.在问我为什么需要这个之前,请先回答.谢谢