diff -r 74139325d305 -r c81406884e16 src/org/sonews/util/DatabaseSetup.java
--- a/src/org/sonews/util/DatabaseSetup.java Sun Aug 29 18:17:37 2010 +0200
+++ b/src/org/sonews/util/DatabaseSetup.java Tue Oct 25 15:06:59 2011 +0200
@@ -15,13 +15,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-
package org.sonews.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
+import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
@@ -32,8 +32,7 @@
* @author Christian Lins
* @since sonews/0.5.0
*/
-public final class DatabaseSetup
-{
+public final class DatabaseSetup {
private static final Map templateMap = new HashMap();
private static final Map urlMap = new HashMap();
@@ -42,58 +41,79 @@
static {
templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
+ templateMap.put("3", "helpers/database_hsqldb_tmpl.sql");
urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
driverMap.put("1", "com.mysql.jdbc.Driver");
driverMap.put("2", "org.postgresql.Driver");
+ driverMap.put("3", "org.hsqldb.jdbcDriver");
}
public static void main(String[] args)
- throws Exception
- {
- System.out.println("sonews Database setup helper");
- System.out.println("This program will create a initial database table structure");
- System.out.println("for the sonews Newsserver.");
- System.out.println("You need to create a database and a db user manually before!");
+ throws Exception {
+
+ loadJDBCDriver();
- System.out.println("Select DBMS type:");
- System.out.println("[1] MySQL 5.x or higher");
- System.out.println("[2] PostgreSQL 8.x or higher");
- System.out.print("Your choice: ");
+ if (args.length == 0) {
+ System.out.println("sonews Database setup helper");
+ System.out.println("This program will create a initial database table structure");
+ System.out.println("for the sonews Newsserver.");
+ System.out.println("You need to create a database and a db user manually before!");
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- String dbmsType = in.readLine();
- String tmplName = templateMap.get(dbmsType);
- if (tmplName == null) {
- System.err.println("Invalid choice. Try again you fool!");
- main(args);
- return;
+ System.out.println("Select DBMS type:");
+ System.out.println("[1] MySQL 5.x or higher");
+ System.out.println("[2] PostgreSQL 8.x or higher");
+ System.out.print("Your choice: ");
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String dbmsType = in.readLine();
+ String tmplName = templateMap.get(dbmsType);
+ if (tmplName == null) {
+ System.err.println("Invalid choice. Try again you fool!");
+ main(args);
+ return;
+ }
+
+ String tmpl = Resource.getAsString(tmplName, true);
+
+ System.out.print("Database server hostname (e.g. localhost): ");
+ String dbHostname = in.readLine();
+
+ System.out.print("Database name: ");
+ String dbName = in.readLine();
+
+ System.out.print("Give name of DB user that can create tables: ");
+ String dbUser = in.readLine();
+
+ System.out.print("Password: ");
+ String dbPassword = in.readLine();
+
+ String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
+ createTables(tmpl, url, dbUser, dbPassword);
+
+ // TODO: Create config file
+
+ } else if(args.length == 4) {
+ String tmplName = args[0];
+ String url = args[1];
+ String dbUser = args[2];
+ String dbPassword = args[3];
+
+ String tmpl = Resource.getAsString(tmplName, true);
+ createTables(tmpl, url, dbUser, dbPassword);
+ } else {
+ System.out.println("Wrong number of parameters!");
}
- // Load JDBC Driver class
- Class.forName(driverMap.get(dbmsType));
+ System.out.println("Ok");
+ }
- String tmpl = Resource.getAsString(tmplName, true);
-
- System.out.print("Database server hostname (e.g. localhost): ");
- String dbHostname = in.readLine();
-
- System.out.print("Database name: ");
- String dbName = in.readLine();
-
- System.out.print("Give name of DB user that can create tables: ");
- String dbUser = in.readLine();
-
- System.out.print("Password: ");
- String dbPassword = in.readLine();
-
- String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
-
+ public static void createTables(String tmpl, String url, String dbUser, String dbPassword)
+ throws SQLException {
Connection conn =
- DriverManager.getConnection(url, dbUser, dbPassword);
- conn.setAutoCommit(false);
+ DriverManager.getConnection(url, dbUser, dbPassword);
String[] tmplChunks = tmpl.split(";");
@@ -107,10 +127,16 @@
}
conn.commit();
- conn.setAutoCommit(true);
+ conn.close();
+ }
- // Create config file
-
- System.out.println("Ok");
+ public static void loadJDBCDriver() {
+ for(String className : driverMap.values()) {
+ try {
+ Class.forName(className);
+ } catch (ClassNotFoundException ex) {
+ System.out.println("Could not load JDBC driver: " + className);
+ }
+ }
}
}