Skip to content

Commit 27af603

Browse files
committed
feat(jdbc-sqlite): add DataBase demo to fetch and display student records
What - Added `DataBase` example under Section28JDBCusingSQLite. - Establishes a connection to `univ.db` using SQLite JDBC driver. - Executes a `SELECT * FROM students` query via `Statement`. - Iterates over `ResultSet` to print all student details: - Roll number - Name - City - Department number Why - Demonstrates the use of plain `Statement` for executing static SQL queries. - Provides a foundational example for working with `ResultSet` in JDBC. - Useful for quick table dumps or when parameter binding is not required. - Serves as a stepping stone before introducing `PreparedStatement`. How - Loaded SQLite JDBC driver with `Class.forName("org.sqlite.JDBC")`. - Opened connection using `DriverManager.getConnection()`. - Created `Statement` to run SQL queries. - Executed `executeQuery("SELECT * FROM students")`. - Used `rs.next()` loop to retrieve column values (`getInt`, `getString`). - Printed records in a pipe-delimited format. - Closed `Statement` and `Connection` to release resources. Key Notes - `Statement` is best for static SQL with no dynamic parameters. - `ResultSet` cursor moves row by row; must check with `rs.next()`. - Always close JDBC resources to prevent memory leaks. - For dynamic queries, prefer `PreparedStatement`. Real-life Applications - Fetch all records for reporting modules. - Generate tabular views in admin dashboards. - Quick debugging tool to verify table contents. - Base implementation for migrating data to another system. Future Improvements - Replace `Statement` with `PreparedStatement` for parameterized queries. - Use try-with-resources for automatic resource cleanup. - Format output into aligned tables for better readability. - Extend functionality to filter records (e.g., by deptno or city). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 96a5497 commit 27af603

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Section28JDBCusingSQLite/JAVA SQL Interfaces/src/DataBase.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ public class DataBase {
77
public static void main(String[] args) throws Exception {
88
Class.forName("org.sqlite.JDBC");
99

10-
//Connection is extablish.
11-
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/univ.db");
12-
System.out.println("✅ Connection Established!");
10+
Connection con = DriverManager.getConnection(
11+
"jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db"
12+
);
1313

14+
System.out.println("Connection Established!");
1415

15-
//Connection object and con is an interface.
16-
//con.
16+
//Connection object and con are an interface.
1717
Statement stm = con.createStatement();
1818

19-
//entire table name means the output of table we stored.
19+
//the entire table name means the output of the table we stored.
2020
ResultSet rs = stm.executeQuery("SELECT * FROM students");
2121

2222
while (rs.next()) {
@@ -25,8 +25,8 @@ public static void main(String[] args) throws Exception {
2525
System.out.print(rs.getString("city") + " ");
2626
System.out.println(rs.getInt("deptno")+" | ");
2727
}
28-
stm.close(); //close the statement.
29-
con.close(); //Connection close.
30-
System.out.println("Connection Closed.");
28+
stm.close();
29+
con.close();
30+
System.out.println("Connection Closed.");
3131
}
3232
}

0 commit comments

Comments
 (0)