Ping a MySQL server

前端 未结 3 928
暖寄归人
暖寄归人 2020-12-03 17:43

Is there a specific method in Java that checks whether a MySQL server is alive?

3条回答
  •  生来不讨喜
    2020-12-03 18:11

    It depends on what method you use to connect to the database. Some drivers, eg MySQL Connector/J do have a ping command:

    /**
    * Detect if the connection is still good by sending a ping command
    * to the server.
    * 
    * @throws SQLException
    *             if the ping fails
    */
    public abstract void ping() throws SQLException;
    

    If you an interface that does not support this then a simple method that always works is to connect to the database as you ordinarily would and execute the following query:

    SELECT 1
    

    You should get back a result set containing a single row, with a single column, containg the value 1. If this succeeds your MySQL is working.

提交回复
热议问题