Commit d0c381c
committed
feat(jdbc-sqlite): add Database.java for connecting and managing SQLite database
What
- Implemented `Database.java` demonstrating JDBC connectivity with SQLite.
- Loads the `org.sqlite.JDBC` driver and establishes a connection to `univ.db`.
- Showcased table management operations:
- Creating a table (`CREATE TABLE Temp(a INTEGER, b FLOAT)`).
- Dropping a table (`DROP TABLE Temp`).
- Ensures proper cleanup by closing `Statement` and `Connection`.
- Added confirmation message for successful connection.
Why
- Demonstrates how to establish JDBC connection with SQLite.
- Provides foundational example for executing SQL statements programmatically.
- Helps developers integrate SQLite into Java applications with minimal setup.
- Serves as a template for database management operations (DDL/DML).
How
- Driver loading: `Class.forName("org.sqlite.JDBC")`.
- Connection established via:
`DriverManager.getConnection("jdbc:sqlite:/path/to/univ.db")`.
- SQL execution using `Statement stm = con.createStatement()`.
- Supports switching between `CREATE` and `DROP` by uncommenting respective queries.
- Final step: gracefully closing resources.
Key Notes
- SQLite automatically creates the database file (`univ.db`) if it doesn’t exist.
- JDBC abstracts away low-level handling → safe, simple, reusable.
- Always close `Statement` and `Connection` to prevent resource leaks.
- SQL queries can be expanded to include `INSERT`, `UPDATE`, `DELETE`.
Real-life Applications
- Setting up local relational databases for desktop apps.
- Managing schema creation and migration via Java.
- Educational projects requiring quick DB integration.
- Prototyping database-backed applications without full-fledged DBMS.
Future Improvements
- Replace `Statement` with `PreparedStatement` to prevent SQL injection.
- Implement try-with-resources for automatic cleanup.
- Add CRUD operation examples for better completeness.
- Extend with logging and error handling best practices.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a294357 commit d0c381c
1 file changed
+10
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | | - | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | | - | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| 23 | + | |
19 | 24 | | |
20 | | - | |
| 25 | + | |
0 commit comments