import java.sql.*; public class DbQuery4 { public static void main(String[] args) throws Exception { String url = "jdbc:mysql://192.168.9.181:3306/mes_cloud?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&connectTimeout=5000&socketTimeout=15000"; try (Connection c = DriverManager.getConnection(url, "mes_cloud", "s2ENj7KaEySTC7dW")) { q(c, "SELECT company_code, COUNT(*) c FROM js_sys_employee WHERE status='0' GROUP BY company_code ORDER BY c DESC"); q(c, "SELECT e.office_code, o.office_name, COUNT(*) c FROM js_sys_employee e LEFT JOIN js_sys_office o ON o.office_code=e.office_code WHERE e.status='0' AND e.company_code='110' GROUP BY e.office_code, o.office_name ORDER BY c DESC"); q(c, "SELECT COUNT(*) c FROM js_sys_employee WHERE company_code='110'"); } } static void q(Connection c, String sql) throws Exception { System.out.println("SQL> " + sql); try (Statement s = c.createStatement(); ResultSet rs = s.executeQuery(sql)) { ResultSetMetaData md = rs.getMetaData(); int n = md.getColumnCount(); int rows=0; while (rs.next()) { StringBuilder sb=new StringBuilder(); for (int i=1;i<=n;i++){ if(i>1)sb.append(" | "); sb.append(md.getColumnLabel(i)).append("=").append(rs.getString(i)); } System.out.println(sb); rows++; } System.out.println("rows="+rows); System.out.println("---"); } } }