public class DBUtil
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REVISION |
Constructor and Description |
---|
DBUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
addDataSource(java.lang.String name,
javax.sql.DataSource dataSource)
Adds the given data source.
|
static java.lang.String |
buildSelectQuery(java.lang.String dataSource,
java.lang.String select,
java.lang.String from,
java.lang.String orderby,
java.lang.String[] whereParams,
java.lang.String[] whereValues,
boolean and)
This convenient method build an SQL Select query from the given parameters.
|
static boolean |
checkDataSource(java.lang.String dataSource)
Returns true if the given dataSource is available (i.e.
|
static SQLQueryResult |
executeQuery(java.lang.String dataSource,
java.lang.String sqlQuery,
int maxRows)
This convient method performs a query and return a SQLQueryResult
|
static void |
executeUpdate(java.lang.String dataSource,
java.lang.String sql)
This convenient method executes an update (insert/update/delete) in the given dataSource.
|
static java.sql.ResultSetMetaData |
getColumnsMetaData(java.lang.String dataSource,
java.lang.String tables,
java.lang.String[] columns)
Extract the metadata of the given columns (or all columns if the parameter is
null ) for the given tables. |
static boolean[] |
getColumnsNumericsMetaData(java.lang.String dataSource,
java.lang.String tables,
java.lang.String[] columns)
This method do a small request to the given database
to extract metadata of given columns.
|
static java.sql.Connection |
getConnection(java.lang.String dataSource)
Attempts to establish a connection with a given data source.
|
static java.sql.Connection |
getConnection(java.lang.String dataSource,
java.lang.String username,
java.lang.String password)
Attempts to establish a connection with a given data source.
|
static DatabaseInfo |
getDatabaseInfo(java.lang.String dataSource)
Returns the Database info for the given data source.
|
static DatabaseInfo |
getDatabaseInfo(java.lang.String url,
java.lang.String user,
java.lang.String password)
Returns the Database info for the given JDBC parameters.
|
static javax.sql.DataSource |
getDataSource(java.lang.String dataSource)
Lookup for the given data source.
|
static java.util.Map<java.lang.String,javax.sql.DataSource> |
getDataSourceMap()
Lookup for all "java:com/env/jdbc/*" data sources declared in the AppServer.
|
static java.lang.Object |
getFirstValue(java.sql.Connection conn,
java.lang.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.
|
static java.lang.Object |
getFirstValue(java.lang.String dataSource,
java.lang.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.
|
static java.lang.Object |
getFirstValue(java.lang.String dataSource,
java.lang.String select,
java.lang.String from,
java.lang.String orderby,
java.lang.String[] whereParams,
java.lang.String[] whereValues,
boolean and)
This convenient method performs a query then it returns the value of the first column of the first row of the result set.
|
static void |
initDataSourceMap(java.util.Set<java.lang.String> includedDataSourceSet,
java.util.Set<java.lang.String> excludedDataSourceSet)
This method must be called before any other methods accessing to the dataSourceMap.
|
public static final java.lang.String REVISION
public static java.util.Map<java.lang.String,javax.sql.DataSource> getDataSourceMap()
getDataSource(String)
public static void initDataSourceMap(java.util.Set<java.lang.String> includedDataSourceSet, java.util.Set<java.lang.String> excludedDataSourceSet)
includedDataSourceSet
- if not empty, only dataSource declared in this set will be used.excludedDataSourceSet
- if not empty, any dataSource declared in this set will be excluded.public static javax.sql.DataSource getDataSource(java.lang.String dataSource)
dataSource
- the name of the data source (eg. "jdbc/TestDB")getDataSourceMap()
public static java.sql.Connection getConnection(java.lang.String dataSource) throws java.sql.SQLException
dataSource
- the name of the data source (eg. "jdbc/TestDB")java.sql.SQLException
- if an error occurspublic static java.sql.Connection getConnection(java.lang.String dataSource, java.lang.String username, java.lang.String password) throws java.sql.SQLException
dataSource
- the name of the data source (eg. "jdbc/TestDB")username
- the database user on whose behalf the connection is being madepassword
- the user's passwordjava.sql.SQLException
- if an error occurspublic static java.lang.String buildSelectQuery(java.lang.String dataSource, java.lang.String select, java.lang.String from, java.lang.String orderby, java.lang.String[] whereParams, java.lang.String[] whereValues, boolean and) throws java.sql.SQLException
dataSource
- 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 parameterwhereParams
- an array of parameterswhereValues
- an array of parameters valueand
- should be 'AND' or 'OR' between parametersjava.sql.SQLException
- if an error occurspublic static boolean[] getColumnsNumericsMetaData(java.lang.String dataSource, java.lang.String tables, java.lang.String[] columns) throws java.sql.SQLException
"numeric" means Types.BIGINT, Types.INTEGER, Types.SMALLINT, Types.NUMERIC
dataSource
- the name of the data source (eg. "jdbc/TestDB")tables
- the table namescolumns
- the column namesjava.sql.SQLException
- if an error occurspublic static java.sql.ResultSetMetaData getColumnsMetaData(java.lang.String dataSource, java.lang.String tables, java.lang.String[] columns) throws java.sql.SQLException
null
) for the given tables.dataSource
- the name of the data source (eg. "jdbc/TestDB")tables
- the table namescolumns
- the column names. If null
, all columns of the given tablesjava.sql.SQLException
- if an error occurspublic static java.lang.Object getFirstValue(java.lang.String dataSource, java.lang.String select, java.lang.String from, java.lang.String orderby, java.lang.String[] whereParams, java.lang.String[] whereValues, boolean and) throws java.sql.SQLException
dataSource
- 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 parameterwhereParams
- an array of parameterswhereValues
- an array of parameters valueand
- should be 'AND' or 'OR' between parametersjava.sql.SQLException
- if an error occurspublic static java.lang.Object getFirstValue(java.lang.String dataSource, java.lang.String sqlQuery) throws java.sql.SQLException
dataSource
- the name of the data source (eg. "jdbc/TestDB")sqlQuery
- the SQL query to be performedjava.sql.SQLException
- if an error occurspublic static java.lang.Object getFirstValue(java.sql.Connection conn, java.lang.String sqlQuery)
conn
- the JDBC connectionsqlQuery
- the SQL query to be performedpublic static SQLQueryResult executeQuery(java.lang.String dataSource, java.lang.String sqlQuery, int maxRows)
dataSource
- the name of the data source (eg. "jdbc/TestDB")sqlQuery
- the SQL (SELECT) querymaxRows
- the max rows limit; zero means there is no limitpublic static void executeUpdate(java.lang.String dataSource, java.lang.String sql)
dataSource
- the data sourcesql
- update querypublic static boolean checkDataSource(java.lang.String dataSource)
dataSource
- the data source.public static void addDataSource(java.lang.String name, javax.sql.DataSource dataSource)
name
- the name of the data source.dataSource
- the data source.public static DatabaseInfo getDatabaseInfo(java.lang.String dataSource)
dataSource
- the data source.public static DatabaseInfo getDatabaseInfo(java.lang.String url, java.lang.String user, java.lang.String password)
url
- the JDBC urluser
- the JDBC userpassword
- the JDBC passwordCopyright © 2001-2021 Jalios SA. All Rights Reserved.