FixEncodingCorrect.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.SQLException;
  5. import java.io.*;
  6. import java.nio.charset.StandardCharsets;
  7. public class FixEncodingCorrect {
  8. public static void main(String[] args) {
  9. // Database connection information
  10. String url = "jdbc:dm://192.168.0.145:30236?charset=utf8";
  11. String username = "WATERSHED";
  12. String password = "WaterShed./1224";
  13. Connection conn = null;
  14. Statement stmt = null;
  15. try {
  16. // Load driver
  17. Class.forName("dm.jdbc.driver.DmDriver");
  18. // Establish connection
  19. System.out.println("Connecting to database...");
  20. conn = DriverManager.getConnection(url, username, password);
  21. stmt = conn.createStatement();
  22. // Set UTF-8 encoding for the session
  23. System.out.println("Setting UTF-8 encoding...");
  24. stmt.executeUpdate("SET NAMES 'UTF8'");
  25. stmt.executeUpdate("SET CLIENT_ENCODING = 'UTF8'");
  26. // Read and execute table creation SQL from project.sql
  27. System.out.println("Reading table creation SQL...");
  28. String createSql = readFile("d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project.sql");
  29. // Split SQL statements (split by semicolon + newline)
  30. String[] createStatements = createSql.split(";");
  31. for (String sql : createStatements) {
  32. sql = sql.trim();
  33. if (!sql.isEmpty()) {
  34. System.out.println("Executing: " + sql.substring(0, Math.min(sql.length(), 50)) + "...");
  35. stmt.executeUpdate(sql);
  36. }
  37. }
  38. // Read and execute data insertion SQL from project_data.sql
  39. System.out.println("Reading data insertion SQL...");
  40. String dataSql = readFile("d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project_data.sql");
  41. // Split and execute data statements
  42. String[] dataStatements = dataSql.split(";");
  43. for (String sql : dataStatements) {
  44. sql = sql.trim();
  45. if (!sql.isEmpty()) {
  46. System.out.println("Executing: " + sql.substring(0, Math.min(sql.length(), 50)) + "...");
  47. stmt.executeUpdate(sql);
  48. }
  49. }
  50. System.out.println("\n✅ SUCCESS!");
  51. System.out.println("Table 'PROJECT' created successfully with 8 projects.");
  52. System.out.println("All Chinese characters should now display correctly.");
  53. } catch (ClassNotFoundException e) {
  54. System.err.println("Driver error: " + e.getMessage());
  55. e.printStackTrace();
  56. } catch (SQLException e) {
  57. System.err.println("SQL error: " + e.getMessage());
  58. e.printStackTrace();
  59. } catch (IOException e) {
  60. System.err.println("File error: " + e.getMessage());
  61. e.printStackTrace();
  62. } finally {
  63. // Close connection
  64. try {
  65. if (stmt != null) stmt.close();
  66. if (conn != null) conn.close();
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72. // Read file with UTF-8 encoding
  73. private static String readFile(String filePath) throws IOException {
  74. StringBuilder content = new StringBuilder();
  75. try (BufferedReader reader = new BufferedReader(new InputStreamReader(
  76. new FileInputStream(filePath), StandardCharsets.UTF_8))) {
  77. String line;
  78. while ((line = reader.readLine()) != null) {
  79. content.append(line).append("\n");
  80. }
  81. }
  82. return content.toString();
  83. }
  84. }