This class is for testing the connectivity to a database. Very useful to avoid data missing during db server is down. Data can be save in file or store in another db server temporarily.
package com.blogspot.lxnx;
import java.io.*;
import java.net.*;
public class Pinger {
public static void main(String args[]) {
try {
InetAddress addr = InetAddress.getByName("localhost");
int port = 3306;
SocketAddress sockaddr = new InetSocketAddress(addr, port);
// Create an unbound socket
Socket sock = new Socket();
// This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
System.out.println("Alive!");
} catch (UnknownHostException e) {
System.err.println("Unable to lookup");
} catch (SocketTimeoutException e) {
System.err.println("Connection timeout");
} catch (IOException e) {
System.err.println("Unable to reach");
}
}
}
credit to http://www.exampledepot.com/egs/java.net/CreateClientSocket.html