javatools.database
Class MySQLDatabase

java.lang.Object
  extended by javatools.database.Database
      extended by javatools.database.MySQLDatabase

public class MySQLDatabase
extends Database

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). The class MySQLDatabase implements the Database-interface for a MySQL data base. Make sure that the file "mysql-connector-java-version-bin.jar" from the "MySQL Connector/J" (see the MySQL-website) is in the classpath. When using Eclipse, add the file via Project ->Properties ->JavaBuildPath ->Libraries ->ExternalJARFile.
Example:

     Database d=new MySQLDatabase("user","password","database");     
     d.queryColumn("SELECT foodname FROM food WHERE origin=\"Italy\"")
     -> [ "Pizza Romana", "Spaghetti alla Bolognese", "Saltimbocca"]
     Database.describe(d.query("SELECT * FROM food WHERE origin=\"Italy\"")
     -> foodname |origin  |calories |
        ------------------------------
        Pizza Rom|Italy   |10000    |
        Spaghetti|Italy   |8000     |
        Saltimboc|Italy   |8000     |        


Nested Class Summary
static class MySQLDatabase.ANSIvarcharBin
          a VARCHAR BINARY type, making sure we are case-sensitive in varchar fields (currently we assume case-sensitive collation is used by default, however, the method 'enforceCaseSensitivity' can replace the normal VARCHAR representation by this one, so if the database collation is not case-sensitive case-sensitive applications can use this sqltype on demand)
static class MySQLDatabase.Blob
          BLOB/TEXT type for MySQL
static class MySQLDatabase.MysqlVarchar
          VARCHAR type for MySQL
 
Nested classes/interfaces inherited from class javatools.database.Database
Database.CommitTransactionSQLException, Database.ConnectionBrokenSQLException, Database.InitTransactionSQLException, Database.Inserter, Database.RollbackTransactionSQLException, Database.StartAutoCommitSQLException, Database.TransactionSQLException
 
Field Summary
static MySQLDatabase.ANSIvarcharBin ansivarcharbin
           
static MySQLDatabase.Blob blob
           
static MySQLDatabase.MysqlVarchar mysqlvarchar
           
 
Fields inherited from class javatools.database.Database
java2SQL, MINCOLUMNWIDTH, SCREENWIDTH, type2SQL
 
Constructor Summary
MySQLDatabase()
           
MySQLDatabase(java.lang.String user, java.lang.String password, java.lang.String database, java.lang.String host, java.lang.String port)
          Constructs a new MySQLDatabase from a user and a password, all other arguments may be null
 
Method Summary
 java.lang.String autoincrementColumn()
          Produces an SQL fragment representing column properties for an autoincrementing integer column s.t.
 java.lang.String cast(java.lang.String value, java.lang.String type)
          Produces an SQL fragment casting the given value to the given type *
 void connect()
          connects to the database specified
 void enforceCaseSensitivity(boolean enable)
          allows to make sure VARCHAR column field based queries are case-sensitive even when the underlying table has a case-insensitive collation
 java.lang.String getSQLStmntIFNULL(java.lang.String a, java.lang.String b)
          returns the database system specific expression for ifnull functionality i.e.
 boolean jarAvailable()
          TRUE if the required JAR is there
 void lockTableReadAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
          Locks a table in read mode, i.e.
 void lockTableWriteAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
          Locks a table in write mode, i.e.
static void main(java.lang.String[] args)
           
 void releaseLocksAndEndTransaction()
          releases all locks the connection holds, commits the current transaction and ends it switches back to autocommit mode
 
Methods inherited from class javatools.database.Database
close, close, close, close, connected, createIndex, createIndexCommand, createIndices, createPrimaryKey, createTable, createView, describe, describe, dumpCSV, dumpQueryAsCSV, endTransaction, endTransaction, executeUpdate, exists, existsTable, finalize, flush, format, formatNullToNull, getConnection, getFetchsize, getResultSetConcurrency, getResultSetType, getSQLType, getSQLType, getSQLType, getValidityCheckTimeout, indexName, isAutoReconnectingOnSelect, isAutoReconnectingOnUpdate, limit, loadCSV, makeCSV, makeCSVForQuery, newInserter, newInserter, newInserter, offset, query, query, query, query, queryValue, reconnect, resetTransaction, runInterface, setAutoReconnectOnSelect, setAutoReconnectOnUpdate, setFetchsize, setResultSetConcurrency, setResultSetType, setValidityCheckTimeout, startTransaction, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mysqlvarchar

public static MySQLDatabase.MysqlVarchar mysqlvarchar

blob

public static MySQLDatabase.Blob blob

ansivarcharbin

public static MySQLDatabase.ANSIvarcharBin ansivarcharbin
Constructor Detail

MySQLDatabase

public MySQLDatabase(java.lang.String user,
                     java.lang.String password,
                     java.lang.String database,
                     java.lang.String host,
                     java.lang.String port)
              throws java.lang.InstantiationException,
                     java.lang.IllegalAccessException,
                     java.lang.ClassNotFoundException,
                     java.sql.SQLException
Constructs a new MySQLDatabase from a user and a password, all other arguments may be null

Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.sql.SQLException

MySQLDatabase

public MySQLDatabase()
Method Detail

enforceCaseSensitivity

public void enforceCaseSensitivity(boolean enable)
allows to make sure VARCHAR column field based queries are case-sensitive even when the underlying table has a case-insensitive collation


connect

public void connect()
             throws java.sql.SQLException
connects to the database specified

Specified by:
connect in class Database
Throws:
java.sql.SQLException

jarAvailable

public boolean jarAvailable()
Description copied from class: Database
TRUE if the required JAR is there

Overrides:
jarAvailable in class Database

lockTableWriteAccess

public void lockTableWriteAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
                          throws java.sql.SQLException
Locks a table in write mode, i.e. other db connections can only read the table, but not write to it Be careful as to not run into deadlocks! Especially do not try to lock tables independently in separate steps, lock all tables needed for some processing in one call

Overrides:
lockTableWriteAccess in class Database
Throws:
java.sql.SQLException

lockTableReadAccess

public void lockTableReadAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
                         throws java.sql.SQLException
Locks a table in read mode, i.e. only this connection can read or write the table Be careful as to not run into deadlocks! Especially do not try to lock tables independently in separate steps, lock all tables needed for some processing in one call

Overrides:
lockTableReadAccess in class Database
Throws:
java.sql.SQLException

releaseLocksAndEndTransaction

public void releaseLocksAndEndTransaction()
                                   throws java.sql.SQLException
releases all locks the connection holds, commits the current transaction and ends it switches back to autocommit mode

Overrides:
releaseLocksAndEndTransaction in class Database
Throws:
java.sql.SQLException

getSQLStmntIFNULL

public java.lang.String getSQLStmntIFNULL(java.lang.String a,
                                          java.lang.String b)
returns the database system specific expression for ifnull functionality i.e. ifnull(a,b) returns b if a is null and a otherwise

Overrides:
getSQLStmntIFNULL in class Database

cast

public java.lang.String cast(java.lang.String value,
                             java.lang.String type)
Produces an SQL fragment casting the given value to the given type *

Overrides:
cast in class Database

autoincrementColumn

public java.lang.String autoincrementColumn()
Produces an SQL fragment representing column properties for an autoincrementing integer column s.t. if used during table creation a column can declared to get by default an integer value assigned according to an internal self-incrementing sequence counter Example: createTable("tableWithSingleAutoIncrementingIDColumn", "ID", autoincrementColumn())

Overrides:
autoincrementColumn in class Database

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception