Class DBUtil

java.lang.Object
com.jalios.util.DBUtil

public class DBUtil extends Object
This class provides a set of static methods which perform various utility operations on databases.

Warning : When working with SQL query, this class does not check tables, fields, columns and where clause parameter against SQL injection. Make sure to only use validated parameter values, i.e. do not use values entered by end user, but only values coming from configuration files or authorized administrators.

Since:
jcms-5.5.0
Author:
Olivier Dedieu
  • Constructor Details

    • DBUtil

      public DBUtil()
  • Method Details

    • getDataSourceMap

      public static Map<String,DataSource> getDataSourceMap()
      Lookup for all "java:com/env/jdbc/*" data sources declared in the AppServer. Returns a map of name / DataSource. For instance if two data sources are declared in the AppServer as "jdbc/db1" and "jdbc/db2", this method returns a map wich contains 2 entries : ("jdbc/db1", DataSource for db1) and ("jdbc/db2", DataSource for db2).
      Returns:
      a map of name / DataSource
      Since:
      jcms-5.5.0
      See Also:
    • initDataSourceMap

      public static void initDataSourceMap(Set<String> dataSourceSet)
      This method must be called before any other methods accessing to the dataSourceMap.
      Parameters:
      dataSourceSet - the name of data sources to initialize
      Since:
      jcms-10.0.7
    • getDataSource

      public static DataSource getDataSource(String dsName)
      Lookup for the given data source.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      Returns:
      the DataSource bound to the given name
      Since:
      jcms-5.5.0
      See Also:
    • getDataSource

      public static DataSource getDataSource(String dsName, boolean searchOutInitDS)
      Lookup for the given data source.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      searchOutInitDS - if true search outside the initialized Data Sources
      Returns:
      the DataSource bound to the given name
      Since:
      jcms-10.0.7
      See Also:
    • getConnection

      public static Connection getConnection(String dsName) throws SQLException
      Attempts to establish a connection with a given data source.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      Returns:
      a connection to the data source
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-5.5.0
    • getConnection

      public static Connection getConnection(String dsName, String username, String password) throws SQLException
      Attempts to establish a connection with a given data source.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      username - the database user on whose behalf the connection is being made
      password - the user's password
      Returns:
      a connection to the data source
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-5.5.0
    • getConnection

      public static Connection getConnection(String dsName, String username, String password, boolean searchOutInitDS) throws SQLException
      Attempts to establish a connection with a given data source.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      username - the database user on whose behalf the connection is being made
      password - the user's password
      searchOutInitDS - if true search outside the initialized Data Sources
      Returns:
      a connection to the data source
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-10.0.7
    • buildSelectQuery

      public static String buildSelectQuery(String dsName, String select, String from, String orderby, String[] whereParams, String[] whereValues, boolean and) throws SQLException
      This convenient method build an SQL Select query from the given parameters. It will use "'" parameter for String value and none for numerical values.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      select - the table filed to select or *
      from - the table name to work with.
      orderby - the order by parameter
      whereParams - an array of parameters
      whereValues - an array of parameters value
      and - should be 'AND' or 'OR' between parameters
      Returns:
      String a datasource compatible SQL Query
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-5.5.0
    • getColumnsNumericsMetaData

      public static boolean[] getColumnsNumericsMetaData(String dsName, String tables, String[] columns) throws SQLException
      This method do a small request to the given database to extract metadata of given columns. It return an array of boolean with value true if the column is "numeric" otherwise false.

      "numeric" means Types.BIGINT, Types.INTEGER, Types.SMALLINT, Types.NUMERIC

      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      tables - the table names
      columns - the column names
      Returns:
      boolean[] an array of boolean
      Throws:
      SQLException - if an error occurs
    • getColumnsMetaData

      public static ResultSetMetaData getColumnsMetaData(String dsName, String tables, String[] columns) throws SQLException
      Extract the metadata of the given columns (or all columns if the parameter is null) for the given tables.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      tables - the table names
      columns - the column names. If null, all columns of the given tables
      Returns:
      ResultSetMetaData with information about columns
      Throws:
      SQLException - if an error occurs
    • getFirstValue

      public static Object getFirstValue(String dsName, String select, String from, String orderby, String[] whereParams, String[] whereValues, boolean and) throws SQLException
      This convenient method performs a query then it returns the value of the first column of the first row of the result set.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      select - the table filed to select or *
      from - the table name to work with.
      orderby - the order by parameter
      whereParams - an array of parameters
      whereValues - an array of parameters value
      and - should be 'AND' or 'OR' between parameters
      Returns:
      the first value or null if no result
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-5.5.0
    • getFirstValue

      public static Object getFirstValue(String dsName, String sqlQuery) throws SQLException
      This convenient method performs a query then it returns the value of the first column of the first row of the result set.
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      sqlQuery - the SQL query to be performed
      Returns:
      the first value or null if no result
      Throws:
      SQLException - if an error occurs
      Since:
      jcms-5.5.0
    • getFirstValue

      public static Object getFirstValue(Connection conn, String sqlQuery)
      This convenient method performs a query then it returns the value of the first column of the first row of the result set. *
      Parameters:
      conn - the JDBC connection
      sqlQuery - the SQL query to be performed
      Returns:
      the first value or null if no result
      Since:
      jcms-5.5.0
    • executeQuery

      public static SQLQueryResult executeQuery(String dsName, String sqlQuery, int maxRows)
      This convient method performs a query and return a SQLQueryResult
      Parameters:
      dsName - the name of the data source (eg. "jdbc/TestDB")
      sqlQuery - the SQL (SELECT) query
      maxRows - the max rows limit; zero means there is no limit
      Returns:
      a SQLQueryResult
      Since:
      jcms-5.5.0
    • executeUpdate

      public static void executeUpdate(String dsName, String sql)
      This convenient method executes an update (insert/update/delete) in the given dataSource.
      Parameters:
      dsName - the data source
      sql - update query
      Since:
      jcms-6.0.0
    • checkDataSource

      public static boolean checkDataSource(String dsName)
      Returns true if the given dataSource is available (i.e. a connection can be open on this dataSource).
      Parameters:
      dsName - the data source name.
      Returns:
      true if the given dataSource is available.
      Since:
      jcms-6.0.0
    • checkDataSource

      public static boolean checkDataSource(String dsName, boolean searchOutInitDS)
      Returns true if the given dataSource is available (i.e. a connection can be open on this dataSource).
      Parameters:
      dsName - the data source name.
      searchOutInitDS - if true search outside the initialized Data Sources
      Returns:
      true if the given dataSource is available.
      Since:
      jcms-10.0.7
    • addDataSource

      public static void addDataSource(String dsName, DataSource dataSource)
      Adds the given data source.
      Parameters:
      dsName - the name of the data source.
      dataSource - the data source.
      Since:
      jcms-6.0.0
    • getDatabaseInfo

      public static DatabaseInfo getDatabaseInfo(String dsName)
      Returns the Database info for the given data source.
      Parameters:
      dsName - the data source.
      Returns:
      the Database info for the given data source.
      Since:
      jcms-10.0.2
    • getDatabaseInfo

      public static DatabaseInfo getDatabaseInfo(String dsName, boolean searchOutInitDS)
      Returns the Database info for the given data source.
      Parameters:
      dsName - the data source.
      searchOutInitDS - if true search outside the initialized Data Sources
      Returns:
      the Database info for the given data source.
      Since:
      jcms-10.0.7
    • getDatabaseInfo

      public static DatabaseInfo getDatabaseInfo(String url, String user, String password)
      Returns the Database info for the given JDBC parameters.
      Parameters:
      url - the JDBC url
      user - the JDBC user
      password - the JDBC password
      Returns:
      the Database info for the given JDBC parameters.
      Since:
      jcms-10.0.2
    • resetC3p0Pools

      public static void resetC3p0Pools()