org.dbunit.util
Class SQLHelper

java.lang.Object
  extended by org.dbunit.util.SQLHelper

public class SQLHelper
extends java.lang.Object

Helper for SQL-related stuff.
TODO: testcases

Since:
Nov 5, 2005
Version:
$Revision: 769 $
Author:
Felipe Leme (dbunit@felipeal.net)

Method Summary
static void close(java.sql.ResultSet resultSet)
          Closes the given result set in a null-safe way
static void close(java.sql.ResultSet rs, java.sql.Statement stmt)
          Close a result set and a prepared statement, checking for null references.
static void close(java.sql.Statement stmt)
          Close a SQL statement, checking for null references.
static java.lang.String getDatabaseInfo(java.sql.DatabaseMetaData metaData)
          Returns the database and JDBC driver information as pretty formatted string
static java.lang.String getPrimaryKeyColumn(java.sql.Connection conn, java.lang.String table)
          Gets the primary column for a table.
static void printAllTables(java.sql.DatabaseMetaData metaData, java.io.PrintStream outputStream)
          Utility method for debugging to print all tables of the given metadata on the given stream
static void printDatabaseInfo(java.sql.DatabaseMetaData metaData, java.io.PrintStream outputStream)
          Prints the database and JDBC driver information to the given output stream
static boolean schemaExists(java.sql.Connection connection, java.lang.String schema)
          Returns true if the given schema exists for the given connection.
static boolean tableExists(java.sql.DatabaseMetaData metaData, java.lang.String schema, java.lang.String tableName)
          Checks if the given table exists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPrimaryKeyColumn

public static java.lang.String getPrimaryKeyColumn(java.sql.Connection conn,
                                                   java.lang.String table)
                                            throws java.sql.SQLException
Gets the primary column for a table.

Parameters:
conn - connection with the database
table - table name
Returns:
name of primary column for a table (assuming it's just 1 column).
Throws:
java.sql.SQLException - raised while getting the meta data

close

public static void close(java.sql.ResultSet rs,
                         java.sql.Statement stmt)
                  throws java.sql.SQLException
Close a result set and a prepared statement, checking for null references.

Parameters:
rs - result set to be closed
stmt - prepared statement to be closed
Throws:
java.sql.SQLException - exception raised in either close() method

close

public static void close(java.sql.Statement stmt)
                  throws java.sql.SQLException
Close a SQL statement, checking for null references.

Parameters:
stmt - statement to be closed
Throws:
java.sql.SQLException - exception raised while closing the statement

close

public static void close(java.sql.ResultSet resultSet)
                  throws java.sql.SQLException
Closes the given result set in a null-safe way

Parameters:
resultSet -
Throws:
java.sql.SQLException

schemaExists

public static boolean schemaExists(java.sql.Connection connection,
                                   java.lang.String schema)
                            throws java.sql.SQLException
Returns true if the given schema exists for the given connection.

Parameters:
connection - The connection to a database
schema - The schema to be searched
Returns:
Returns true if the given schema exists for the given connection.
Throws:
java.sql.SQLException
Since:
2.3.0

tableExists

public static boolean tableExists(java.sql.DatabaseMetaData metaData,
                                  java.lang.String schema,
                                  java.lang.String tableName)
                           throws java.sql.SQLException
Checks if the given table exists.

Parameters:
metaData - The database meta data
schema - The schema in which the table should be searched. If null the schema is not used to narrow the table name.
tableName - The table name to be searched
Returns:
Returns true if the given table exists in the given schema. Else returns false.
Throws:
java.sql.SQLException
Since:
2.3.0

printAllTables

public static void printAllTables(java.sql.DatabaseMetaData metaData,
                                  java.io.PrintStream outputStream)
                           throws java.sql.SQLException
Utility method for debugging to print all tables of the given metadata on the given stream

Parameters:
metaData -
outputStream -
Throws:
java.sql.SQLException

getDatabaseInfo

public static java.lang.String getDatabaseInfo(java.sql.DatabaseMetaData metaData)
                                        throws java.sql.SQLException
Returns the database and JDBC driver information as pretty formatted string

Parameters:
metaData - The JDBC database metadata needed to retrieve database information
Returns:
The database information as formatted string
Throws:
java.sql.SQLException

printDatabaseInfo

public static void printDatabaseInfo(java.sql.DatabaseMetaData metaData,
                                     java.io.PrintStream outputStream)
                              throws java.sql.SQLException
Prints the database and JDBC driver information to the given output stream

Parameters:
metaData - The JDBC database metadata needed to retrieve database information
outputStream - The stream to which the information is printed
Throws:
java.sql.SQLException


Copyright © 2002-2008. All Rights Reserved.