This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes check app server and databse server are up or down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "check app server and databse server are up or down" Watch "check app server and databse server are up or down" New topic

check app server and databse server are up or down

Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
I have webapplication which is built on struts

I have a standalone progrmme which has to check whether the app server(JBoss) as well as database server are running are not....

I am able to check app server is down or not

but How can I check the database server is running or not

this is the standalone app which I have

package com.jps.healthcheck;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;


import com.jps.utils.ConfigReader;
import com.jps.utils.DbBean;
import com.jps.Mail.SendMail;

public class HealthChecker {

public static void main (String[] args) {

URL url;
HttpURLConnection httpUrlConnection;

InputStream inputStream = null;
DataInputStream dataInputStream;
String contents;

int switchParam=0;
String message="";
try {
url = new URL("http://localhost:8080/NewAplusStruts/");
httpUrlConnection = (HttpURLConnection)url.openConnection();
httpUrlConnection .connect();
System.out.println("httpUrlConnection .getResponseMessage():"+httpUrlConnection .getResponseMessage());
System.out.println("urlConnection.getResponseCode():"+httpUrlConnection .getResponseCode()+" HttpURLConnection.HTTP_OK:"+HttpURLConnection.HTTP_OK);
if (httpUrlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println(" Server is running succesfully.............");

inputStream = url.openStream();
dataInputStream = new DataInputStream(new BufferedInputStream(inputStream));
while ((contents = dataInputStream.readLine()) != null) {

case 200:
System.out.println("in the case 200............................");
case 500:
message="Internal Server Error";
default :
message="Internal Server Error";

} catch (MalformedURLException mue) {
System.out.println("Ouch - a MalformedURLException happened.");

catch(ConnectException ce){
System.out.println("Not able to connect to the Application server...........");
message=" Server is not running";
catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
message="Either the Application Server or Database server is not running";

public static void sendMail(String message){
ConfigReader configReader=new ConfigReader();
String From=configReader.getpropertyinfo("from");
String To=configReader.getpropertyinfo("to");
String Subject=configReader.getpropertyinfo("subject");
String Message=message;
//String Message=configReader.getpropertyinfo("message");

System.out.println("Mail sent");

// end of main
} // end of class definition

"And the trouble is, if you don't risk anything, you risk even more." -- Erica Jong.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
For the database just connect, do a simple query and disconnect.

I have a little site checker like this with a few other features you are free to borrow. It reports errors for: the page comes back but takes longer than a max time, the page contains any of a set of "must not find" strings, the page does not contain any of a set of "must find" strings.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Don't get me started about those stupid light bulbs.
subject: check app server and databse server are up or down