-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateTable.java
42 lines (35 loc) · 1.12 KB
/
UpdateTable.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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class UpdateTable {
private static Connection conn;
private static PreparedStatement pstmt;
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/demo","postgres","tiger");
String q = "UPDATE student set name=? WHERE id =?";
pstmt = conn.prepareStatement(q);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the new Name :");
String name = br.readLine();
System.out.println("Enter the id:");
int id = Integer.parseInt(br.readLine());
pstmt.setString(1,name);
pstmt.setInt(2,id);
pstmt.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(conn != null)conn.close();
if(pstmt != null)pstmt.close();
System.out.println("sucessfully closed connections...");
}catch (Exception e) {
e.printStackTrace();
}
}
}
}