-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAPI.java
132 lines (116 loc) · 3.56 KB
/
MAPI.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package me.CopyableCougar4.main;
import java.sql.*;
import org.bukkit.plugin.java.JavaPlugin;
public class MAPI extends JavaPlugin {
/**
* Get a database connection
* @param ip
* @param database
* @param username
* @param password
* @return
*/
public static Connection getConnection(String ip, String database, String username, String password){
String url = "jdbc:mysql://" + ip + ":3306/";
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver).newInstance();
return DriverManager.getConnection(url+database, username, password);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return null;
}
public static void closeConnection(Connection conn) throws SQLException{
conn.close();
}
/**
* Inserts records into a given database connection
* @param table
* @param columns --> any strings should be surrounded in single quotes
* @param values --> any strings should be surrounded in single quotes
* @param databaseConnection
*/
public static void insert(String table, Object[] columns, Object[] values, Connection databaseConnection){
String cols = toString(columns);
String value = toString(values);
try {
Statement st = databaseConnection.createStatement();
st.executeQuery("INSERT INTO " + table + "(" + cols + ") VALUES (" + value + ")");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static String toString(Object[] array){
String result = "";
Integer count = 0;
for(Object o : array){
if(count == 0)
result = (String)o;
else
result += ", " + (String)o;
count++;
}
return result;
}
/**
* Selects a value from a database as an object. criteriaValue should be formatted as column>value
* @param databaseConnection
* @param column
* @param table
* @param criteriaValue
* @return
*/
public static Object select(Connection databaseConnection, String column, String table, String criteriaValue){
String[] criteria = criteriaValue.split(">");
Object result = null;
try {
Statement st = databaseConnection.createStatement();
ResultSet rs = st.executeQuery("SELECT " + column + " FROM " + table + "WHERE " + criteria[0] + "='" + criteria[1] + "'");
while(rs.next()){
result = rs.getObject(column);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* Updates a value in the database. criteria and change should be formatted as column>value
* @param databaseConnection
* @param table
* @param criteria
* @param change
* @return
*/
public static boolean update(Connection databaseConnection, String table, String criteria, String change){
try {
Statement st = databaseConnection.createStatement();
String[] criteriaV = criteria.split(">");
String[] update = change.split(">");
st.executeUpdate("UPDATE " + table + " WHERE " + criteriaV[0] + "=" + criteriaV[1] + " SET "+ update[0] + "=" + update[1]);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
/**
* Deletes a record from a database
* @param databaseConnection
* @param table
* @param criteria
* @return
*/
public static boolean delete(Connection databaseConnection, String table, String criteria){
String[] caseValue = criteria.split(">");
try {
Statement st = databaseConnection.createStatement();
st.executeQuery("DELETE FROM " + table + " WHERE " + caseValue[0] + "=" + caseValue[1]);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}