how to check wheather the mysql server is down or up using java code
Hi All,
I am using mysql as a database for my project.
Now i want to check wheather mysql server is running or not using java code
how to write the code for checking wheather the mysql server is running or not.
please help me
Thanks in advance.
Thanks,
Prakash
# 1
Obtain a connection with the MySQL database. If the database server is not running an exception gets generated.
# 2
Hi thanks for ur reply ,
can you please tell that what kind of exception i will get if the mysql server is down,
I am getting an exception called java.net.ConnectException ,
Is this the exception i will get if the mysql server is not running in the machine .
Please explain me ..
Thanks in advance.
Thanks,
Prakash
# 3
java.net.ConnectException indicates the MySQL server is not running.
# 4
java.net.ConnectException Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port).
# 5
"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port). " http://java.sun.com/j2se/1.4.2/docs/api/java/net/ConnectException.html
# 6
Thanks for ur updates
but now the problem is
am using code like this
catch(java.net.ConnectException e )
{
doing some process
}
but am not able to catch this exception here
instead am getting this exception under
catch(java.sql.SQLException e )
{
System.out.println(e);
}
how to catch the java.net.ConnectException
please help me
thanks in advance
thanks,
Prakash
# 7
Prakash,
The error is that you're unable to connect. As an earlier post has indicated, it could probably mean that there is no process waiting for connection requests ( = MySQL not running)
What do you mean by "catch the exception" ?
Have you tried the getCause() method?
D
# 8
Hi D,
Thank very much for ur updates .
see wen am trying to connect the mysql server if the server is down
then i should get the java.net.ConnectException right ? but now the problem is am not able to catch this exception if am using
catch(java.net.ConnectException e)
{
System.out.println(e);
}
i dont no why this ? am not able to catch this exception here instead
am getting java.net.connectException using this exception
catch(java.sql.SQLException e)
{
System.out.println(e)
}
the output am getting is :
java.sql.SQLEXcception :java.net.ConnectException :connection refused .
but actuall output shoud be
java.net.ConnectExceptoin: connection refused Right ?
are you getting my problem .
Main thing is I need to catch java.net.ConnectExcepton ..
please explain me ..
thanks in advance
thanks
~Prakash
# 9
Hi Prakash,When you are trying to get the connection and the db server is not running, then the actual exception(ConnectException) is wrapped inside the SQLException and hence you are not able to catch it in the catch block for ConnectException.-Jayant
# 10
Hi jayant,
Thank you very much for ur updates :
But now the problem is
now am trying to connecting to the mysql server and i want to do some process say i want to send a mail only if i get ConnectException
but if i cant catch this specific exception i cant send the email .
so please explain me how to catch that specific exception
i dont want to catch java.sql.SQLException ....
or Is there is any way to check the getting exception is ConnectException or something else in java.sql.SQLException catch block
please give me an idea ..
thanks in advance
Thanks,
Prakash
# 11
Hi Prakash,Well you need to catch the SQLException only, since this is the exception you would be getting in this condition. In the catch block you need to check the exception type (in your case ConnectException) and then proceed as your requirement.Thanks,Jayant
# 12
Hi Jayant,
Thanks for ur updates ..
Yes Jayant ,thats what am looking for
How to check the Exception type .
Please give me sample code or syntax to proceed more on this issue.
i want to check the ConnectException type under java.sql.SQLException
How to check it ? please provie me sample code or syntax if possible..
i tried in this way but its not working :
catch(java.sql.SQLException e)
{
if (e=="java.net.ConnectException")// getting error incompatible operation sqlexception and string ..
{
}
}
please give me an idea how to check the exception type .
thanks in advance
Thanks
Prakash
# 13
Hi Prakash,
You need to do like:
if(e instanceof ConnectException) {
...
} else {
...
}
-Jayant
# 14
> You need to do like:> if(e instanceof ConnectException) {No.That implies that SqlException is a ConnectException - it isn't.If the SqlException wraps the ConnectException then it will be found as a cause (see the Throwable API) of the SqlException
# 15
Hey Folks,
am in bit confusion , Give me a clear idea about to catch the ConnectException
coz i tried like this i got null value
catch(java.sql.SQLException e)
{
System.out.println(e.getCause());// am getting nulll value ..wat to do with this null value..
}
Explain me more...
Thanks in advance ...
thanks
Prakash
# 16
hi jschell,
I did not mean to exactly check instanceof ConnectException in the catch block (my fault of wrting like that), I was intending to suggest how it could be done. You are correct that ConnectException is not a SQLException.
Prakash,
You may need to check the SQL State and then take the proper action.
Thanks,
Jayant
# 17
Hi Folks,
Still i didnt get the solution for my issue.
catch(java.sql.SQLException e )
{
if( e instanceof java.net.ConnectException)
{
System.out.println("connect Exception");
}
System.out.println("Exception: " + e.getMessage());
}
see am connecting to one linux machine and in that machine it dont have any mysql server .so wen am try to connect to that machine using
String murl = "jdbc:mysql://" +"build-win"+ "/dbname";
Connection conn = DriverManager.getConnection(murl, "Stat","Pass");
i should get the Connect Exception ....
Yes am getting the exception but its not printing the connection exception -- see the above code
instead,
Its just printing the exception msg
Exception: Unable to connect to any hosts due to exception: java.net.ConnectException: Connection refused: connect
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused: connect
STACKTRACE:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
i want to catch this ConnectException only if its some other sql exception i should ignore those exceptions.
I dont no y this code is not executing since its a ConnectException
if( e instanceof java.net.ConnectException)
{
System.out.println("connect Exception");
}
how to do that please explain me with code if possible..
Thanks in advance
awating for ur updates ..
Thanks,
Prakash
# 18
Hi Jayant ,How to check the sql state I have tried e.getClause() its giving only null value and i dont no wat to do with this value.can u please explain me how to check the sql state..thanks in advance...Thanks,Prakash
# 19
> catch(java.sql.SQLException e )
> {
> if( e instanceof java.net.ConnectException)
> {
As I already said - that is wrong.
For that to work SQLException would have to have a parent child relationship with ConnectException.
And it doesn't.
>
> I dont no y this code is not executing since its a
> ConnectException
>
No it isn't.
I wouldn't be surprised if it is an inner exception however.
# 20
Hi jschell ,
Thanks for your updates .
To Make it very clear what is my requirements is :
Now am getting this exception if mysql server is not running in my machine under this exception
catch(java.sql.SQLException e)
{
System.out.println(e.getMessage()));
}
and am getting output as :
java.sql.SQLException: java.net.ConnectException Unable to connect to any hosts due to exception: java.net.ConnectException: Connection refused: connect
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused: connect
STACKTRACE:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
........etc...
so its very clear am getting this exception coz of mysql server is not running in the machine.
Now can you please tell me i want to do some process like sending email .. only when i get this ConnectException
Please give me idea for that..
How to put a code for this ...
I dont want to worry about any other sql exceptions. i need to do proceess only wen i get this Connect Exception ..
how to proceed more on this issue.
Thanks in advance ..
Thanks,
~Prakash
# 21
> Please give me idea for that.. The idea is that you look at the javadocs for Throwable. From one of the methods there you can get to the inner/nested exception.And which I suggested in reply #14.
# 22
Hi Jscell.
Thanks for ur updates.
sorry for putting more questions ..
I have tried this option in my codiNg part.
Note : mysql server is not running so i should get java.net.ConnectException
catch(java.sql.SQLException e)
{
e.getSQLState();
..
some coding here
}
i got the output as :
08S01
It gives the above value for e.getSQLState()- Is this the unique value for java.net.ConnectException.
Please update me .
one more question :
In what situations we will get this java.net.ConnectException ..
1. if mysql server is not running in the server and if we trying to connect to database we will get this exceptoin
..like that ..
wat are the other options we will get this java.net.ConnectException...
Or we will get this exception only mysql is not running . plz give me updates on this ..
Thanks in advance
ThanKs
~Prakash
# 23
Hi Jscell,Do u have any idea on this.(see my previous reply..)Reply me..Thanks,Prakash
# 24
> Hi Jscell,
>
> Do u have any idea on this.(see my previous reply..)
>
I have the idea that you are ignoring my suggestions and instead doing exactly what I said would not work.
The following is NOT a method of Throwable...
e.getSQLState();
Again you MUST read the java docs for Throwable.
# 25
> coz i tried like this i got null value
>
> catch(java.sql.SQLException e)
> {
> System.out.println(e.getCause());// am getting
> nulll value ..wat to do with this null value..
> }
>
I too find that the getCause() method of the SQLException returns null in this particular situation. So I've used the getMessage() method of Throwable to parse the message string
} catch( SQLException sle ) {
if ( sle.getMessage().indexOf("Connection refused") > 0 ) {
....
But jschell, is this the method to which you refer? I've always suspected that my code was a bad hack because ultimately mysql in it's discretion could alter the message itself, but perhaps not the exception that was thrown, no? So I was hoping to identify the exception as ConnectException conclusively in code but can't find a way. Like I've said, getCause() will return null, which is unfortunate because it seems to me that this situation should be an illustration of how getCause should work. What is it that I'm not seeing or doing right?
# 26
> > coz i tried like this i got null value
> >
> > catch(java.sql.SQLException e)
> > {
> > System.out.println(e.getCause());// am getting
> > nulll value ..wat to do with this null value..
> > }
> >
>
> I too find that the getCause() method of the
> SQLException returns null in this particular
> situation.
And what exactly why is that relevant in terms of the OP?
The OP posted a specific example in reply #17. That example does in fact, by definition, have a nested exception.
The fact that your case is different has nothing to do with that.
The result will vary by database and driver.
> But jschell, is this the method to which you refer?
> I've always suspected that my code was a bad hack
> because ultimately mysql in it's discretion could
> alter the message itself,
Correct, but there is no other way.
You can only do what the database/driver/version allows.
# 27
> > > coz i tried like this i got null value
> > >
> > > catch(java.sql.SQLException e)
> > > {
> > > System.out.println(e.getCause());// am
> getting
> > > nulll value ..wat to do with this null value..
> > > }
> > >
> >
> > I too find that the getCause() method of the
> > SQLException returns null in this particular
> > situation.
>
> And what exactly why is that relevant in terms of the
> OP?
>
> The OP posted a specific example in reply #17. That
> example does in fact, by definition, have a nested
> exception.
>
> The fact that your case is different has nothing to
> do with that.
I am in fact using MySQL which is why I've posted to this thread. The stack trace that appears when I attempt to connect to MySQL when the server has not been started is similar to the one that appears in post 17, referring explicitly to a nested exception.
> The result will vary by database and driver.
I am using MySQL v4.1 and the driver version I don't have offhand but it is unlikely that it is the most current so I will look into that. Thanks for noting the relevance of the driver.
> > But jschell, is this the method to which you
> refer?
> > I've always suspected that my code was a bad hack
> > because ultimately mysql in it's discretion could
> > alter the message itself,
>
> Correct, but there is no other way.
>
> You can only do what the database/driver/version
> allows.
This answers my question. Thanks.
# 28
>
> I am in fact using MySQL which is why I've posted to
> this thread. The stack trace that appears when I
> attempt to connect to MySQL when the server has not
> been started is similar to the one that appears in
> post 17, referring explicitly to a nested exception.
>
Either there is or isn't a nested exception attached to the exception that is thrown.
If there isn't then it means that the driver is attaching the nested part as text into the message itself.
# 29
> Either there is or isn't a nested exception attached
> to the exception that is thrown.
>
> If there isn't then it means that the driver is
> attaching the nested part as text into the message
> itself.
Ok then that must be what is happening. The driver is attaching the nested part as text.