public class DbExec
extends java.lang.Object
Constructor and Description |
---|
DbExec(DbConfig dbConfig,
boolean connectAsSystemAdmin,
org.slf4j.Logger logger)
DbExec constructor:
Create a database connection and set autoCommit to false.
|
Modifier and Type | Method and Description |
---|---|
void |
closeConnection()
Close database connection.
|
void |
commit()
Commit current transaction
|
void |
createSchema(java.lang.String schema,
java.lang.String username,
java.lang.String password,
java.lang.String userHost)
Create schema and user.
|
java.sql.Statement |
createStatement()
Create a statement using current connection.
|
void |
deleteSchema(java.lang.String schema,
java.lang.String username,
java.lang.String userHost)
Delete the schema and revoke privilege from the user.
|
boolean |
execute(java.lang.String query)
Execute query, can not execute multiple statements.
|
java.sql.ResultSet |
executeQuery(java.lang.String query)
Execute query and close statement.
|
int |
executeSql(java.io.Reader isReader)
Execute a sql file.
|
int |
executeSqlFile(java.lang.String sqlFile,
java.lang.String encoding)
Execute sql file.
|
int |
executeSqlText(java.lang.String sqlText)
Execute the Sql text that consists of multiple statements.
|
int |
executeUpdate(java.lang.String query)
Execute update query
|
int |
executeUpdate(java.lang.String query,
java.lang.Object[] params)
Execute update
|
void |
exportSchema(java.lang.String schema,
java.io.File outputFile)
Export schema objects to the file.
|
java.sql.Connection |
getConnection()
Get database connection
|
DbConfig |
getDbConfig()
Get the DbConfig that is associated with this DbExec instance.
|
static DbExec |
getInstance(DbConfig dbConfig,
boolean connectAsSystemAdmin,
org.slf4j.Logger logger)
Get DbExec.
|
org.slf4j.Logger |
getLogger()
Get logger used by this DbExec.
|
java.util.List<java.lang.String> |
getSchemaNames(java.lang.String catelog,
java.lang.String schemaPrefix)
Get schema names that has the prefix, excluding the prefix itself.
|
void |
importSchema(java.lang.String srcSchema,
java.lang.String targetSchema,
java.io.File dumpFile)
Import schema objects to the targetSchema.
|
boolean |
isConnectAsSystemAdmin()
Whether user is connected to database server as system administrator
|
boolean |
isSchemaExist(java.lang.String catelog,
java.lang.String schema)
Does the schema exist by the connection of the sqlExec.
|
boolean |
isTableColumnDefined(java.lang.String catelog,
java.lang.String schema,
java.lang.String tableColumn)
Check if the column is defined in the table.
|
boolean |
isTableExist(java.lang.String catelog,
java.lang.String schema,
java.lang.String tableName)
Check if the table exists in the schema of the database.
|
void |
rollback()
Rollback current transaction
|
void |
setCurrentSchema(java.lang.String schema)
Set current schema of session.
|
void |
setLogger(org.slf4j.Logger logger)
Set logger
|
public DbExec(DbConfig dbConfig, boolean connectAsSystemAdmin, org.slf4j.Logger logger) throws java.sql.SQLException
dbConfig
- Database configconnectAsSystemAdmin
- whether to connect as system administratorlogger
- logger. null to disable loggingjava.sql.SQLException
- throw if a database access error occurspublic static DbExec getInstance(DbConfig dbConfig, boolean connectAsSystemAdmin, org.slf4j.Logger logger) throws java.sql.SQLException
dbConfig
- Database configconnectAsSystemAdmin
- whether to connect as system administratorlogger
- logger. null to disable loggingjava.sql.SQLException
- throw if a database access error occurspublic DbConfig getDbConfig()
public java.sql.Connection getConnection()
public boolean isConnectAsSystemAdmin()
public org.slf4j.Logger getLogger()
public void setLogger(org.slf4j.Logger logger)
logger
- logger. disable logging if null.public java.sql.ResultSet executeQuery(java.lang.String query) throws java.sql.SQLException
query
- sql queryjava.sql.SQLException
- throw if an error occurs when executing the querypublic java.sql.Statement createStatement() throws java.sql.SQLException
java.sql.SQLException
- throw if an database access error occurspublic int executeUpdate(java.lang.String query) throws java.sql.SQLException
query
- sql queryjava.sql.SQLException
- throw if an error occurs when executing the querypublic int executeUpdate(java.lang.String query, java.lang.Object[] params) throws java.sql.SQLException
query
- sql stringparams
- query parameter values by index.java.sql.SQLException
- throw if an error occurs when executing the querypublic boolean execute(java.lang.String query) throws java.sql.SQLException
query
- sql queryjava.sql.SQLException
- throw if an error occurs when executing the querypublic int executeSqlFile(java.lang.String sqlFile, java.lang.String encoding) throws java.sql.SQLException
sqlFile
- sql file to executeencoding
- sql file encoding such as UTF-8java.sql.SQLException
- throw if an error occurs when executing any of the sql statements in the filepublic int executeSqlText(java.lang.String sqlText) throws java.sql.SQLException
sqlText
- sql textjava.sql.SQLException
- throw if an error occurs when executing any of the sql statements in the textpublic int executeSql(java.io.Reader isReader) throws java.sql.SQLException
isReader
- the reader to get sql statementsjava.sql.SQLException
- throw if an error occurs when executing any of the sql statements in the file.public void closeConnection() throws java.sql.SQLException
java.sql.SQLException
- if a database access error occurspublic void commit() throws java.sql.SQLException
java.sql.SQLException
- throw if the transaction can not be committedpublic void rollback() throws java.sql.SQLException
java.sql.SQLException
- throw if the transaction can not be rolled backpublic boolean isSchemaExist(java.lang.String catelog, java.lang.String schema) throws java.sql.SQLException
catelog
- catelog nameschema
- schema namejava.sql.SQLException
- throw if a database access error occurspublic java.util.List<java.lang.String> getSchemaNames(java.lang.String catelog, java.lang.String schemaPrefix) throws java.sql.SQLException
catelog
- database catelogschemaPrefix
- schema name prefixjava.sql.SQLException
- throw if a database access error occurspublic boolean isTableExist(java.lang.String catelog, java.lang.String schema, java.lang.String tableName) throws java.sql.SQLException
catelog
- database catelogschema
- schema nametableName
- tableNamejava.sql.SQLException
- throw if a database access error occurspublic boolean isTableColumnDefined(java.lang.String catelog, java.lang.String schema, java.lang.String tableColumn) throws java.sql.SQLException
catelog
- database catelogschema
- schema nametableColumn
- format: tableName.columnNamejava.sql.SQLException
- throw if a database access error occurspublic void setCurrentSchema(java.lang.String schema) throws java.sql.SQLException, ExecException
schema
- new schemaExecException
- throw if a command execution error occursjava.sql.SQLException
- throw if a SQL execution error occurspublic void createSchema(java.lang.String schema, java.lang.String username, java.lang.String password, java.lang.String userHost) throws java.sql.SQLException, ExecException
schema
- schema to createusername
- user to access the schema objectspassword
- the user password to make connection to databaseuserHost
- the user host name. some DBMS limits access from the specified host only.ExecException
- throw if a command execution error occursjava.sql.SQLException
- throw if a SQL execution error occurspublic void deleteSchema(java.lang.String schema, java.lang.String username, java.lang.String userHost) throws java.sql.SQLException, ExecException
schema
- the schema to deleteusername
- the user from which to revoke privilegesuserHost
- user host to identify the userExecException
- throw if a command execution error occursjava.sql.SQLException
- throw if a SQL execution error occurspublic void exportSchema(java.lang.String schema, java.io.File outputFile) throws java.sql.SQLException, ExecException
schema
- schema to exportoutputFile
- the file to store schema objectsExecException
- throw if a command execution error occursjava.sql.SQLException
- throw if a SQL execution error occurspublic void importSchema(java.lang.String srcSchema, java.lang.String targetSchema, java.io.File dumpFile) throws ExecException, java.sql.SQLException
srcSchema
- dumpFile schematargetSchema
- the target schema to import datadumpFile
- the dump file to import, can be sql or other format.ExecException
- throw if a command execution error occursjava.sql.SQLException
- throw if a SQL execution error occurs
© 2014-2019 Cmobilecom and/or its licensors. All rights reserved.
Report an issue or request a new feature