chemaxon.util
Class DatabaseTools

java.lang.Object
  extended by chemaxon.util.DatabaseTools

public class DatabaseTools
extends java.lang.Object

Tools that can be applied in the development of applications accessing databases.

Author:
Ferenc Csizmadia, Szilard Dorant

Constructor Summary
DatabaseTools()
           
 
Method Summary
static java.util.ArrayList getFieldNames(ConnectionHandler ch, java.lang.String tableName, java.util.ArrayList types)
          Returns field names in a table.
static int[] getFieldTypes(ConnectionHandler ch, java.lang.String tableName)
          Returns field types in a table.
static boolean isConnectionAlive(java.sql.Connection con)
          Checks if JDBC connection to a database is alive or not.
static boolean isIntType(int type)
          Determines if an SQL type is an integer
static boolean isRealType(int type)
          Determines if an SQL type is a non-integer number
static boolean isTextType(int type)
          Determines if an SQL type is a text type
static byte[] readBytes(java.sql.ResultSet rs, int idx)
          Reads bytes from a ResultSet.
static byte[] readBytes(java.sql.ResultSet rs, java.lang.String columnName)
          Reads bytes from a ResultSet.
static java.lang.String readString(java.sql.ResultSet rs, int idx)
          Reads a String from a ResultSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseTools

public DatabaseTools()
Method Detail

isConnectionAlive

public static boolean isConnectionAlive(java.sql.Connection con)
Checks if JDBC connection to a database is alive or not. Especially useful if a database server may time out JDBC connections that have not been used for a long time.


getFieldNames

public static java.util.ArrayList getFieldNames(ConnectionHandler ch,
                                                java.lang.String tableName,
                                                java.util.ArrayList types)
                                         throws java.sql.SQLException
Returns field names in a table.

Parameters:
ch - ConnectionHandler opened to the database.
tableName - the name of the database table.
types - ArrayList object to store type names in it.
Throws:
java.sql.SQLException

getFieldTypes

public static int[] getFieldTypes(ConnectionHandler ch,
                                  java.lang.String tableName)
                           throws java.sql.SQLException
Returns field types in a table.

Parameters:
ch - ConnectionHandler opened to the database.
tableName - the name of the database table.
Returns:
the field types in a table, in order
Throws:
java.sql.SQLException
Since:
JChem 2.0

isTextType

public static boolean isTextType(int type)
Determines if an SQL type is a text type

Parameters:
type - the SQL type
Returns:
true, if the specified type is a text type
Since:
JChem 2.0

isIntType

public static boolean isIntType(int type)
Determines if an SQL type is an integer

Parameters:
type - the SQL type
Returns:
true, if the specified type is an integer type
Since:
JChem 2.0

isRealType

public static boolean isRealType(int type)
Determines if an SQL type is a non-integer number

Parameters:
type - the SQL type
Returns:
true, if the specified type is a non-integer number
Since:
JChem 2.0

readBytes

public static byte[] readBytes(java.sql.ResultSet rs,
                               int idx)
                        throws java.sql.SQLException
Reads bytes from a ResultSet. The strucure field can be LONGVARBINARY, BLOB or CLOB, VARCHAR(2)

Parameters:
rs - the ResultSet
idx - the index of the field (first column is 1)
Returns:
the data as a byte array. In case of CLOB "ASCII" encoding is used.
Throws:
java.sql.SQLException - in case of database error, or invalid column type

readBytes

public static byte[] readBytes(java.sql.ResultSet rs,
                               java.lang.String columnName)
                        throws java.sql.SQLException
Reads bytes from a ResultSet. The strucure field can be LONGVARBINARY, BLOB or CLOB, VARCHAR(2)

Parameters:
rs - the ResultSet
columnName - the name of the field
Returns:
the strucure as a byte array. In case of CLOB "ASCII" encoding is used.
Throws:
java.sql.SQLException - in case of database error, or invalid column type

readString

public static java.lang.String readString(java.sql.ResultSet rs,
                                          int idx)
                                   throws java.sql.SQLException
Reads a String from a ResultSet. The field type can be LONGVARBINARY, BLOB or CLOB, VARCHAR(2)

Parameters:
rs - the ResultSet
idx - the index of the field (first column is 1)
Returns:
the strucure as a String. In case of binary types "ASCII" encoding is used.
Throws:
java.sql.SQLException - in case of database error, or invalid column type
Since:
JChem 3.2