Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/CollectionManager.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/CollectionManager.java	(revision 8744)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/CollectionManager.java	(revision 8745)
@@ -175,5 +175,5 @@
     this.qualifiedCollectionName = site+"_"+collection;
 
-    this.database = GS3SQLConnectionFactory.createConnection(this.qualifiedCollectionName);
+    this.database = GS3SQLConnectionFactory.getGS3SQLConnection(this.qualifiedCollectionName);
     /*    if (this.database != null) {
       this.database.clearCollection(collection);
@@ -182,5 +182,5 @@
     */
     if (this.database == null) {
-      this.database = GS3SQLConnectionFactory.createConnection("test");
+      this.database = GS3SQLConnectionFactory.getGS3SQLConnection("test");
       this.database.initCollection(this.qualifiedCollectionName);
     }
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/GSDataDump.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/GSDataDump.java	(revision 8744)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/GSDataDump.java	(revision 8745)
@@ -14,5 +14,5 @@
     String database = args[0];
 
-    GS3SQLConnection connection = GS3SQLConnectionFactory.createConnection(database);
+    GS3SQLConnection connection = GS3SQLConnectionFactory.getGS3SQLConnection(database);
     DocumentList docList = DocumentList.readSQLDocuments(connection);
     docList.writeDocuments(new File(database));
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnection.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnection.java	(revision 8744)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnection.java	(revision 8745)
@@ -10,150 +10,19 @@
 import java.sql.ResultSet;
 
-public class GS3SQLConnection
+
+public class GS3SQLConnection extends SQLConnection
 {
-  private Connection connection;
-  private Statement  statement;
-
-  class GS3SQLCreateTable
-  { String tableName;
-    List   properties;
-    List   primaryKey;
-    List   indexed;
-
-    public GS3SQLCreateTable(String tableName)
-    { this.tableName = tableName;
-      this.properties = new ArrayList();
-      this.primaryKey = new ArrayList();
-      this.indexed    = new ArrayList();
-    }
-
-    public void addAutoPrimaryKey(String fieldName)
-    {
-      GS3SQLField field = new GS3SQLField(fieldName, GS3SQLField.AUTOINTEGER_TYPE);
-      this.properties.add(field);
-      this.setPrimaryKey(fieldName);
-    }
-
-    public void addProperty(String fieldname)
-    { GS3SQLField field = new GS3SQLField(fieldname);
-      this.properties.add(field);
-    }
-    
-    public void addProperty(String fieldname, String type)
-    { GS3SQLField field = new GS3SQLField(fieldname, type);
-      this.properties.add(field);
-    }
-
-    public void addProperty(String fieldname, int length)
-    { GS3SQLField field = new GS3SQLField(fieldname, length);
-      this.properties.add(field);
-    }
-
-    public void setPrimaryKey(String fieldname)
-    { this.primaryKey.clear();
-      this.primaryKey.add(fieldname);
-    }
-
-    public void extendPrimaryKey(String fieldname)
-    { this.primaryKey.add(fieldname);
-    }
-    
-    public void addIndex(String fieldname)
-    { this.indexed.add(fieldname);
-    }
-
-    public String toString()
-    {	
-      StringBuffer reply = new StringBuffer();
-
-      reply.append("CREATE TABLE ");
-      reply.append(this.tableName);
-      reply.append(" (");
-      for (int f = 0; f < this.properties.size(); f ++)
-      { GS3SQLField field = (GS3SQLField) this.properties.get(f);
-	if (f != 0)
-	{ reply.append(",");
-	}
-	reply.append(field.toString());
-      }
-
-      for (int p = 0; p < this.primaryKey.size(); p ++)
-      { if (p == 0)
-	{ reply.append(", PRIMARY KEY(");
-	}
-        else
-	{ reply.append(",");
-	}
-        reply.append(this.primaryKey.get(p).toString());
-
-	if (p == this.primaryKey.size() - 1)
-	{ reply.append(")");
-	}
-      }
-      // TODO: add primary key, index etc. notations
-      reply.append(");");
-      
-      return reply.toString();
-    }
-  }
-
-  public GS3SQLConnection(java.sql.Connection connection)
-  { this.connection = connection;
-  }
-
-  public boolean execute(String sql)
-  {
-    try {
-      this.statement = this.connection.createStatement();
-      this.statement.execute(sql);
-    }
-    catch (SQLException ex) {
-	System.out.println(ex);
-      return false;
-    }
-    return true;
-  }
-  
-  public Statement createStatement()
-  { try {
-      return this.connection.createStatement();
-    }
-    catch (SQLException ex)
-    {
-      return null;
-    }
-  }
-
-  public Statement getStatement()
-  { return this.statement;
-  }
-
-  public ResultSet getResultSet()
-  { try {
-      return this.statement.getResultSet();
-    }
-    catch (SQLException ex)
-    {
-      return null;
-    }
-  }
+
+    public GS3SQLConnection(java.sql.Connection connection)
+    { 
+	super(connection);
+    }
+    
 
   public void clearCollection(String collectionName)
   {
-    // strip the old database
-    String killCommand = "DROP DATABASE " + collectionName;
-
-    try {
-      if (/*makeClean*/true) {
-	this.statement = this.connection.createStatement();
-	this.statement.execute(killCommand);
-	System.out.println("attempting: " + killCommand);
-      }
-    }
-    catch (SQLException sqlEx) {
-      System.out.println(sqlEx);
-    }
+      dropDatabase(collectionName);
   }
-
+    
 
   /**
@@ -177,17 +46,18 @@
     // if there is an error, assume that the database doesn't exist...
     // and reconnect to a "test" database for now...
-    this.connection = GS3SQLConnectionFactory.reconnect("test");
+    this.connection = GS3SQLConnectionFactory.getConnection("test");
     */
 
-    //   create database
-    String command = "CREATE DATABASE " + collectionName;
-
+      // create the new db
+      if (!createDatabase(collectionName)) {
+	  return false;
+      }
+      // now connect to it
+      if (connectToDatabase(collectionName)) {
+	  return false;
+      }
+    
+      
     try {
-      this.statement = this.connection.createStatement();
-      this.statement.execute(command);
-
-      // reconnect with the new database
-      this.connection = GS3SQLConnectionFactory.reconnect(collectionName);
-
       statement = this.connection.createStatement();
 
@@ -365,14 +235,4 @@
   }
 
-  public void deleteCollection(String collection)
-  {
-    try {
-      statement = this.connection.createStatement();
-      statement.execute("DROP DATABASE "+collection+";");
-    }
-    catch (SQLException ex)
-    {
-    }
-  }
 }
 
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnectionFactory.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnectionFactory.java	(revision 8744)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnectionFactory.java	(revision 8745)
@@ -5,7 +5,8 @@
 import java.sql.SQLException;
 
+
 public class GS3SQLConnectionFactory
 {
-    public static Connection reconnect(String database)
+    public static Connection getConnection(String database)
     {
 	try {
@@ -25,29 +26,44 @@
     }
     
-    public static GS3SQLConnection /*GS3SQLConnection*/ createConnection(String database)
-    {	
-	try
-	    {	Class.forName("com.mysql.jdbc.Driver").newInstance();
-	     
-	        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/"+database+"?user=gsdl3admin"/*&password=greenstone"*/);
-		return new GS3SQLConnection(connection);
-	    }
-	catch (SQLException sqlEx)
-	    {	System.out.println(sqlEx.toString());
-	    }
-	catch (Exception ex)
-	    {	System.out.println(ex.toString());
-		return null;
-	    }
-	return null;
+    public static SQLConnection getSQLConnection(String database)
+    {
+	Connection c = getConnection(database);
+	if (c== null) {
+	    return null;
+	}
+	return new SQLConnection(c);
     }
 
-    public static GS3SQLConnection createConnection()
-    { return createConnection("test");
+    
+    public static GS3SQLConnection getGS3SQLConnection(String database)
+    {
+	Connection c = getConnection(database);
+	if (c== null) {
+	    return null;
+	}
+	return new GS3SQLConnection(c);
+    }
+
+    /* returns a Connection to the server, */
+    public static Connection getConnection() 
+    {
+	return getConnection("test");
+    }
+
+    /* returns a Connection to the server, */
+    public static SQLConnection getSQLConnection() 
+    {
+	return getSQLConnection("test");
+    }
+
+    public static GS3SQLConnection getGS3SQLConnection()
+    { 
+	return getGS3SQLConnection("test");
     }
 
     public static void main(String args[])
-    { GS3SQLConnection connection = createConnection();
-    connection.initCollection("maya");
+    { 
+	GS3SQLConnection connection = getGS3SQLConnection();
+	connection.initCollection("maya");
     }
 }
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/SQLConnection.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/SQLConnection.java	(revision 8745)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/SQLConnection.java	(revision 8745)
@@ -0,0 +1,102 @@
+package org.greenstone.gsdl3.gs3build.database;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.ResultSet;
+
+public class SQLConnection
+{
+    protected Connection connection;
+    protected Statement  statement;
+
+
+    public SQLConnection(java.sql.Connection connection)
+    { 
+	this.connection = connection;
+    }
+    
+    protected void finalize() {
+	if (this.connection!=null) {
+	    try {
+		this.connection.close();
+	    } catch (Exception e) {}
+	    
+	    this.connection = null;
+	}
+    }
+    
+    public boolean execute(String sql)
+    {
+	try {
+	    this.statement = this.connection.createStatement();
+	    this.statement.execute(sql);
+	}
+	catch (SQLException ex) {
+	    System.out.println(ex);
+	    return false;
+	}
+	return true;
+    }
+    
+    public Statement createStatement()
+    { 
+	try {
+	    return this.connection.createStatement();
+	}
+	catch (SQLException ex) {
+	    return null;
+	}
+    }
+    
+    public Statement getStatement()
+    { 
+	return this.statement;
+    }
+    
+    public ResultSet getResultSet()
+    { 
+	try {
+	    return this.statement.getResultSet();
+	}
+	catch (SQLException ex) {
+	    return null;
+	}
+    }
+    
+    public boolean connectToDatabase(String database) {
+	this.connection = GS3SQLConnectionFactory.getConnection(database);
+	if (this.connection == null) {
+	    return false;
+	}
+	return true;
+    }
+    
+    public boolean dropDatabase(String database) {
+	try {
+	    this.statement = this.connection.createStatement();
+	    this.statement.execute("DROP DATABASE "+database+";");
+	}
+	catch (SQLException ex){
+	    System.err.println(ex);
+	    return false;
+	}
+	return true;
+	
+    }
+
+    public boolean createDatabase(String database) {
+	try {
+	    String command = "CREATE DATABASE " + database;
+	    this.statement = this.connection.createStatement();
+	    this.statement.execute(command);
+	} catch (Exception e) {
+	    System.err.println(e);
+	    return false;
+	}
+	 
+	return true;
+    }
+
+}
