JDBC CLI Documentation
JDBC (Java Database Connectivity) is a standard Java API for accessing and managing relational databases. It can be used to execute SQL queries and retrieve results programmatically. Refer to Java Database Connectivity for more details.
The MetricsHub JDBC CLI allows users to connect to databases, execute queries, and retrieve data or perform updates. It supports JDBC-compliant URLs, authentication, and configurable timeouts.
Syntax
jdbccli HOSTNAME --username USERNAME --password PASSWORD --url JDBC_URL --query SQL_QUERY
Options
| Option | Description | Default Value |
|---|---|---|
HOSTNAME | Hostname or IP address of the database server. | None |
--url | The JDBC URL for the database connection. This option is required. | None |
--username | Username for JDBC authentication. | None |
--password | Password for JDBC authentication. If not provided, you will be prompted interactively. | None |
--query | The SQL query to execute. This option is required. | None |
--timeout | Timeout in seconds for SQL query execution. | 30 |
--driver-class | The fully-qualified java.sql.Driver implementation class to use (e.g., com.ibm.as400.access.AS400JDBCDriver). Required when the URL is not accepted by any of the built-in drivers. | None |
--driver-jar | The path to the JDBC driver .jar file. Ignored unless --driver-class is also provided. | None |
-v | Enables verbose mode. Use -v for basic logs, -vv for detailed logs. | None |
-h, --help | Displays detailed help information about available options. | None |
Examples
Example 1: Basic SQL Query
jdbccli dev-01 --username admin --password secret --url="jdbc:mysql://dev-01:3306/MyDb" --query="SELECT * FROM users"
Example 2: Basic SQL Query with Interactive Password Input
jdbccli dev-01 --username admin --url="jdbc:postgresql://dev-01:5432/MyDb" --query="SELECT * FROM employees"
The CLI prompts for the password if not provided.
Example 3: Basic SQL Query with Timeout Configuration
jdbccli dev-01 --username admin --password secret --url="jdbc:oracle:thin:@dev-01:1521/MyDb" --query="UPDATE accounts SET balance = balance - 100" --timeout 60
Example 4: SQL Query with an External JDBC Driver
jdbccli dev-01 --username admin --password secret --url="jdbc:as400://dev-01;naming=sql;prompt=false;errors=full" --query="SELECT * FROM QSYS2.SYSTEM_STATUS_INFO" --driver-class com.ibm.as400.access.AS400JDBCDriver --driver-jar /opt/db/drivers/jt400-21.0.6-java11.jar
Use --driver-class and --driver-jar when the JDBC driver for your database is not bundled with MetricsHub.