Skip to content

Instantly share code, notes, and snippets.

@loriopatrick
Created January 18, 2013 05:16
Show Gist options
  • Save loriopatrick/4562510 to your computer and use it in GitHub Desktop.
Save loriopatrick/4562510 to your computer and use it in GitHub Desktop.
Something I always use when handling databases in Java. Has not been throughly tested.
import java.sql.*;
import java.util.Date;
import java.sql.Connection;
import java.util.ArrayList;
public class Database {
public Database(String server, String port, String user, String pass, boolean connect) {
this.server = server;
this.port = port;
this.user = user;
this.pass = pass;
if (connect) {
try {
connect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Connection con = null;
private String server;
private String port;
private String user;
private String pass;
protected boolean connected = false;
private float lastCall = 0;
public boolean check() {
try {
con.createStatement().execute("SELECT 1");
return false;
} catch (Exception e) {
try {
connect();
long time = new Date().getTime();
if (time - lastCall < 3600) return false;
lastCall = time;
return true;
} catch (Exception e2) {
e.printStackTrace();
return false;
}
}
}
public void connect() throws SQLException {
if (con != null) disconnect();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port, user, pass);
connected = true;
}
public void disconnect() throws SQLException {
if (con == null) return;
con.close();
con = null;
connected = false;
}
public boolean execute(String command) throws SQLException {
try {
return con.createStatement().execute(command);
} catch (SQLException e) {
if (check()) {
return execute(command);
}
throw new SQLException(e);
}
}
public boolean execute(String command, Object... values) throws SQLException {
try {
PreparedStatement statement = con.prepareStatement(command);
for (int i = 0; i < values.length; ++i) {
statement.setObject(i + 1, values[i]);
}
return statement.execute();
} catch (SQLException e) {
if (check()) {
return execute(command, values);
}
throw new SQLException(e);
}
}
public boolean createDatabase(String name) throws SQLException {
return execute("CREATE DATABASE ?", name);
}
public int update(String command, Object... values) throws SQLException {
try {
PreparedStatement statement = con.prepareStatement(command, Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < values.length; ++i) {
statement.setObject(i + 1, values[i]);
}
statement.executeUpdate();
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
return rs.getInt(1);
}
return -1;
} catch (SQLException e) {
if (check()) {
return update(command, values);
}
throw new SQLException(e);
}
}
public int count(String command, Object... values) throws SQLException {
try {
PreparedStatement statement = con.prepareStatement(command);
for (int i = 0; i < values.length; ++i) {
statement.setObject(i + 1, values[i]);
}
statement.executeQuery();
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
return rs.getInt(1);
}
return -1;
} catch (SQLException e) {
if (check()) {
return count(command, values);
}
throw new SQLException(e);
}
}
public ArrayList<String[]> query(String command, Object[] data, String[] fields) throws SQLException {
try {
PreparedStatement p = con.prepareStatement(command);
for (int i = 0; i < data.length; ++i) {
p.setObject(i + 1, data[i]);
}
return query(p, fields);
} catch (SQLException e) {
if (check()) {
return query(command, data, fields);
}
throw new SQLException(e);
}
}
public ArrayList<String[]> query(String command, String[] fields) throws SQLException {
try {
return query(con.createStatement().executeQuery(command), fields);
} catch (SQLException e) {
if (check()) {
return query(command, fields);
}
throw new SQLException(e);
}
}
public ArrayList<String[]> query(PreparedStatement statement, String[] fields) throws SQLException {
try {
return query(statement.executeQuery(), fields);
} catch (SQLException e) {
if (check()) {
return query(statement, fields);
}
throw new SQLException(e);
}
}
public ArrayList<String[]> query(ResultSet rs, String[] fields) throws SQLException {
ArrayList<String[]> result = new ArrayList<String[]>();
while (rs.next()) {
String[] temp = new String[fields.length];
for (int i = 0; i < temp.length; ++i) {
temp[i] = rs.getString(fields[i]);
}
result.add(temp);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment