Class KDB
java.lang.Object
ch.k43.util.KDB
- All Implemented Interfaces:
AutoCloseable
Class to support all JDBC compliant databases with basic database functions. The result set from SELECT statements is kept
in memory with easy to access objects and functions.
Example:
try (KDB db = new KDB(KDB.JDBC_H2, "jdbc:h2:mem:mydb", "", "")) {
KLog.abort(!db.isConnected(), "Error: {}", db.getErrorMessage());
db.exec("CREATE TABLE addresses (sequence INT AUTO_INCREMENT, lastname VARCHAR(20), firstname VARCHAR(20))");
db.prepare("INSERT INTO addresses (lastname, firstname) VALUES (?, ?)");
db.execPrepare("Smith", "Joe");
db.execPrepare("Miller", "Bob");
db.execPrepare("Johnson", "Evelyn");
db.exec("SELECT * FROM addresses");
System.out.println(db.getDataAsJSON());
- Since:
- 2024.06.14
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringCloudscape JDBC driverstatic final StringDB2 JDBC driverstatic final StringDerby driversstatic final StringH2 JDBC driverstatic final StringHSQLDB JDBC driverstatic final StringInformix JDBC driverstatic final StringMariaDB JDBC driverstatic final StringMS SQL JDBC driverstatic final StringMySQL JDBC driverstatic final StringOracle JDBC driverstatic final StringPostGreSQL JDBC driverstatic final StringSQLite JDBC driverstatic final StringSybase JDBC driver -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the JDBC connection.booleancommit()Commit transaction.booleanExecute dynamic SQL statement.booleanExecute dynamic SQL statement.booleanExecute dynamic SQL statement.booleanexecPrepare(Object... argObjects) Complete and execute precompiled SQL statement.intGet number of columns in result set.String[]Get column names in result set.int[]Get column widths in result set.getData()Get fetched data as an ArrayList (rows) with an array of Objects (columns).Get result set formatted as CSV string.getDataAsCSV(char argDelimiter) Get result set formatted as CSV string.getDataAsCSV(char argDelimiter, boolean argHeader) Get result set formatted as CSV string delimited by the passed character (Example: ',')Get result set formatted as JSON stringGet result set formatted as display table with column headersgetDataAsTable(boolean argHeader) Get result set formatted as display tableGet result set formatted as XML UTF-8 stringGet result set formatted as YAML stringlongGet elapsed time of last SQL statement.Get last error message.longGet number of rows read or updated.Get table name of first column of result set.getTableName(int argColumnNumber) Get table name of given column number.booleanGet state of JDBC connection.booleanPrepare SQL statement.booleanPrepare SQL statement.booleanPrepare SQL statement.booleanrollback()Rollback transaction.voidsetAutoCommit(boolean argState) Set auto commit state.toString()String representation of object.
-
Field Details
-
JDBC_ORACLE
-
JDBC_MYSQL
-
JDBC_H2
-
JDBC_DB2
-
JDBC_POSTGRESQL
-
JDBC_MSSQL
-
JDBC_SYBASE
-
JDBC_CLOUDSCAPE
-
JDBC_INFORMIX
-
JDBC_HSQLDB
-
JDBC_DERBY
-
JDBC_SQLITE
-
JDBC_MARIADB
-
-
Constructor Details
-
KDB
Load JDBC driver and establish connection to database.- Parameters:
argDriverClass- JDBC driver class name (Example: "org.h2.Driver")argURL- JDBC connection URL (Example: "jdbc:h2:mem:myDb")argUserName- User nameargPassword- Password
-
-
Method Details
-
close
-
commit
public boolean commit()Commit transaction.- Returns:
- True if successful, false otherwise
- Since:
- 2024.06.17
-
exec
Execute dynamic SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().- Parameters:
argStatement- SQL statement- Returns:
- Success or failure
-
exec
Execute dynamic SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().- Parameters:
argStatement- SQL statementargMaxRows- Maximum number of rows to fetch or 0 for all- Returns:
- Success or failure
- Since:
- 2024.08.19
-
exec
Execute dynamic SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().- Parameters:
argStatement- SQL statementargMaxRows- Maximum number of rows to fetch or 0 for allargTimeOutSec- Execution time out in seconds or 0 for no timeout- Returns:
- Success or failure
- Since:
- 2024.09.17
-
execPrepare
Complete and execute precompiled SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().- Parameters:
argObjects- ... Values to be inserted in the sequence of the placeholder(s) '?' of the precompiled SQL statement- Returns:
- Success or failure
- Since:
- 2024.10.23
-
getColumnCount
public int getColumnCount()Get number of columns in result set.- Returns:
- Number of columns or 0
-
getColumnNames
-
getColumnWidths
public int[] getColumnWidths()Get column widths in result set.- Returns:
- Column width or null
-
getData
-
getDataAsCSV
Get result set formatted as CSV string. The default field delimiter is a comma.- Returns:
- CSV string or null
-
getDataAsCSV
Get result set formatted as CSV string.- Parameters:
argDelimiter- Delimiter character- Returns:
- CSV string or null
-
getDataAsCSV
Get result set formatted as CSV string delimited by the passed character (Example: ',')- Parameters:
argDelimiter- Delimiter characterargHeader- Write header line with column names- Returns:
- CSV string or null
- Since:
- 2024.06.21
-
getDataAsJSON
-
getDataAsTable
Get result set formatted as display table with column headers- Returns:
- String with formatted table
-
getDataAsTable
Get result set formatted as display table- Parameters:
argHeader- True to add column header, false otherwise- Returns:
- String with formatted table
- Since:
- 2024.09.01
-
getDataAsXML
Get result set formatted as XML UTF-8 string- Returns:
- XML string or null
-
getDataAsYAML
Get result set formatted as YAML string- Returns:
- YAML string or null
- Since:
- 2024.09.14
-
getElapsedTime
public long getElapsedTime()Get elapsed time of last SQL statement.- Returns:
- Elapsed time in milliseconds or 0
- Since:
- 2024.06.24
-
getErrorMessage
-
getRowCount
public long getRowCount()Get number of rows read or updated.- Returns:
- Row count
-
getTableName
Get table name of first column of result set. Note: The underlying JDBC call does not always return the table name, as in SELECT COUNT(*).- Returns:
- Table name or null
-
getTableName
Get table name of given column number. Note: The underlying JDBC call does not always return the table name, as in SELECT COUNT(*).- Parameters:
argColumnNumber- Column number for which the table name is returned- Returns:
- Table name or null
- Since:
- 2024.06.27
-
isConnected
public boolean isConnected()Get state of JDBC connection.- Returns:
- True if connected, false otherwise
-
prepare
Prepare SQL statement. The prepared statement must later be executed withexecPrepared().- Parameters:
argStatement- SQL statement- Returns:
- Success or failure
- Since:
- 2024.09.25
-
prepare
Prepare SQL statement. The prepared statement must later be executed withexecPrepared().- Parameters:
argStatement- SQL statementargMaxRows- Maximum number of rows to fetch or 0 for all- Returns:
- Success or failure
- Since:
- 2024.09.25
-
prepare
Prepare SQL statement. The prepared statement must later be executed withexecPrepare().- Parameters:
argStatement- SQL statementargMaxRows- Maximum number of rows to fetch or 0 for allargTimeOutSec- Execution time out in seconds or 0 for no time out- Returns:
- Success or failure
- Since:
- 2024.10.24
-
rollback
public boolean rollback()Rollback transaction.- Returns:
- True if successful, false otherwise
- Since:
- 2024.06.17
-
setAutoCommit
public void setAutoCommit(boolean argState) Set auto commit state.- Parameters:
argState- True for auto commit, false otherwise- Since:
- 2024.06.26
-
toString
-