-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateStudent.java
42 lines (35 loc) · 1.05 KB
/
UpdateStudent.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
package JDBC_ManageStudent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class UpdateStudent {
public void update() {
Student student = new Student();
ConnectJDBC connectJdbc = new ConnectJDBC();
Connection connection = connectJdbc.getconnect();
String query = "UPDATE student SET name =?, age = ? where id = ?";
PreparedStatement pst = null;
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter id: ");
student.setId(sc.nextInt());
System.out.println("Enter new name: ");
student.setName(sc.next());
System.out.println("Enter new age: ");
student.setAge(sc.nextInt());
sc.close();
pst = connection.prepareStatement(query);
pst.setString(1, student.getName());
pst.setInt(2, student.getAge());
pst.setInt(3, student.getId());
int row = pst.executeUpdate();
if(row !=0) {
System.out.println("Updated sucess!");
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}