Package ch.k43.util

Klasse KDB

java.lang.Object
ch.k43.util.KDB
Alle implementierten Schnittstellen:
AutoCloseable

public class KDB extends Object implements AutoCloseable
Database class to support compliant JDBC databases. 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.exec("INSERT INTO ADDRESSES (LASTNAME, FIRSTNAME) VALUES ('Smith', 'John')");
    db.exec("SELECT * FROM ADDRESSES");
    System.out.println(db.getDataAsTable());
 }
 
Seit:
2024.06.14
  • Felddetails

  • Konstruktordetails

    • KDB

      public KDB(String argDriverClass, String argURL, String argUserName, String argPassword)
      Load JDBC driver and establish connection to database.
      Parameter:
      argDriverClass - JDBC driver class name (Example: "org.h2.Driver")
      argURL - JDBC connection URL (Example: "jdbc:h2:mem:myDb")
      argUserName - User name
      argPassword - Password
  • Methodendetails

    • close

      public void close()
      Close the JDBC connection.
      Angegeben von:
      close in Schnittstelle AutoCloseable
    • commit

      public boolean commit()
      Commit transaction.
      Gibt zurück:
      True if successful, false otherwise
      Seit:
      2024.06.17
    • exec

      public boolean exec(String argStatement)
      Execute dynamic SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().
      Parameter:
      argStatement - SQL statement
      Gibt zurück:
      Success or failure
    • exec

      public boolean exec(String argStatement, int argMaxRows)
      Execute dynamic SQL statement. For SELECT statements, the result set is fetched and saved as convenient Java Objects to be retrieved by getDataXXX().
      Parameter:
      argStatement - SQL statement
      argMaxRows - Maximum number of rows to fetch or -1 for all
      Gibt zurück:
      Success or failure
      Seit:
      2024.08.19
    • getColumnCount

      public int getColumnCount()
      Get number of columns in result set.
      Gibt zurück:
      Number of columns or -1
    • getColumnNames

      public String[] getColumnNames()
      Get column names in result set.
      Gibt zurück:
      Column names or null
    • getColumnWidths

      public int[] getColumnWidths()
      Get column widths in result set.
      Gibt zurück:
      Column width or null
    • getData

      public ArrayList<Object[]> getData()
      Get fetched data as an ArrayList (rows) with an array of Objects (columns).
      Gibt zurück:
      Array with data or null
    • getDataAsCSV

      public String getDataAsCSV()
      Get result set formatted as CSV string. The default field delimiter is a comma.
      Gibt zurück:
      CSV string or null
    • getDataAsCSV

      public String getDataAsCSV(char argDelimiter)
      Get result set formatted as CSV string.
      Parameter:
      argDelimiter - Delimiter character
      Gibt zurück:
      CSV string or null
    • getDataAsCSV

      public String getDataAsCSV(char argDelimiter, boolean argHeader)
      Get result set formatted as CSV string delimited by the passed character (Example: ',')
      Parameter:
      argDelimiter - Delimiter character
      argHeader - Write header line with column names
      Gibt zurück:
      CSV string or null
      Seit:
      2024.06.21
    • getDataAsJSON

      public String getDataAsJSON()
      Get result set formatted as JSON string
      Gibt zurück:
      JSON string or null
    • getDataAsTable

      public String getDataAsTable()
      Get result set formatted as display table with column headers
      Gibt zurück:
      String with formatted table
    • getDataAsTable

      public String getDataAsTable(boolean argHeader)
      Get result set formatted as display table
      Parameter:
      argHeader - True to add column header, false otherwise
      Gibt zurück:
      String with formatted table
      Seit:
      2024.09.01
    • getDataAsXML

      public String getDataAsXML()
      Get result set formatted as XML UTF-8 string
      Gibt zurück:
      XML string or null
    • getDataAsYAML

      public String getDataAsYAML()
      Get result set formatted as YAML string
      Gibt zurück:
      YAML string or null
    • getElapsedTime

      public long getElapsedTime()
      Get elapsed time of last SQL statement.
      Gibt zurück:
      Elapsed time in milliseconds or -1
      Seit:
      2024.06.24
    • getErrorMessage

      public String getErrorMessage()
      Get last error message.
      Gibt zurück:
      Error message or null
    • getRowCount

      public long getRowCount()
      Get number of rows read or updated.
      Gibt zurück:
      Row count or or null
    • getTableName

      public String 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(*).
      Gibt zurück:
      Table name or null
    • getTableName

      public String getTableName(int argColumnNumber)
      Get table name of given column number. Note: The underlying JDBC call does not always return the table name, as in SELECT COUNT(*).
      Parameter:
      argColumnNumber - Column number for which the table name is returned
      Gibt zurück:
      Table name or null
      Seit:
      2024.06.27
    • isConnected

      public boolean isConnected()
      Get state of JDBC connection.
      Gibt zurück:
      True if connected, false otherwise
    • rollback

      public boolean rollback()
      Rollback transaction.
      Gibt zurück:
      True if successful, false otherwise
      Seit:
      2024.06.17
    • setAutoCommit

      public void setAutoCommit(boolean argState)
      Set auto commit state.
      Parameter:
      argState - True for auto commit, false otherwise
      Seit:
      2024.06.26
    • toString

      public String toString()
      String representation of object.
      Setzt außer Kraft:
      toString in Klasse Object
      Seit:
      2024.08.23