| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.sql.SQLException;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- public class FixEncodingCorrect {
- public static void main(String[] args) {
- // Database connection information
- String url = "jdbc:dm://192.168.0.145:30236?charset=utf8";
- String username = "WATERSHED";
- String password = "WaterShed./1224";
-
- Connection conn = null;
- Statement stmt = null;
-
- try {
- // Load driver
- Class.forName("dm.jdbc.driver.DmDriver");
-
- // Establish connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(url, username, password);
- stmt = conn.createStatement();
-
- // Set UTF-8 encoding for the session
- System.out.println("Setting UTF-8 encoding...");
- stmt.executeUpdate("SET NAMES 'UTF8'");
- stmt.executeUpdate("SET CLIENT_ENCODING = 'UTF8'");
-
- // Read and execute table creation SQL from project.sql
- System.out.println("Reading table creation SQL...");
- String createSql = readFile("d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project.sql");
-
- // Split SQL statements (split by semicolon + newline)
- String[] createStatements = createSql.split(";");
- for (String sql : createStatements) {
- sql = sql.trim();
- if (!sql.isEmpty()) {
- System.out.println("Executing: " + sql.substring(0, Math.min(sql.length(), 50)) + "...");
- stmt.executeUpdate(sql);
- }
- }
-
- // Read and execute data insertion SQL from project_data.sql
- System.out.println("Reading data insertion SQL...");
- String dataSql = readFile("d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project_data.sql");
-
- // Split and execute data statements
- String[] dataStatements = dataSql.split(";");
- for (String sql : dataStatements) {
- sql = sql.trim();
- if (!sql.isEmpty()) {
- System.out.println("Executing: " + sql.substring(0, Math.min(sql.length(), 50)) + "...");
- stmt.executeUpdate(sql);
- }
- }
-
- System.out.println("\n✅ SUCCESS!");
- System.out.println("Table 'PROJECT' created successfully with 8 projects.");
- System.out.println("All Chinese characters should now display correctly.");
-
- } catch (ClassNotFoundException e) {
- System.err.println("Driver error: " + e.getMessage());
- e.printStackTrace();
- } catch (SQLException e) {
- System.err.println("SQL error: " + e.getMessage());
- e.printStackTrace();
- } catch (IOException e) {
- System.err.println("File error: " + e.getMessage());
- e.printStackTrace();
- } finally {
- // Close connection
- try {
- if (stmt != null) stmt.close();
- if (conn != null) conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-
- // Read file with UTF-8 encoding
- private static String readFile(String filePath) throws IOException {
- StringBuilder content = new StringBuilder();
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(
- new FileInputStream(filePath), StandardCharsets.UTF_8))) {
- String line;
- while ((line = reader.readLine()) != null) {
- content.append(line).append("\n");
- }
- }
- return content.toString();
- }
- }
|