-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
62 lines (51 loc) · 1.55 KB
/
Copy pathDatabase.java
File metadata and controls
62 lines (51 loc) · 1.55 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
Connection con;
Database(String dbName, String username, String password) {
// this URL is used to find the database (that might be running on a server, your computer, etc.)
String url = "jdbc:mysql://localhost:3306/" + dbName;
try {
//use the mysql jdbc driver to access the mysql database in the most intuitive way
Class.forName("com.mysql.cj.jdbc.Driver");
// connect to the database with the given credentials
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public void UpdateDatabase(String command) {
try {
//creates a statement that will be send to the database
Statement stat = con.createStatement();
//execute update are the commands we would run in the terminal
stat.executeUpdate(command);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet runSQL(String sql) {
try {
//creates a statement
Statement stat = con.createStatement();
//stores the result of the executed SQL Query
ResultSet rs = stat.executeQuery(sql);
//returns the result
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void CloseConnection() {
try {
//closes the connection to the database
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}