Skip to content

Commit 91160db

Browse files
committed
Version 2.0.0
Implemented Builders Added Multiple Requests (requested by @MayusYT)
1 parent 6305e33 commit 91160db

File tree

9 files changed

+351
-28
lines changed

9 files changed

+351
-28
lines changed

README.md

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![](https://jitpack.io/v/Tallerik/MySQL-API.svg)](https://jitpack.io/#Tallerik/MySQL-API)
12
# MySQL-API
23
Simple Java API for MySQL
34

@@ -16,7 +17,7 @@ Simple Java API for MySQL
1617
<dependency>
1718
<groupId>com.github.Tallerik</groupId>
1819
<artifactId>MySQL-API</artifactId>
19-
<version>0ae2e72c</version>
20+
<version>2.0.0</version>
2021
</dependency>
2122
</dependencies>
2223
```
@@ -30,7 +31,7 @@ allprojects {
3031
}
3132
3233
dependencies {
33-
implementation 'com.github.Tallerik:MySQL-API:0ae2e72c'
34+
implementation 'com.github.Tallerik:MySQL-API:2.0.0'
3435
}
3536
```
3637

@@ -65,20 +66,48 @@ boolean: isDebug();
6566
```
6667

6768
### MySQL Data interaction
69+
**Default way**
6870
```Java
6971
boolean: sql.tableInsert("myTable", "name, age", "Robert", "32"); // INSERT Statement
7072
boolean: sql.rowUpdate("myTable", new UpdateValue("age", "45"), "name = 'Robert'"); // UPDATE Statement
71-
CachedRowSetImpl: sql.rowSelect("myTable", "*", "name = 'Robert'"); // SELECT Statement
73+
Result: sql.rowSelect("myTable", "*", "name = 'Robert'"); // SELECT Statement
7274
boolean: sql.custom("DELETE * FROM myTable;"); // Custom SQL Statement
7375
```
74-
**Tip**
75-
`CachedRowSetImpl` is like ResultSet
76+
77+
**Request builder**
78+
```java
79+
// sql.tableInsert();
80+
Insert ins = new Insert();
81+
ins.setTable("myTable");
82+
ins.setColumns("column1, column2");
83+
ins.setData("value1", "value2");
84+
boolean: sql.tableInsert(ins); // Multiple Builders accepted (sql.tableInsert(ins, ins2, ins3)
85+
86+
87+
// sql.rowUpdate();
88+
Update up = new Update();
89+
up.setTable("myTable");
90+
up.setValue(new UpdateValue("column1", "value3"));
91+
up.setFilter("column2 = 'value2'");
92+
boolean: sql.rowUpdate(up); // Multiple Builders accepted (sql.rowUpdate(up, up2, up3)
93+
94+
95+
// sql.rowSelect();
96+
Select select = new Select();
97+
select.setTable("myTable");
98+
select.setColumns("*"); // Optional default '*'
99+
select.setFilter(""); // Optional default ''
100+
Result: sql.rowSelect(select); // Only one Builder accepted!
101+
```
102+
103+
104+
#### Result
105+
76106
```Java
77-
CachedRowSetImpl res = sql.rowSelect(...);
78-
while(res.next()) {
79-
System.out.PrintLn(res.getString("myValue"));
107+
List<Row> rowList = res.getRows();
108+
for(Row r : rowList) {
109+
System.out.println(r.get("column1") + " " + r.get("column2"));
80110
}
81-
82111
```
83112

84113
### Close Connection

pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
<groupId>de.tallerik</groupId>
88
<artifactId>mysql-api</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
1012

13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<version>3.8.0</version>
17+
<configuration>
18+
<source>1.8</source>
19+
<target>1.8</target>
20+
</configuration>
21+
</plugin>
22+
23+
</plugins>
24+
</build>
1125
<dependencies>
1226
<dependency>
1327
<groupId>mysql</groupId>

src/main/java/de/tallerik/MySQL.java

+143-16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package de.tallerik;
22

33
import com.mysql.cj.jdbc.MysqlDataSource;
4-
import com.sun.rowset.CachedRowSetImpl;
4+
import de.tallerik.utils.*;
55

66
import javax.sql.DataSource;
7-
import java.sql.Connection;
8-
import java.sql.ResultSet;
9-
import java.sql.SQLException;
10-
import java.sql.Statement;
7+
import java.sql.*;
118

12-
@SuppressWarnings("Duplicates")
9+
@SuppressWarnings("Duplicates unused")
1310
public class MySQL {
1411

1512
// Vars
@@ -66,14 +63,14 @@ public boolean connect() {
6663
dataSource.setDatabaseName(db);
6764
dataSource.setUser(user);
6865
dataSource.setPassword(password);
69-
66+
dataSource.setAllowMultiQueries(true);
7067
con = dataSource.getConnection();
7168
System.out.println("Connection established");
7269
return true;
7370
}
7471
catch(SQLException ex)
7572
{
76-
System.out.println(ex);
73+
ex.printStackTrace();
7774
return false;
7875
}
7976
}
@@ -133,6 +130,43 @@ public boolean tableInsert(String table, String columns, String... data) {
133130
}
134131
return false;
135132
}
133+
public boolean tableInsert(Insert... builders) {
134+
String sql = "";
135+
for (Insert b : builders) {
136+
String sqldata = "";
137+
int i = 0;
138+
for (String d : b.getData()) {
139+
sqldata = sqldata + "'" + d + "'";
140+
i++;
141+
if(i != b.getData().length) {
142+
sqldata = sqldata + ", ";
143+
}
144+
}
145+
146+
147+
sql = sql + "INSERT INTO " + b.getTable() + " (" + b.getColumns() + ") VALUES (" + sqldata + "); ";
148+
149+
}
150+
Statement stmt = null;
151+
try {
152+
stmt = con.createStatement();
153+
stmt.execute(sql);
154+
155+
} catch (SQLException e) {
156+
e.printStackTrace();
157+
} finally {
158+
if(stmt != null) {
159+
try {
160+
stmt.close();
161+
return true;
162+
} catch (SQLException e) {
163+
e.printStackTrace();
164+
}
165+
}
166+
}
167+
return false;
168+
}
169+
136170
public boolean rowUpdate(String table, UpdateValue value, String filter) {
137171
String change = "";
138172
int i = 0;
@@ -162,7 +196,40 @@ public boolean rowUpdate(String table, UpdateValue value, String filter) {
162196
}
163197
return false;
164198
}
165-
public CachedRowSetImpl rowSelect(String table, String columns, String filter) {
199+
public boolean rowUpdate(Update... builders) {
200+
String sql = "";
201+
for (Update u : builders) {
202+
String change = "";
203+
int i = 0;
204+
for(String key : u.getValue().getKeys()) {
205+
change = change + key + " = '" + u.getValue().get(key) + "'";
206+
i++;
207+
if(i != u.getValue().getKeys().size()) {
208+
change = change + ", ";
209+
}
210+
}
211+
sql = sql + "UPDATE " + u.getTable() + " SET " + change + " WHERE " + u.getFilter() + "; ";
212+
}
213+
Statement stmt = null;
214+
try {
215+
stmt = con.createStatement();
216+
stmt.execute(sql);
217+
} catch (SQLException e) {
218+
e.printStackTrace();
219+
} finally {
220+
if (stmt != null) {
221+
try {
222+
stmt.close();
223+
return true;
224+
} catch (SQLException e) {
225+
e.printStackTrace();
226+
}
227+
}
228+
}
229+
return false;
230+
}
231+
232+
public Result rowSelect(String table, String columns, String filter) {
166233
if(columns == null || columns.equals("")) {
167234
columns = "*";
168235
}
@@ -172,21 +239,81 @@ public CachedRowSetImpl rowSelect(String table, String columns, String filter) {
172239
}
173240
sql = sql + ";";
174241

175-
Statement stmt = null;
176-
ResultSet res = null;
242+
Statement stmt;
243+
ResultSet res;
177244
try {
178245
stmt = con.createStatement();
179246
res = stmt.executeQuery(sql);
180-
CachedRowSetImpl crs = new CachedRowSetImpl();
181-
crs.populate(res);
182-
stmt.close();
183-
return crs;
247+
ResultSetMetaData resmeta = res.getMetaData();
248+
Result result = new Result();
249+
while(res.next()) {
250+
Row row = new Row();
251+
int i = 1;
252+
boolean bound = true;
253+
while (bound) {
254+
try {
255+
row.addcolumn(resmeta.getColumnName(i), res.getObject(i));
256+
} catch (SQLException e) {
257+
bound = false;
258+
}
259+
260+
i++;
261+
}
262+
result.addrow(row);
263+
}
264+
return result;
184265

185266
} catch (SQLException e) {
186267
e.printStackTrace();
268+
return new Result();
187269
}
188-
return null;
189270
}
271+
public Result rowSelect(Select s) {
272+
String sql = "";
273+
String columns;
274+
String lsql;
275+
if(s.getColumns() == null || s.getColumns().equals("")) {
276+
columns = "*";
277+
} else {
278+
columns = s.getColumns();
279+
}
280+
lsql = "# noinspection SqlResolveForFile
281+
282+
SELECT " + columns + " FROM " + s.getTable();
283+
if(s.getFilter() != null && !s.getFilter().equals("")) {
284+
lsql = lsql + " WHERE " + s.getFilter();
285+
}
286+
lsql = lsql + "; ";
287+
sql = sql + lsql;
288+
289+
Statement stmt;
290+
ResultSet res;
291+
try {
292+
stmt = con.createStatement();
293+
res = stmt.executeQuery(sql);
294+
ResultSetMetaData resmeta = res.getMetaData();
295+
Result result = new Result();
296+
while(res.next()) {
297+
Row row = new Row();
298+
int i = 1;
299+
boolean bound = true;
300+
while (bound) {
301+
try {
302+
row.addcolumn(resmeta.getColumnName(i), res.getObject(i));
303+
} catch (SQLException e) {
304+
bound = false;
305+
}
306+
i++;
307+
}
308+
result.addrow(row);
309+
}
310+
return result;
311+
} catch (SQLException e) {
312+
e.printStackTrace();
313+
return new Result();
314+
}
315+
}
316+
190317
public boolean custom(String sql) {
191318
Statement stmt = null;
192319
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.tallerik.utils;
2+
3+
import java.util.List;
4+
5+
6+
@SuppressWarnings("Duplicates unused")
7+
public class Insert {
8+
private String table = "";
9+
private String columns = "";
10+
private String[] data = null;
11+
public Insert(String table, String columns, String... data) {
12+
this.table = table;
13+
this.columns = columns;
14+
this.data = data;
15+
}
16+
public Insert() {}
17+
public void setColumns(String columns) {
18+
this.columns = columns;
19+
}
20+
public void setData(String... data) {
21+
this.data = data;
22+
}
23+
public void setData(List<String> data) {
24+
this.data = (String[]) data.toArray();
25+
}
26+
public void setTable(String table) {
27+
this.table = table;
28+
}
29+
public String getColumns() {
30+
return columns;
31+
}
32+
public String getTable() {
33+
return table;
34+
}
35+
public String[] getData() {
36+
return data;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.tallerik.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
@SuppressWarnings("Duplicates unused")
8+
public class Result {
9+
private List<Row> rows = new ArrayList<>();
10+
public Result(){}
11+
public void addrow(Row row) {
12+
rows.add(row);
13+
}
14+
15+
public List<Row> getRows() {
16+
return rows;
17+
}
18+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.tallerik.utils;
2+
3+
import java.util.HashMap;
4+
import java.util.Set;
5+
6+
7+
@SuppressWarnings("Duplicates unused")
8+
public class Row {
9+
private HashMap<String, Object> content = new HashMap<>();
10+
public Row(){}
11+
public void addcolumn(String name, Object content) {
12+
this.content.put(name, content);
13+
}
14+
15+
public HashMap<String, Object> getColumns() {
16+
return content;
17+
}
18+
public Object get(String key) {
19+
return content.get(key);
20+
}
21+
public Set<String> getKeys() {
22+
return content.keySet();
23+
}
24+
}

0 commit comments

Comments
 (0)