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 String
Cloudscape JDBC driverstatic final String
DB2 JDBC driverstatic final String
Derby driversstatic final String
H2 JDBC driverstatic final String
HSQLDB JDBC driverstatic final String
Informix JDBC driverstatic final String
MariaDB JDBC driverstatic final String
MS SQL JDBC driverstatic final String
MySQL JDBC driverstatic final String
Oracle JDBC driverstatic final String
PostGreSQL JDBC driverstatic final String
SQLite JDBC driverstatic final String
Sybase JDBC driver -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the JDBC connection.boolean
commit()
Commit transaction.boolean
Execute dynamic SQL statement.boolean
Execute dynamic SQL statement.boolean
Execute dynamic SQL statement.boolean
execPrepare
(Object... argObjects) Complete and execute precompiled SQL statement.int
Get 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 stringlong
Get elapsed time of last SQL statement.Get last error message.long
Get 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.boolean
Get state of JDBC connection.boolean
Prepare SQL statement.boolean
Prepare SQL statement.boolean
Prepare SQL statement.boolean
rollback()
Rollback transaction.void
setAutoCommit
(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
-