DbQuery5.java 1.8 KB

12345678910111213141516171819202122232425
  1. import java.sql.*;
  2. public class DbQuery5 {
  3. public static void main(String[] args) throws Exception {
  4. String url = "jdbc:mysql://192.168.9.181:3306/mes_cloud?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&connectTimeout=8000&socketTimeout=15000";
  5. try (Connection c = DriverManager.getConnection(url, "mes_cloud", "s2ENj7KaEySTC7dW")) {
  6. q(c, "SELECT COUNT(*) total_110 FROM js_sys_employee WHERE company_code='110'");
  7. q(c, "SELECT COUNT(*) under_sd FROM js_sys_employee e JOIN js_sys_office o ON o.office_code=e.office_code WHERE e.company_code='110' AND (o.office_code='SD' OR o.parent_code='SD' OR o.parent_codes LIKE '%,SD,%')");
  8. q(c, "SELECT e.office_code, o.office_name, o.parent_code, COUNT(*) c FROM js_sys_employee e LEFT JOIN js_sys_office o ON o.office_code=e.office_code WHERE e.company_code='110' GROUP BY e.office_code, o.office_name, o.parent_code ORDER BY c DESC");
  9. q(c, "SELECT e.emp_code, e.emp_name, e.office_code, o.office_name, o.parent_code FROM js_sys_employee e LEFT JOIN js_sys_office o ON o.office_code=e.office_code WHERE e.company_code='110' AND NOT (o.office_code='SD' OR o.parent_code='SD' OR IFNULL(o.parent_codes,'') LIKE '%,SD,%') LIMIT 50");
  10. }
  11. }
  12. static void q(Connection c, String sql) throws Exception {
  13. System.out.println("SQL> " + sql);
  14. try (Statement s = c.createStatement(); ResultSet rs = s.executeQuery(sql)) {
  15. ResultSetMetaData md = rs.getMetaData();
  16. int n = md.getColumnCount(); int rows=0;
  17. while (rs.next()) {
  18. StringBuilder sb=new StringBuilder();
  19. for (int i=1;i<=n;i++){ if(i>1)sb.append(" | "); sb.append(md.getColumnLabel(i)).append("=").append(rs.getString(i)); }
  20. System.out.println(sb); rows++;
  21. }
  22. System.out.println("rows="+rows); System.out.println("---");
  23. }
  24. }
  25. }