Fixed some bugs

This commit is contained in:
Ziver Koc 2009-09-16 21:29:22 +00:00
parent 73be002969
commit 19a0e92d88
4 changed files with 126 additions and 62 deletions

View file

@ -18,24 +18,41 @@ public class MySQLConnection {
* @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{
public MySQLConnection(String url, String db, String user, String password)
throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
Class.forName ("com.mysql.jdbc.Driver").newInstance();
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 tex: rows.getStatement().close();
* <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 returnQuery(String sql) throws SQLException{
public synchronized ResultSet query(String sql) throws SQLException{
Statement s = conn.createStatement ();
s.executeQuery (sql);
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())
return result.getString(0);
return null;
}
/**
* Runs a query in the MySQL server and returns effected rows
*
@ -52,6 +69,7 @@ public class MySQLConnection {
/**
* 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
*/

View file

@ -63,7 +63,7 @@ public class MySQLQueue<E> implements Queue<E>{
@SuppressWarnings("unchecked")
public synchronized E peek() {
try {
ResultSet rs = db.returnQuery("SELECT * FROM "+table+" LIMIT 1");
ResultSet rs = db.query("SELECT * FROM "+table+" LIMIT 1");
if (rs.next()) {
return (E) Converter.toObject(rs.getBytes("data"));
}
@ -77,7 +77,7 @@ public class MySQLQueue<E> implements Queue<E>{
@SuppressWarnings("unchecked")
public synchronized E poll() {
try {
ResultSet rs = db.returnQuery("SELECT * FROM "+table+" LIMIT 1");
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"));
@ -108,7 +108,7 @@ public class MySQLQueue<E> implements Queue<E>{
public boolean contains(Object arg0) {
try {
ResultSet rs = db.returnQuery("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
ResultSet rs = db.query("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
if (rs.next()) {
return true;
}
@ -135,7 +135,7 @@ public class MySQLQueue<E> implements Queue<E>{
public synchronized boolean remove(Object arg0) {
try {
ResultSet rs = db.returnQuery("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
ResultSet rs = db.query("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
rs.getStatement().close();
return true;
} catch (Exception e) {
@ -156,7 +156,7 @@ public class MySQLQueue<E> implements Queue<E>{
public int size() {
try {
ResultSet rs = db.returnQuery("SELECT count(*) FROM "+table);
ResultSet rs = db.query("SELECT count(*) FROM "+table);
if (rs.next()) {
return rs.getInt(1);
}