Java Question

dynamic output for SHOW PROCESSLIST in mysql using JAVA

I know that using JDBC we can create output for queries inside a database, but how can I get the output of the command

SHOW PROCESSLIST
as an output of a Java program.

In mysql we get it by:
SHOW PROCESSLIST


But I was wondering if we can generate the output using Java?

Answer

You can use executeQuery for getting SHOW PROCESSLIST ResultSet.

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", password);
        Statement stmt = null;

        try {
                stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SHOW PROCESSLIST");
                while (rs.next())
                {
                    System.out.println(rs.getString("Host"));
                    System.out.println(rs.getString("Id"));
                    System.out.println(rs.getString("User"));
                    System.out.println(rs.getString("db"));
                    System.out.println(rs.getString("Command"));
                    System.out.println(rs.getString("state"));
                    System.out.println(rs.getString("info"));
                }
Comments