Added a JSON parser and a new DB connection handler and pool
This commit is contained in:
parent
952a388cf1
commit
6ad303a948
6 changed files with 785 additions and 1 deletions
108
src/zutil/db/MySQLConnection.java
Normal file
108
src/zutil/db/MySQLConnection.java
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package zutil.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class MySQLConnection {
|
||||
Connection conn = null;
|
||||
|
||||
/**
|
||||
* Connects to a MySQL server
|
||||
*
|
||||
* @param url is the URL of the MySQL server
|
||||
* @param db is the database to connect to
|
||||
* @param user is the user name
|
||||
* @param password is the password
|
||||
*/
|
||||
public MySQLConnection(String url, String db, String user, String password)
|
||||
throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
|
||||
Class.forName ("com.mysql.jdbc.Driver").newInstance();
|
||||
DriverManager.setLoginTimeout(10);
|
||||
conn = DriverManager.getConnection ("jdbc:mysql://"+url+"/"+db, user, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a query and returns the result.<br>
|
||||
* <b>NOTE:</b> Don't forget to close the ResultSet and the Statement or it
|
||||
* can lead to memory leak: rows.getStatement().close();
|
||||
*
|
||||
* @param sql is the query to execute
|
||||
* @return the data that the DB returned
|
||||
*/
|
||||
public synchronized ResultSet query(String sql) throws SQLException{
|
||||
Statement s = conn.createStatement ();
|
||||
s.executeQuery(sql);
|
||||
return s.getResultSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first cell of the first row of the query
|
||||
*
|
||||
* @param sql is the SQL query to run, preferably with the LIMIT 1 at the end
|
||||
* @return A SQL row if it exists or else null
|
||||
*/
|
||||
public synchronized String simpleQuery(String sql) throws SQLException{
|
||||
Statement s = conn.createStatement ();
|
||||
s.executeQuery(sql);
|
||||
ResultSet result = s.getResultSet();
|
||||
if(result.next()){
|
||||
String tmp = result.getString(1);
|
||||
result.close();
|
||||
return tmp;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a query in the MySQL server and returns effected rows
|
||||
*
|
||||
* @param sql is the query to execute
|
||||
* @return the number of rows effected
|
||||
*/
|
||||
public synchronized int updateQuery(String sql) throws SQLException{
|
||||
Statement s = conn.createStatement ();
|
||||
int ret = s.executeUpdate(sql);
|
||||
s.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the last inserted id or -1 if there was an error
|
||||
* @throws SQLException
|
||||
*/
|
||||
public int getLastInsertID() throws SQLException{
|
||||
Statement s = conn.createStatement ();
|
||||
s.executeQuery("SELECT LAST_INSERT_ID()");
|
||||
ResultSet result = s.getResultSet();
|
||||
if(result.next()){
|
||||
int tmp = result.getInt(1);
|
||||
result.close();
|
||||
return tmp;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a Prepared Statement.<br>
|
||||
* <b>NOTE:</b> Don't forget to close the PreparedStatement or it can lead to memory leak
|
||||
*
|
||||
* @param sql is the SQL query to run
|
||||
* @return The PreparedStatement
|
||||
*/
|
||||
public synchronized PreparedStatement prepareStatement(String sql) throws SQLException{
|
||||
return conn.prepareStatement(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the database
|
||||
*/
|
||||
public synchronized void close() throws SQLException{
|
||||
if (conn != null){
|
||||
conn.close ();
|
||||
}
|
||||
}
|
||||
}
|
||||
181
src/zutil/db/MySQLQueue.java
Normal file
181
src/zutil/db/MySQLQueue.java
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
package zutil.db;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
|
||||
import zutil.MultiPrintStream;
|
||||
import zutil.converters.Converter;
|
||||
|
||||
/**
|
||||
* This class creates a queue that stors the
|
||||
* data in a mysql table.
|
||||
* The table should look like this:
|
||||
* CREATE TABLE `queue` (
|
||||
* `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
* `data` BINARY NOT NULL
|
||||
* );
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class MySQLQueue<E> implements Queue<E>{
|
||||
// GO TO KNOW = SELECT LAST_INSERT_ID() as pos_id
|
||||
private MySQLConnection db;
|
||||
private String table;
|
||||
|
||||
/**
|
||||
* Initiats the queue.
|
||||
* WARNING!! this will erase all rows i the table
|
||||
* @param db The connection to the db
|
||||
* @param table The name of the table
|
||||
* @throws SQLException
|
||||
*/
|
||||
public MySQLQueue(MySQLConnection db, String table){
|
||||
this.db = db;
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public boolean add(Object arg0){
|
||||
try {
|
||||
PreparedStatement sql = db.prepareStatement("INSERT INTO "+table+" (data) VALUES(?)");
|
||||
sql.setObject(1, arg0);
|
||||
sql.executeUpdate();
|
||||
sql.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public E element() {
|
||||
return peek();
|
||||
}
|
||||
|
||||
public boolean offer(Object arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized E peek() {
|
||||
try {
|
||||
ResultSet rs = db.query("SELECT * FROM "+table+" LIMIT 1");
|
||||
if (rs.next()) {
|
||||
return (E) Converter.toObject(rs.getBytes("data"));
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized E poll() {
|
||||
try {
|
||||
ResultSet rs = db.query("SELECT * FROM "+table+" LIMIT 1");
|
||||
if (rs.next()) {
|
||||
db.updateQuery("DELETE FROM "+table+" WHERE id="+rs.getInt("id")+" LIMIT 1");
|
||||
return (E) Converter.toObject(rs.getBytes("data"));
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
return poll();
|
||||
}
|
||||
|
||||
public boolean addAll(Collection<? extends E> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
try {
|
||||
db.updateQuery("TRUNCATE TABLE `"+table+"`");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(Object arg0) {
|
||||
try {
|
||||
ResultSet rs = db.query("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
|
||||
if (rs.next()) {
|
||||
return true;
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containsAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return (peek() != null);
|
||||
}
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized boolean remove(Object arg0) {
|
||||
try {
|
||||
ResultSet rs = db.query("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
|
||||
rs.getStatement().close();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean removeAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
try {
|
||||
ResultSet rs = db.query("SELECT count(*) FROM "+table);
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(MultiPrintStream.out);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public E[] toArray() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E[] toArray(Object[] arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue