Skip to content

Commit a0c4fa3

Browse files
committed
feat(jdbc): add DatabaseStudent example to query students table
What - Implemented **DatabaseStudent** class showcasing how to fetch and display records from the `students` table using JDBC. - Steps included: - Loaded SQLite JDBC driver (`org.sqlite.JDBC`). - Established connection to `univ.db`. - Created a `Statement` object to run SQL queries. - Executed `SELECT * FROM students`. - Iterated through `ResultSet` to extract: - `roll` (int) - `name` (String) - `city` (String) - `deptno` (int) - Printed student details in a pipe-separated format. - Closed all JDBC resources properly (`ResultSet`, `Statement`, `Connection`). Why - Demonstrates retrieving **tabular data with multiple columns** via JDBC. - Provides a real example of iterating over rows and handling multiple data types. - Reinforces correct usage of column names (fixed typo from `nmae` to `name`). How to use - Ensure `univ.db` exists with a `students` table containing columns: `roll`, `name`, `city`, `deptno`. - Run program → prints all student records. Key Points - `Statement stm = con.createStatement();` allows execution of static queries. - `rs.getXxx("columnName")` retrieves column values by name with type safety. - Use `while (rs.next())` to iterate through result rows. - Always release resources (ResultSet, Statement, Connection) to prevent leaks. Real-life Applications - Fetching student records in university management systems. - Basis for building **CRUD operations** in academic or HR-related software. - Useful template for displaying multi-column results in reporting modules. Notes - This example is **read-only**; no inserts, updates, or deletes. - For parameterized queries (e.g., filtering by deptno), prefer **PreparedStatement**. - Code demonstrates **basic Statement usage** before moving to more secure query execution patterns. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8adccb0 commit a0c4fa3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Section28JDBCusingSQLite/JDBCProgram/src/DatabaseStudent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ public static void main(String[] args) {
88

99
// Establish the connection to your database.
1010
// Add a path. to yur DB
11-
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/univ.db");
11+
Connection con = DriverManager.getConnection("jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db");
1212

1313
// Create a statement object to execute the query
14-
Statement stm = con.createStatement(); //It Create-select statement.
14+
Statement stm = con.createStatement(); //It Creates-select statement.
1515

1616
// Execute the query (ensure the SQL statement is enclosed in quotes)
1717
ResultSet rs = stm.executeQuery("SELECT * FROM students");
@@ -36,4 +36,4 @@ public static void main(String[] args) {
3636
e.printStackTrace();
3737
}
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)