-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoJdbc.java
More file actions
57 lines (50 loc) · 1.74 KB
/
Copy pathDemoJdbc.java
File metadata and controls
57 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* Example: Execute Query and Process Result (Select One Record)
*
* Concept:
* When selecting data, we use executeQuery() instead of executeUpdate().
* executeQuery() returns a ResultSet object representing the database rows.
* We must call rs.next() to move the cursor to the first row before reading.
*
* Database Schema:
* CREATE DATABASE java_jdbc;
* USE java_jdbc;
* CREATE TABLE student (
* id INT PRIMARY KEY AUTO_INCREMENT,
* name VARCHAR(100),
* age INT,
* course VARCHAR(100)
* );
*
* Expected Output:
* Name of the student is: John Doe
*/
public class DemoJdbc {
public static void main(String[] args) throws Exception {
String url = "jdbc:postgresql://localhost:5432/java_jdbc";
String uname = "postgres";
String pass = "YOUR_PASSWORD";
// SQL query to fetch a specific record
String sql = "SELECT name FROM student WHERE id=101";
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
// executeQuery is used for SELECT statements
ResultSet rs = st.executeQuery(sql);
// rs.next() moves the cursor to the first row.
// It returns true if a row exists, false if empty.
if (rs.next()) {
// Retrieve the data using the column name or index
String name = rs.getString("name");
System.out.println("Name of the student is: " + name);
} else {
System.out.println("Student with ID 101 not found.");
}
rs.close();
st.close();
con.close();
}
}