Skip to content

Commit d0c381c

Browse files
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

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Section28JDBCusingSQLite/DDLUsingJDBC/src/Database.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
import java.sql.Statement;
44

55
public class Database {
6-
public static void main(String[] args)throws Exception {
6+
public static void main(String[] args) throws Exception {
7+
// Load SQLite JDBC driver
78
Class.forName("org.sqlite.JDBC");
89

9-
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/univ.db");
10+
String url = "jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db";
11+
Connection con = DriverManager.getConnection(url);
1012

1113
Statement stm = con.createStatement();
1214

13-
//stm.executeUpdate("create table Temp(a integer, b float)");
15+
// Uncomment if you want to create a table.
16+
// stm.executeUpdate("CREATE TABLE Temp(a INTEGER, b FLOAT)");
1417

15-
//stm.executeUpdate("drop table Temp");
18+
// Uncomment if you want to drop a table.
19+
// stm.executeUpdate("DROP TABLE Temp");
1620

1721
stm.close();
1822
con.close();
23+
System.out.println("Database connected successfully!");
1924
}
20-
}
25+
}

0 commit comments

Comments
 (0)