diff -r ed84c8bdd87b -r 73b21e9f3958 src/org/sonews/util/DatabaseSetup.java --- a/src/org/sonews/util/DatabaseSetup.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/util/DatabaseSetup.java Mon Aug 30 00:20:06 2010 +0200 @@ -25,7 +25,6 @@ import java.sql.Statement; import java.util.HashMap; import java.util.Map; -import org.sonews.config.Config; import org.sonews.util.io.Resource; /** @@ -33,95 +32,85 @@ * @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(); - private static final Map driverMap - = new HashMap(); - - static - { - templateMap.put("1", "helpers/database_mysql5_tmpl.sql"); - templateMap.put("2", "helpers/database_postgresql8_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"); - } - - 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!"); - - 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; - } - - // Load JDBC Driver class - Class.forName(driverMap.get(dbmsType)); - - 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(); + private static final Map templateMap = new HashMap(); + private static final Map urlMap = new HashMap(); + private static final Map driverMap = new HashMap(); - System.out.print("Give name of DB user that can create tables: "); - String dbUser = in.readLine(); + static { + templateMap.put("1", "helpers/database_mysql5_tmpl.sql"); + templateMap.put("2", "helpers/database_postgresql8_tmpl.sql"); - System.out.print("Password: "); - String dbPassword = in.readLine(); - - String url = urlMap.get(dbmsType) - .set("HOSTNAME", dbHostname) - .set("DB", dbName).toString(); - - Connection conn = - DriverManager.getConnection(url, dbUser, dbPassword); - conn.setAutoCommit(false); - - String[] tmplChunks = tmpl.split(";"); - - for(String chunk : tmplChunks) - { - if(chunk.trim().equals("")) - { - continue; - } - - Statement stmt = conn.createStatement(); - stmt.execute(chunk); - } - - conn.commit(); - conn.setAutoCommit(true); - - // Create config file - - System.out.println("Ok"); - } - + 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"); + } + + 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!"); + + 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; + } + + // Load JDBC Driver class + Class.forName(driverMap.get(dbmsType)); + + 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(); + + Connection conn = + DriverManager.getConnection(url, dbUser, dbPassword); + conn.setAutoCommit(false); + + String[] tmplChunks = tmpl.split(";"); + + for (String chunk : tmplChunks) { + if (chunk.trim().equals("")) { + continue; + } + + Statement stmt = conn.createStatement(); + stmt.execute(chunk); + } + + conn.commit(); + conn.setAutoCommit(true); + + // Create config file + + System.out.println("Ok"); + } }