-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAndDAO.java
63 lines (52 loc) · 2.1 KB
/
AndDAO.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
package DAO;
import java.sql.*;
public class AndDAO {
private static String url = "jdbc:mysql://127.0.0.1:3306/andorid?serverTimezone=UTC";
private static String user = "root";
private static String password = "19970426";
public static void add(String receive, String response) {
String sql = "INSERT INTO andorid (receive,response) VALUES(?,?)";
try(Connection connection = DriverManager.getConnection(url,user,password)){
Class.forName("com.mysql.jdbc.Driver");
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,receive);
ps.setString(2,response);
ps.execute();
}catch (ClassNotFoundException | SQLException e){
e.printStackTrace();
}
}
public static void delete(int id) {
String sql = "DELETE FROM TABLE andorid WHERE id=?";
try(Connection connection = DriverManager.getConnection(url,user,password)){
Class.forName("com.mysql.jdbc.Driver");
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,id);
ps.execute();
}catch (ClassNotFoundException | SQLException e){
e.printStackTrace();
}
}
public static String search(String text) {
String sql = "SELECT receive,response FROM andorid";
String comman = "i do not know";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
try(Connection connection = DriverManager.getConnection(url,user,password)){
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
String result = rs.getString("receive");
if(text.equals(result)){
comman = rs.getString("response");
}
}
}catch (SQLException e){
e.printStackTrace();
}
return comman;
}
}