-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadDatabase.java
78 lines (62 loc) · 2.36 KB
/
ReadDatabase.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sample;
import javafx.collections.ObservableList;
import java.sql.*;
public class ReadDatabase {
private Connection connect() {
String url = "jdbc:sqlite:Dictionaries.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void selectAll(ObservableList<Word> words){
String sql = "SELECT word, info FROM Dictionary";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
while (rs.next()) {
Word newWord = new Word();
newWord.setWord_target(rs.getString("word"));
newWord.setWord_explain(rs.getString("info"));
words.addAll(newWord);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void insert(String word, String info) {
String sql = "INSERT INTO Dictionary(word,info) VALUES(?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, word);
pstmt.setString(2, info);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void delete(String word) {
String sql = "DELETE FROM Dictionary WHERE word = ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, word);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void update(String word, String info) {
String sql = "UPDATE Dictionary SET info = ? " + "WHERE word = ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(2, word);
pstmt.setString(1, info);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}