| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.sql.SQLException;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class CreateTable {
- 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();
-
- // Execute create table SQL
- System.out.println("Executing create table SQL...");
- executeSqlFile(conn, "d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project.sql");
-
- // Execute insert data SQL
- System.out.println("Executing insert data SQL...");
- executeSqlFile(conn, "d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project_data.sql");
-
- System.out.println("Table created and data inserted successfully!");
-
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // Close connection
- try {
- if (stmt != null) stmt.close();
- if (conn != null) conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-
- // Execute SQL file
- private static void executeSqlFile(Connection conn, String filePath) throws SQLException, IOException {
- BufferedReader reader = new BufferedReader(new FileReader(filePath));
- String line;
- StringBuilder sql = new StringBuilder();
-
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- // Skip comments and empty lines
- if (line.isEmpty() || line.startsWith("--")) {
- continue;
- }
- // Skip CREATE TABLE IF NOT EXISTS - we want to drop and recreate
- if (line.toUpperCase().startsWith("DROP TABLE IF EXISTS")) {
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(line);
- stmt.close();
- continue;
- }
- // Skip BEGIN and COMMIT statements
- if (line.toUpperCase().equals("BEGIN;") || line.toUpperCase().equals("COMMIT;")) {
- continue;
- }
- // Append line to SQL
- sql.append(" ").append(line);
- // If ends with semicolon, execute SQL
- if (line.endsWith(";")) {
- String sqlStatement = sql.toString().trim();
- if (!sqlStatement.isEmpty()) {
- Statement stmt = conn.createStatement();
- try {
- stmt.executeUpdate(sqlStatement);
- } catch (SQLException e) {
- System.err.println("Error executing SQL: " + sqlStatement);
- throw e;
- } finally {
- stmt.close();
- }
- }
- sql = new StringBuilder();
- }
- }
-
- // Execute any remaining SQL
- String remainingSql = sql.toString().trim();
- if (!remainingSql.isEmpty()) {
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(remainingSql);
- stmt.close();
- }
-
- reader.close();
- }
- }
|