Commit a0c4fa3
committed
feat(jdbc): add DatabaseStudent example to query
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]>students table1 parent 8adccb0 commit a0c4fa3
File tree
1 file changed
+3
-3
lines changed- Section28JDBCusingSQLite/JDBCProgram/src
1 file changed
+3
-3
lines changedLines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
0 commit comments