请在此处找到我的代码:
基本上,位于aa.aa.a.aaa的数据库连接失败,我继续收到以下错误:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLState: 08S01
VendorError: 0
SQLException: 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.
SQLState: 08S01
VendorError: 0
SQLException: 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.
SQLState: 08S01
VendorError: 0
SQLException: 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.
SQLState: 08S01
VendorError: 0
SQLException: Communications link failure
有没有办法,我可以退出Catch块,以便错误在一次让用户知道后停止打印,好的错误已经发生,然后从bb.bb.b开始转移到下一个IP地址。 bbb等等..
我的代码如下,
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import static com.Ostermiller.util.StringHelper.escapeSQL;
import static java.lang.Math.abs;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ResultsReader extends Task<Integer> {
private static int NumberOfResultsThreads = 10;
private static int NumberOfResultsContactsToProcessPerThread = 1000;
private static int NumberOfRecordsToInsertAtATime = 100;
private int CurrModValue;
private static String IP[];
@Override
protected Integer call() throws Exception {
int iterations = 0;
int i = 0;
// boolean done = false;
Statement SelectResultsStmt = null;
Statement UpdateResultsStmt = null;
Statement InsertRemoteResultsStmt = null;
Statement InsertRemoteResultsErrorLogStmt = null;
ResultSet SelectResultsRS = null;
Connection connRemote = null;
Connection connRemoteforCatch = null;
while (i <= 5) {
try {
Connection[] MainConnection = new Connection[5];
String[] RemoteIPAddress = new String[5];
RemoteIPAddress[0] = "aa.aa.a.aaa";
RemoteIPAddress[1] = "bb.bb.b.bbb";
RemoteIPAddress[2] = "cc.cc.c.ccc";
RemoteIPAddress[3] = "dd.dd.d.ddd";
RemoteIPAddress[4] = "ee.ee.e.eee";
RemoteIPAddress[5] = "ff.ff.f.fff";
RemoteIPAddress[6] = "gg.gg.g.ggg";
MainConnection[0] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[0] + ":3306/test", RemoteUser, RemotePass);
MainConnection[1] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[1] + ":3306/test", RemoteUser, RemotePass);
MainConnection[2] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[2] + ":3306/test", RemoteUser, RemotePass);
MainConnection[3] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[3] + ":3306/test", RemoteUser, RemotePass);
MainConnection[4] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[4] + ":3306/test", RemoteUser, RemotePass);
MainConnection[5] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[5] + ":3306/test", RemoteUser, RemotePass);
MainConnection[6] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[6] + ":3306/test", RemoteUser, RemotePass);
Connection connRemote = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test", MainUser, MainPass);
String QueryString = " SELECT "+ " // my table fields here+ " + " FROM "+ " RemoteIPtable "+ " ;
SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
SelectResultsRS = SelectResultsStmt.executeQuery(QueryString);
int CurrentBillingAmount = 0;
int CurrentRowCount = 0;
String QueryStringInsertFromRemote = "";
while (SelectResultsRS.next() ) {
CurrentRowCount++;
QueryStringInsertFromRemote = " INSERT INTO mytabletest "
+ " ( " + " + " + " + "" + " ) "
+ " VALUES "
+ " ( " + " NULL, " + " " + " ) ";
InsertRemoteResultsStmt = connRemote.createStatement();
InsertRemoteResultsStmt.executeUpdate(QueryStringInsertFromRemote);
// reset string to empty
QueryStringInsertFromRemote = "";
SelectResultsRS.updateRow();
}
System.out.println("IP Just Completed is:" + RemoteIPAddress[i]);
i++;
System.out.println("Value of i is :" + i);
}
catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
} // END Of WHILE
}
return iterations;
}
我在SOF的某个地方读过,有人建议在catch块结束后使用continue
语句,但这对我不起作用。
答案 0 :(得分:0)
是..这样做..
...
boolean flag = false;
while (i <= 5) {
...
try{ ... }
catch (SQLException ex) {
if(!flag){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
flag = true;
}
i++;
}
...
答案 1 :(得分:0)
boolean flag = false; //declare boolean variable
while (i <= 5) {
//Your code
try { ...
} catch (SQLException ex) {
if(flag){
continue;
}
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
flag = true;
i++;
}