Class DbServer

java.lang.Object
  |
  +--DbServer
Direct Known Subclasses:
PersonRegister

public class DbServer
extends java.lang.Object

Class DbServer is a class containing all initation code needed to create and start a single mckoi databse service as part of your process, operating directly towards the file system. This class is created for usage by students at LIU while attending a Java and Algorithm course - it is not designed for storing or protecting vital data.

This class will automatically create a directory called .mckoidb in the home directory of the current user, and in there it will create a configuration file and a data directory for the database.

For each register (or table) you plan to store in this database, you will have to extend this class. The class SamplePersonDb is provided as an example of how to automatically create the tables needed, how to decide if data should be inserted or updated in the table, etc etc.

The default name of the database is "DemoDB", which can be changed by assigning a new name to the databaseName property of DbServer before the first call to getConnection().

This implementation will allways use the same dummy password when connecting to a database - meaning, it does not provide any protection of your data. However, another user will normally not have write privileges onto your files, and can thus not modify your data. If you change this (granting another user write privileges), make shure you do not start two programs that access the same database simultaneously. For such project a Mckoi server must be used.

Version:
2005-01-28
Author:
Peter Loborg

Field Summary
static java.lang.String databaseName
          The name of the database to be created or connected to.
 
Constructor Summary
DbServer()
           
 
Method Summary
static boolean containsTable(java.lang.String tabName)
          Check if table tabName exists by asking the database.
(package private) static java.lang.String getConfigFileUrl(java.lang.String dbName)
          Create a configuration file and home directory for a database unless it already exists.
static java.sql.Connection getConnection()
          Obtain a Connection object to the databse, creating the databse if needed.
static void showTables()
          Prints information about all tables in the database to stdout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

databaseName

public static java.lang.String databaseName
The name of the database to be created or connected to. Must be changed before the first call to getConnection() in order to have effect (the name used is then copied to an internal location).

Constructor Detail

DbServer

public DbServer()
Method Detail

containsTable

public static boolean containsTable(java.lang.String tabName)
                             throws java.sql.SQLException
Check if table tabName exists by asking the database.

java.sql.SQLException

getConfigFileUrl

static java.lang.String getConfigFileUrl(java.lang.String dbName)
Create a configuration file and home directory for a database unless it already exists.

Using this implementation, the configuration file and the corresponding data files are allways created in a directory called .mckoidbs in the current users home directory.

For each database there are three parts created in that common directory:

Warning: If a configuration file is removed this code will re-create that file but may then fail to clear the data directory. Thus, when removing a database, allways remove all three entities (the config file and the corresponding _data and _log directories)

For more details about the cofiguration file, read the in-line comments of the source code for this method, the sample cofiguration file in the Mckoi simple demo directory or the Mckoi documentation.

Throws:
java.lang.RuntimeException - if there is no configuration file but there seems to be a data directory with the same dbName and there is a failure while cleaning up that directory, or if the existing config file can not be read.

getConnection

public static java.sql.Connection getConnection()
Obtain a Connection object to the databse, creating the databse if needed. This method stores a successfully obtained connection internally, and subsequent calls will return the same connection object.

The actual creation of the database is performed by getConfigFileUrl().


showTables

public static void showTables()
                       throws java.sql.SQLException
Prints information about all tables in the database to stdout.

java.sql.SQLException