Advertisement
forextheblack

DatabaseClient

Apr 8th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1.     private Connection databaseConnection;
  2.     private long lastUsed;
  3.     private boolean isUsed;
  4.    
  5.     public DatabaseClient(Connection connection) {
  6.         this.databaseConnection = connection;
  7.         this.setLastUsed(System.currentTimeMillis());
  8.         this.isUsed = true;
  9.     }
  10.    
  11.    
  12.     // SQL Executers
  13.     public void executeSQL(String SQL, Object... sts) {
  14.         try {
  15.             PreparedStatement statement = this.databaseConnection.prepareStatement(SQL);
  16.                
  17.             int count = 0;
  18.             for(int i = 0; i < sts.length; i++) {
  19.                 if(sts[i] instanceof String) {
  20.                     statement.setString(++count, (String)sts[i]);
  21.                 } else if (sts[i] instanceof Integer) {
  22.                     statement.setInt(++count, (Integer)sts[i]);
  23.                 } else if(sts[i] instanceof Short) {
  24.                     statement.setShort(++count, (Short)sts[i]);
  25.                 } else if(sts[i] instanceof Double) {
  26.                     statement.setDouble(++count, (Double)sts[i]);
  27.                 } else if(sts[i] instanceof Long) {
  28.                     statement.setLong(++count, (Long)sts[i]);
  29.                 } else {                 
  30.                     statement.setObject(++count, sts[i]);
  31.                 }                
  32.             }
  33.             statement.execute();
  34.             statement.close();
  35.         } catch (Exception e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39.        
  40.     public ResultSet executeQuery(String SQL, Object... sts) {
  41.         try {
  42.             PreparedStatement statement = this.databaseConnection.prepareStatement(SQL);
  43.                  
  44.             int count = 0;
  45.             for(int i = 0; i < sts.length; i++) {
  46.                 if(sts[i] instanceof String) {
  47.                     statement.setString(++count, (String)sts[i]);
  48.                 } else if (sts[i] instanceof Integer) {
  49.                     statement.setInt(++count, (Integer)sts[i]);
  50.                 } else if(sts[i] instanceof Short) {
  51.                     statement.setShort(++count, (Short)sts[i]);
  52.                 } else if(sts[i] instanceof Double) {
  53.                     statement.setDouble(++count, (Double)sts[i]);
  54.                 } else if(sts[i] instanceof Long) {
  55.                     statement.setLong(++count, (Long)sts[i]);
  56.                 } else {                 
  57.                     statement.setObject(++count, sts[i]);
  58.                 }                
  59.             }
  60.             return statement.executeQuery();             
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.             return null;
  64.         }
  65.     }  
  66.     //  //
  67.    
  68.     // OTHERS
  69.     public void setLastUsed(long lastUsedInMs) {
  70.         this.lastUsed = lastUsedInMs;
  71.     }
  72.    
  73.     public long getLastUsed() {
  74.         return this.lastUsed;
  75.     }
  76.    
  77.     public void finish() {
  78.         this.isUsed = false;
  79.     }
  80.    
  81.     public void restart() {
  82.         this.setLastUsed(System.currentTimeMillis());
  83.         this.isUsed = true;
  84.     }
  85.    
  86.     public boolean isUsed() {
  87.         return this.isUsed;
  88.     }
  89.    
  90.     public void dispose() {
  91.         this.setLastUsed(0);
  92.         try {
  93.             this.databaseConnection.close();
  94.             this.databaseConnection = null;
  95.         } catch (Exception e) {
  96.             // TODO Auto-generated catch block
  97.             e.printStackTrace();
  98.         }      
  99.     }
  100.     //  //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement