CreateTable.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.SQLException;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. public class CreateTable {
  9. public static void main(String[] args) {
  10. // Database connection information
  11. String url = "jdbc:dm://192.168.0.145:30236?charset=utf8";
  12. String username = "WATERSHED";
  13. String password = "WaterShed./1224";
  14. Connection conn = null;
  15. Statement stmt = null;
  16. try {
  17. // Load driver
  18. Class.forName("dm.jdbc.driver.DmDriver");
  19. // Establish connection
  20. System.out.println("Connecting to database...");
  21. conn = DriverManager.getConnection(url, username, password);
  22. stmt = conn.createStatement();
  23. // Execute create table SQL
  24. System.out.println("Executing create table SQL...");
  25. executeSqlFile(conn, "d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project.sql");
  26. // Execute insert data SQL
  27. System.out.println("Executing insert data SQL...");
  28. executeSqlFile(conn, "d:\\Web\\DN\\ruoyi-admin\\src\\main\\resources\\sql\\project_data.sql");
  29. System.out.println("Table created and data inserted successfully!");
  30. } catch (ClassNotFoundException e) {
  31. e.printStackTrace();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. } finally {
  37. // Close connection
  38. try {
  39. if (stmt != null) stmt.close();
  40. if (conn != null) conn.close();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. // Execute SQL file
  47. private static void executeSqlFile(Connection conn, String filePath) throws SQLException, IOException {
  48. BufferedReader reader = new BufferedReader(new FileReader(filePath));
  49. String line;
  50. StringBuilder sql = new StringBuilder();
  51. while ((line = reader.readLine()) != null) {
  52. line = line.trim();
  53. // Skip comments and empty lines
  54. if (line.isEmpty() || line.startsWith("--")) {
  55. continue;
  56. }
  57. // Skip CREATE TABLE IF NOT EXISTS - we want to drop and recreate
  58. if (line.toUpperCase().startsWith("DROP TABLE IF EXISTS")) {
  59. Statement stmt = conn.createStatement();
  60. stmt.executeUpdate(line);
  61. stmt.close();
  62. continue;
  63. }
  64. // Skip BEGIN and COMMIT statements
  65. if (line.toUpperCase().equals("BEGIN;") || line.toUpperCase().equals("COMMIT;")) {
  66. continue;
  67. }
  68. // Append line to SQL
  69. sql.append(" ").append(line);
  70. // If ends with semicolon, execute SQL
  71. if (line.endsWith(";")) {
  72. String sqlStatement = sql.toString().trim();
  73. if (!sqlStatement.isEmpty()) {
  74. Statement stmt = conn.createStatement();
  75. try {
  76. stmt.executeUpdate(sqlStatement);
  77. } catch (SQLException e) {
  78. System.err.println("Error executing SQL: " + sqlStatement);
  79. throw e;
  80. } finally {
  81. stmt.close();
  82. }
  83. }
  84. sql = new StringBuilder();
  85. }
  86. }
  87. // Execute any remaining SQL
  88. String remainingSql = sql.toString().trim();
  89. if (!remainingSql.isEmpty()) {
  90. Statement stmt = conn.createStatement();
  91. stmt.executeUpdate(remainingSql);
  92. stmt.close();
  93. }
  94. reader.close();
  95. }
  96. }