DbQuery.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import java.sql.*;
  2. public class DbQuery {
  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=5000&socketTimeout=15000";
  5. String roleName = "\u8bbe\u5907\u7ef4\u4fee";
  6. String userName = "\u5f20\u51e4";
  7. String officeName = "\u6280\u672f\u8d28\u91cf";
  8. try (Connection c = DriverManager.getConnection(url, "mes_cloud", "s2ENj7KaEySTC7dW")) {
  9. run(c, "SELECT role_code, role_name, data_scope FROM js_sys_role WHERE role_code='dy0017' OR role_name LIKE '%" + roleName + "%'");
  10. run(c, "SELECT ctrl_type, ctrl_permi, ctrl_data FROM js_sys_role_data_scope WHERE role_code='dy0017' ORDER BY ctrl_type, ctrl_data");
  11. run(c, "SELECT u.user_code, u.login_code, u.user_name FROM js_sys_user u WHERE u.user_name LIKE '%" + userName + "%' LIMIT 10");
  12. run(c, "SELECT ur.user_code, u.user_name, ur.role_code FROM js_sys_user_role ur JOIN js_sys_user u ON u.user_code=ur.user_code WHERE ur.role_code='dy0017'");
  13. run(c, "SELECT o.office_code, o.office_name FROM js_sys_office o WHERE o.office_name LIKE '%" + officeName + "%'");
  14. run(c, "SELECT e.company_code, COUNT(*) c FROM js_sys_employee e WHERE e.office_code IN (SELECT office_code FROM js_sys_office WHERE office_name LIKE '%" + officeName + "%') AND e.status='0' GROUP BY e.company_code");
  15. run(c, "SELECT COUNT(*) cnt FROM js_sys_user u JOIN js_sys_employee e ON e.emp_code=u.ref_code WHERE u.user_type='employee' AND u.status='0' AND e.office_code IN (SELECT office_code FROM js_sys_office WHERE office_name LIKE '%" + officeName + "%')");
  16. run(c, "SELECT u.user_name, e.office_code, IFNULL(e.company_code,'') company_code FROM js_sys_user u JOIN js_sys_employee e ON e.emp_code=u.ref_code WHERE u.user_type='employee' AND u.status='0' AND e.office_code IN (SELECT office_code FROM js_sys_office WHERE office_name LIKE '%" + officeName + "%') AND (e.company_code IS NULL OR e.company_code='') ORDER BY u.user_name");
  17. }
  18. }
  19. static void run(Connection c, String sql) throws Exception {
  20. System.out.println("SQL> " + sql);
  21. try (Statement s = c.createStatement(); ResultSet rs = s.executeQuery(sql)) {
  22. ResultSetMetaData md = rs.getMetaData();
  23. int n = md.getColumnCount();
  24. int rows = 0;
  25. while (rs.next()) {
  26. StringBuilder sb = new StringBuilder();
  27. for (int i = 1; i <= n; i++) {
  28. if (i > 1) sb.append(" | ");
  29. sb.append(md.getColumnLabel(i)).append("=").append(rs.getString(i));
  30. }
  31. System.out.println(sb);
  32. rows++;
  33. }
  34. System.out.println("rows=" + rows);
  35. System.out.println("---");
  36. }
  37. }
  38. }