-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2q3sa.java
More file actions
46 lines (44 loc) · 1.59 KB
/
Copy patha2q3sa.java
File metadata and controls
46 lines (44 loc) · 1.59 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
import java.util.*;
import java.sql.*;
public class a2q3sa {
public static void main (String[] args) {
String url = "jdbc:postgresql://localhost:5432/java";
String user = "postgres";
String pass = "12345";
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement stmt = con.createStatement();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a No to Insert No of records");
int num = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= num; i++) {
System.out.println("Student " + i + " Details");
System.out.println("Enter a ID");
int id = sc.nextInt();
sc.nextLine();
System.out.println("Enter a Name");
String name = sc.nextLine();
System.out.println("Enter a Address");
String address = sc.nextLine();
String insertQuery = String.format("INSERT INTO students VALUES (%s, '%s', '%s');",id, name, address);
stmt.executeUpdate(insertQuery);
}
String selectQuery = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(selectQuery);
System.out.println("Students Details \nID :\t Name : \t Address : \n");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String address = rs.getString("address");
System.out.println(String.format("| %s | %s | %s |", id, name, address));
}
con.close();
stmt.close();
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}