-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQ23_Retrieve_DB_data.java
53 lines (44 loc) · 1.37 KB
/
Q23_Retrieve_DB_data.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
51
52
53
//Q23. WAP to retrieve data from database.
package JAVA_Lab_File;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Q23_Retrieve_DB_data {
private Connection connect() {
String url = "jdbc:sqlite:Logins.Sqlite3";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void selectAll(){
String sql = "SELECT * FROM Members";
try {
Statement stmt;
try (Connection conn = this.connect()) {
stmt = conn.createStatement();
}
ResultSet rs = stmt.executeQuery(sql);
// loop through the result set
while (rs.next()) {
System.out.println(rs.getInt("id") + "\t" +
rs.getString("name") + "\t" +
rs.getDouble("capacity"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Q23_Retrieve_DB_data app = new Q23_Retrieve_DB_data();
app.selectAll();
}
}