diff options
author | Simon Rettberg | 2014-03-28 17:51:24 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-03-28 17:51:24 +0100 |
commit | fbbfee0a32ce83f5bfe36d78eddafed7226a041c (patch) | |
tree | f3d44e8b9e2bca678165cb3dd97ec1aa1380f244 /src/main/java/org/openslx/imagemaster/db/MySQL.java | |
download | masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.tar.gz masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.tar.xz masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.zip |
Initial Commit
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db/MySQL.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/MySQL.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/MySQL.java b/src/main/java/org/openslx/imagemaster/db/MySQL.java new file mode 100644 index 0000000..15bf5e2 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/db/MySQL.java @@ -0,0 +1,80 @@ +package org.openslx.imagemaster.db; + +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Properties; +import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; + +import org.apache.log4j.Logger; +import org.openslx.imagemaster.util.Util; + +import fi.evident.dalesbred.Database; + +/** + * Class for talking to the DB via the dalesbred jdbc wrapper. Package private, + * so only the Db* classes can actually communicate with the database. + * + */ +class MySQL +{ + private static final Logger log = Logger.getLogger( MySQL.class ); + private static Database db = null; + + static + { + // Load connection info from class (TODO: Make pretty) + Properties properties = new Properties(); + try { + final BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/mysql.properties" ) ); + properties.load( stream ); + stream.close(); + } catch ( FileNotFoundException e ) { + log.fatal( "config/mysql.properties not found!" ); + System.exit( 1 ); + } catch ( IOException e ) { + log.fatal( "Error reading from config/mysql.properties: " + e.getMessage() ); + System.exit( 1 ); + } catch ( Exception e ) { + log.fatal( "Generic error loading mysql properties file." ); + e.printStackTrace(); + System.exit( 1 ); + } + final String host = properties.getProperty( "host" ); + final String dbname = properties.getProperty( "db" ); + final String user = properties.getProperty( "user" ); + final String password = properties.getProperty( "password" ); + + Util.notNullFatal( host, "host not set in mysql properties" ); + Util.notNullFatal( dbname, "db not set in mysql properties" ); + Util.notNullFatal( user, "user not set in mysql properties" ); + Util.notNullFatal( password, "password not set in mysql properties" ); + + // Setup db connection + try { + MysqlDataSource ds = new MysqlDataSource(); + ds.setServerName( host ); + ds.setDatabaseName( dbname ); + ds.setUser( user ); + ds.setPassword( password ); + db = Database.forDataSource( ds ); + } catch ( Exception e ) { + log.fatal( "Error initializing mysql data source!" ); + e.printStackTrace(); + System.exit( 1 ); + } + } + + protected static <T> List<T> findAll( final Class<T> clazz, final String sql, final Object... args ) + { + return db.findAll( clazz, sql, args ); + } + + protected static <T> T findUniqueOrNull( final Class<T> clazz, final String sql, final Object... args ) + { + return db.findUniqueOrNull( clazz, sql, args ); + } + +} |