-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.java
50 lines (42 loc) · 1.81 KB
/
Demo.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
43
44
45
46
47
48
49
50
import java.sql.*;
public class Demo {
public static ResultSet queryExecution(Connection conn, String query) {
Statement st;
ResultSet rs = null;
try {
st = conn.createStatement();
rs = st.executeQuery(query);
} catch (SQLException e) {
System.out.println("Error when executing query.");
}
return rs;
}
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/pagila?user=postgres&password=SuperSecret";
String query = "select title, rental_rate from film";
try {
Connection conn = DriverManager.getConnection(url);
System.out.println("Welcome to Pagila Reports!\n\n");
System.out.println("Tile | Rental Rate");
System.out.println("---------------------------------------");
ResultSet rs = queryExecution(conn, query);
float sum = 0.0f;
try {
while (rs.next()) {
float rental_rate = Float.parseFloat(rs.getString(2));
String output = String.format("%-25s : %11.2f", rs.getString(1), rental_rate);
sum += rental_rate;
System.out.println(output);
}
} catch (NumberFormatException e) {
System.out.println("Unexpected values in result set.");
} catch (SQLException e) {
System.out.println("Error processing result set");
}
System.out.println("---------------------------------------");
System.out.println(String.format("%-25s : %11.2f", "Total", sum));
} catch (SQLException e) {
System.out.println("ERROR: Could not connect to the database.\\n");
}
}
}