FileDocCategorySizeDatePackage
TextToJDBC.javaAPI DocExample2467Wed Jun 16 21:54:58 BST 2004None

TextToJDBC

public class TextToJDBC extends Object
Convert the database from text form to JDBC form.
version
$Id: TextToJDBC.java,v 1.1 2004/06/17 01:54:58 ian Exp $

Fields Summary
protected static final String
TEXT_NAME
protected static final String
DB_URL
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] fn)

	
	      
			  
		
		BufferedReader is = new BufferedReader(new FileReader(TEXT_NAME));
		
		System.out.println("Setting UP JDBC Connection");
		// Load the database driver
		Class.forName("jdbc.idbDriver");

		Connection conn = DriverManager.getConnection(DB_URL, "ian", "secritt");

		Statement stmt = conn.createStatement();
		System.out.println("Creating table and index");
		stmt.executeUpdate("DROP TABLE userdb");
		stmt.executeUpdate("CREATE TABLE userdb (\n" +
			"name     char(12) PRIMARY KEY,\n" +
			"password char(20),\n" +
			"fullName char(30),\n" +
			"email    char(60),\n" +
			"city     char(20),\n" +
			"prov     char(20),\n" +
			"country  char(20),\n" +
			"privs    int\n" +
			")");
		stmt.executeUpdate("CREATE INDEX nickIndex ON userdb (name)");
		stmt.close();
		// put the data in the table
		PreparedStatement ps = conn
				.prepareStatement("INSERT INTO userdb VALUES (?,?,?,?,?,?,?,?)");
		String line;
		while ((line = is.readLine()) != null) {
			//name:password:fullname:City:Prov:Country:privs
			if (line.startsWith("#")) { // comment
				continue;
			}
			StringTokenizer st = new StringTokenizer(line, ":");
			String nick = st.nextToken();
			String pass = st.nextToken();
			String full = st.nextToken();
			String email = st.nextToken();
			String city = st.nextToken();
			String prov = st.nextToken();
			String ctry = st.nextToken();
			// User u = new User(nick, pass, full, email,
			// city, prov, ctry);
			String privs = st.nextToken();
			int iprivs = 0;
			if (privs.indexOf("A") != -1) {
				iprivs |= User.P_ADMIN;
			}
			if (privs.indexOf("E") != -1) {
				iprivs |= User.P_EDIT;
			}
			ps.setString(1, nick);
			ps.setString(2, pass);
			ps.setString(3, full);
			ps.setString(4, email);
			ps.setString(5, city);
			ps.setString(6, prov);
			ps.setString(7, ctry);
			ps.setInt(8, iprivs);
			ps.executeUpdate();
		}
		ps.close(); // All done with that statement
		conn.close(); // All done with that DB connection
		return; // All done with this program.