Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Connection databaseConnection;
- private long lastUsed;
- private boolean isUsed;
- public DatabaseClient(Connection connection) {
- this.databaseConnection = connection;
- this.setLastUsed(System.currentTimeMillis());
- this.isUsed = true;
- }
- // SQL Executers
- public void executeSQL(String SQL, Object... sts) {
- try {
- PreparedStatement statement = this.databaseConnection.prepareStatement(SQL);
- int count = 0;
- for(int i = 0; i < sts.length; i++) {
- if(sts[i] instanceof String) {
- statement.setString(++count, (String)sts[i]);
- } else if (sts[i] instanceof Integer) {
- statement.setInt(++count, (Integer)sts[i]);
- } else if(sts[i] instanceof Short) {
- statement.setShort(++count, (Short)sts[i]);
- } else if(sts[i] instanceof Double) {
- statement.setDouble(++count, (Double)sts[i]);
- } else if(sts[i] instanceof Long) {
- statement.setLong(++count, (Long)sts[i]);
- } else {
- statement.setObject(++count, sts[i]);
- }
- }
- statement.execute();
- statement.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public ResultSet executeQuery(String SQL, Object... sts) {
- try {
- PreparedStatement statement = this.databaseConnection.prepareStatement(SQL);
- int count = 0;
- for(int i = 0; i < sts.length; i++) {
- if(sts[i] instanceof String) {
- statement.setString(++count, (String)sts[i]);
- } else if (sts[i] instanceof Integer) {
- statement.setInt(++count, (Integer)sts[i]);
- } else if(sts[i] instanceof Short) {
- statement.setShort(++count, (Short)sts[i]);
- } else if(sts[i] instanceof Double) {
- statement.setDouble(++count, (Double)sts[i]);
- } else if(sts[i] instanceof Long) {
- statement.setLong(++count, (Long)sts[i]);
- } else {
- statement.setObject(++count, sts[i]);
- }
- }
- return statement.executeQuery();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- // //
- // OTHERS
- public void setLastUsed(long lastUsedInMs) {
- this.lastUsed = lastUsedInMs;
- }
- public long getLastUsed() {
- return this.lastUsed;
- }
- public void finish() {
- this.isUsed = false;
- }
- public void restart() {
- this.setLastUsed(System.currentTimeMillis());
- this.isUsed = true;
- }
- public boolean isUsed() {
- return this.isUsed;
- }
- public void dispose() {
- this.setLastUsed(0);
- try {
- this.databaseConnection.close();
- this.databaseConnection = null;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // //
Advertisement
Add Comment
Please, Sign In to add comment