Ping MySQL Server Using JDBC

user552245 picture user552245 · Mar 26, 2011 · Viewed 13.5k times · Source

This seems rather simple (not for me)...

How does one ping a mysql server using the JDBC? i have successfully used select and insert queries with JDBC and MySQL, but how to ping???

TIA

Answer

Brian Roach picture Brian Roach · Mar 27, 2011

The MySQL JDBC driver (Connector/J) provides a ping mechanism.

If you do a SQL query prepended with /* ping */ such as:

"/* ping */ SELECT 1"

This will actually cause the driver send a ping to the server and return a fake, light-weight, result set.

(You can find this buried fairly deep in the Connector/J documentation; search for "ping" on that page. Read it carefully: this mechanism is very sensitive to the syntax used. Unlike most SQL, the "parsing" of the "ping" marker happens in the client-side JDBC driver itself.).