Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
63 lines (55 loc) · 1.73 KB
/
Copy pathDatabase.java
File metadata and controls
63 lines (55 loc) · 1.73 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
62
63
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Database {
private String host;
private int port;
private String username;
private String password;
private String database;
public Database(String host, String username, String password, String database) {
this.host = host;
this.port = 3306;
this.username = username;
this.password = password;
this.database = database;
}
public Database(String host, int port, String username, String password, String database){
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.database = database;
}
public Query table(String name){
return new Query(this, name);
}
//Direct query
public void execute(String query) {
try {
Connection connection = newConnection();
connection.createStatement().execute(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet executeQuery(String query){
try {
Connection connection = newConnection();
ResultSet resultSet = connection.createStatement().executeQuery(query);
return resultSet;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private Connection newConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}