import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class ExecuteSql {
    public static void main(String[] args) {
        String url = "jdbc:dm://8.142.21.6:5236?schema=JZ_TEST";
        String username = "SYSDBA";
        String password = "Dameng1@34";
        String sqlFile = "d:/Work/2-项目/14-长春司法智慧矫正系统/6、Code/ethereal-whispers/05-dev-env/code/platform/sql/sys_area_jilin.sql";
        
        try {
            Class.forName("dm.jdbc.driver.DmDriver");
            Connection conn = DriverManager.getConnection(url, username, password);
            conn.setAutoCommit(false);
            Statement stmt = conn.createStatement();
            
            BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
            StringBuilder sqlBuilder = new StringBuilder();
            String line;
            int count = 0;
            
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty() || line.startsWith("--")) {
                    continue;
                }
                sqlBuilder.append(line);
                if (line.endsWith(";")) {
                    String sql = sqlBuilder.toString();
                    stmt.executeUpdate(sql);
                    count++;
                    sqlBuilder = new StringBuilder();
                    if (count % 100 == 0) {
                        System.out.println("已执行 " + count + " 条SQL语句");
                    }
                }
            }
            
            conn.commit();
            stmt.close();
            conn.close();
            reader.close();
            
            System.out.println("执行完成！共执行 " + count + " 条SQL语句");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
