-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCPostgres.java
More file actions
61 lines (43 loc) · 1.77 KB
/
JDBCPostgres.java
File metadata and controls
61 lines (43 loc) · 1.77 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
57
58
59
60
61
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
/*
* This code works fine on any PC! We use Java + JDBC + Postgres DB from Supabase.
*/
public class JDBCPostgres {
static final synchronized public void main(String... abc) throws Exception {
// Take value from console
Scanner s = new Scanner(System.in);
System.out.print("Enter ID: ");
int id = s.nextInt();
System.out.print("Enter Name: ");
String name = s.next();
s.nextLine();
System.out.print("Enter Address: ");
String address = s.nextLine();
System.out.print("Enter Mobile Number: ");
long mobile = s.nextLong();
// JDBC connection!
// Step0 : Import Packages
// Step1 : Load the Driver
Class.forName("org.postgresql.Driver"); // PostgreSQL driver
System.out.println("Driver loaded successfully!");
// Step2 : Conn Establishment (Supabase connection string)
String url = "jdbc:postgresql://aws-0-ap-south-1.pooler.supabase.com:6543/postgres?user=postgres.yshxqhzwcekhvudlkaju&password=root";
Connection con = DriverManager.getConnection(url);
System.out.println("Connection Established!");
// Step3: Create PreparedStatement
PreparedStatement ps = con.prepareStatement("INSERT INTO student (id, name, address, mobile) VALUES (?, ?, ?, ?)");
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, address);
ps.setLong(4, mobile);
// Step4: Execute the query
int ch = ps.executeUpdate();
System.out.println(ch + " row(s) affected");
// Step5: Release the resources
ps.close();
con.close();
}
}