-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetStudent.java
44 lines (34 loc) · 1008 Bytes
/
GetStudent.java
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
package JDBC_ManageStudent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class GetStudent {
public void get() {
Student student = new Student();
// Enter value
Scanner sc = new Scanner(System.in);
System.out.println("Enter id: ");
student.setId(sc.nextInt());
sc.close();
// Connect to table
ConnectJDBC connectJdbc = new ConnectJDBC();
Connection connection = connectJdbc.getconnect();
String query = "SELECT * FROM student WHERE id=?";
PreparedStatement pst = null;
try {
pst = connection.prepareStatement(query);
pst.setInt(1, student.getId());
ResultSet rs = pst.executeQuery();
while(rs.next()) {
System.out.print("ID : " + student.getId());
System.out.print(", Name : " + rs.getString("name"));
System.out.println(", Age : " + rs.getInt("age"));
}
connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}