]> WPIA git - gigi.git/blob - util/org/cacert/gigi/util/DatabaseManager.java
e43eeb36203ed71c550d4ce7ce08cdd24267a9a4
[gigi.git] / util / org / cacert / gigi / util / DatabaseManager.java
1 package org.cacert.gigi.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.SQLException;
9 import java.sql.Statement;
10
11 public class DatabaseManager {
12         public static String readFile(File f) throws IOException {
13                 return new String(Files.readAllBytes(f.toPath()));
14         }
15         public static void main(String[] args) throws SQLException,
16                         ClassNotFoundException, IOException {
17                 if (args.length < 4) {
18                         System.err
19                                         .println("Usage: com.mysql.jdbc.Driver jdbc:mysql://localhost/cacert user password");
20                         return;
21                 }
22                 run(args);
23         }
24         public static void run(String[] args) throws ClassNotFoundException,
25                         SQLException, IOException {
26                 Class.forName(args[0]);
27                 Connection conn = DriverManager
28                                 .getConnection(args[1], args[2], args[3]);
29                 Statement stmt = conn.createStatement();
30                 String sql = readFile(new File("doc/tableStructure.sql"));
31                 String[] stmts = sql.split(";");
32                 for (String string : stmts) {
33                         if (!string.trim().equals("")) {
34                                 stmt.addBatch(string);
35                         }
36                 }
37                 stmt.executeBatch();
38                 stmt.close();
39         }
40 }