跪拜 Guibai
← All articles
Database

KingbaseES JDBC: From Default Admin to a Proper Business Connection

By 倔强的石头_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

KingbaseES is a PostgreSQL-compatible database widely deployed in Chinese enterprise and government systems. A clean JDBC bootstrap that separates the admin account from a least-privilege application account, and that verifies permissions explicitly, prevents the credential-mixing and silent-failure patterns that derail integration projects.

Summary

Most application connection failures trace back to three misalignments: the driver, the connection string, and account permissions. This walkthrough starts from a fresh KingbaseES install, connects with the default `system` account, then creates a dedicated business database (`kb_app`), schema (`shop`), and separate application and read-only accounts. It locates the JDBC driver from the server installation, constructs a minimal connection URL, and runs a `PreparedStatement` query from a plain Java class on Windows 11.

A second version extracts hardcoded credentials into a `db.properties` file, making the setup ready for Spring Boot or environment-variable injection. The verification section insists on three checks beyond a silent success: identity confirmation via `current_database()` and `current_user`, explicit read/write and read-only permission tests, and deliberate error injection to preview authentication failures, connection timeouts, and missing-table messages before they hit production.

Common pitfalls are cataloged with direct fixes: driver-not-found classpath issues, connection-refused network misconfigurations, authentication failures from mixing `system/test` with `app_user/kb_app`, missing schema qualifiers causing "relation does not exist" errors, and Chinese-character encoding problems on Windows consoles.

Takeaways
Connection failures are usually a driver-classpath, URL, or permissions problem, not a code-logic problem.
Never build applications on the default `system` account or `test` database; create a dedicated business database, schema, and least-privilege application user.
Use `PreparedStatement` and `try-with-resources` in the minimal verification program to avoid SQL injection and resource leaks.
Always qualify table names with the schema (`shop.t_connection_check`) instead of relying on the default search path.
Extract connection details into a `db.properties` file immediately after the first successful query to prepare for Spring Boot and environment-variable configuration.
After a silent success, explicitly verify identity (`current_database()`, `current_user`), permissions (read/write vs. read-only), and error handling (wrong password, wrong IP, wrong table name).
On Windows, use PowerShell with `-encoding UTF-8` during compilation to avoid Chinese-character encoding issues.
Conclusions

The tutorial treats deliberate error injection as a first-class verification step, which is a practical habit that most getting-started guides skip but that pays off immediately in integration projects.

Separating a read-only `report_user` from a read-write `app_user` at the database level, even in a minimal demo, establishes a permission model that maps directly to production reporting and service accounts.

The progression from a hardcoded Java class to a `db.properties` file in the same article models the exact refactor a developer should perform before committing the first connection code.

Concepts & terms
KingbaseES
A Chinese relational database management system that is wire-protocol and SQL compatible with PostgreSQL, commonly used in enterprise and government deployments.
JDBC connection string for KingbaseES
A URL following the pattern `jdbc:kingbase8://<host>:<port>/<database>` that identifies the driver protocol, server address, port, and target database for a Java application.
Schema-qualified table names
Writing table references as `schema.table` (e.g., `shop.t_connection_check`) rather than bare table names, which avoids ambiguity when multiple schemas exist or the default search path changes.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗