-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2q4sa.java
More file actions
56 lines (53 loc) · 1.92 KB
/
Copy patha2q4sa.java
File metadata and controls
56 lines (53 loc) · 1.92 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
import java.util.*;
import java.sql.*;
public class a2q4sa {
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("Employees " + 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();
System.out.println("Enter a Contact");
String contact = sc.nextLine();
String insertQuery = "INSERT INTO employees VALUES (?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(insertQuery);
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, contact);
ps.executeUpdate();
}
String SelectQuery = "SELECT * FROM employees";
ResultSet rs = stmt.executeQuery(SelectQuery);
System.out.println("Employees Details \nID :\t Name : \t Address : \t Contact No : \n");
while (rs.next()) {
int Id = rs.getInt("id");
String Name = rs.getString("name");
String Address = rs.getString("address");
String Contact = rs.getString("contact");
System.out.println(String.format("| %s | %s | %s | %s |", Id, Name, Address, Contact));
}
con.close();
stmt.close();
ps.close();
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}