Home > Back-end >  No matter how to set access database timeout, after pulling cable are abnormal card 20 seconds to co
No matter how to set access database timeout, after pulling cable are abnormal card 20 seconds to co

Time:11-21

Is router changed the rules in recent years, long even outside the database from home, may be half an hour or so connections will be failure, then the query will card for 20 seconds, so I want to send this card are set too short time, with the program after the operation, pull cable way to test, how to set the timeout or card for 20 seconds, want how to set up effective? Or can't set?
 package cn. Yishou. Testmysql; 

Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import Java, SQL Statement;
Import the Java. Util. Concurrent. Executor;

Public class TestMySQL {
The static final String url="JDBC: mysql://80.240.28.38:2135/Db01? AllowMultiQueries=true& amp; useUnicode=true& amp; CharacterEncoding=utf8 & amp; User=guest2 & amp; Password=test009 ";
The static final String SQL="SELECT 666 a";
The static final int total=5;//loop execution times
The static final ints interval=2000;//every time interval of milliseconds

Public static void main (String [] args) throws the Exception {
Class.forName("com.mysql.jdbc.Driver");
The Connection con=DriverManager. GetConnection (url);
Executor Executor=new Executor () {
@ Override
Public void the execute (Runnable command) {
System. The out. Println (" I don't know what's the use of ");
}
};
Con. SetNetworkTimeout (executor, 3000);//set the connection timeout
//
Long=System. CurrentTimeMillis ()/1000;
//
Try {
For (int I=0; i System. Out. Println (the String. Format (" \ n the first seconds=% d % d times ", I + 1, System. CurrentTimeMillis ()/1000 - now));
Thread.sleep (interval);
The query (con, SQL);
}
//
con.close();
System. Out. Println (the String. Format (" to finish second=% d ", System. CurrentTimeMillis ()/1000 - now));
} the catch (Exception e) {
System. Out. Println (the String. Format (" error seconds=% d ", System. CurrentTimeMillis ()/1000 - now));
e.printStackTrace();
}
}

Private static void query (Connection con, String SQL) throws SQLException {
The Statement Statement=con. CreateStatement ();
The statement. SetQueryTimeout (2000);//set the query timeout
The ResultSet set=statement. ExecuteQuery (SQL);
Set. The next ();
Long value=https://bbs.csdn.net/topics/set.getLong (1);
System. Out. Println (" value="HTTP://https://bbs.csdn.net/topics/+ value);
Set. The close ();
}

}



  • Related