qiTsuk

DBClass

Mar 23rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. package com.dkproduction.Utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. import com.dkproduction.Model.AToken;
  9. import com.wowza.wms.logging.WMSLogger;
  10. import com.wowza.wms.logging.WMSLoggerFactory;
  11.  
  12. public class DBClass {
  13.     private static Connection conn;
  14.     private static final String connString = "jdbc:mysql://localhost:3306/tokentest?user=root";
  15.     private static WMSLogger log;
  16.  
  17.     public DBClass() {
  18.         log = WMSLoggerFactory.getLogger(null);
  19.         log.info("DatabaseClass Awakened!");
  20.     }
  21.  
  22.     public static AToken getTokenByUsername(String username) {
  23.         conn = null;
  24.         AToken atoken = null;
  25.         String tokenText = "", tokenTimestamp = "", tokenExpire = "", oldTokenText = "", oldTokenTimeStamp = "",
  26.                 oldTokenExpire = "", banned = "";
  27.         int streaming = 0;
  28.         try {
  29.             conn = DriverManager.getConnection(connString);
  30.             String query = "SELECT * FROM token WHERE username='" + username + "'";
  31.             PreparedStatement prepState = null;
  32.             ResultSet rs = null;
  33.             try {
  34.                 prepState = conn.prepareStatement(query);
  35.                 rs = prepState.executeQuery();
  36.                 while (rs.next()) {
  37.                     tokenText = rs.getString("token_text");
  38.                     tokenTimestamp = rs.getString("time_stamp");
  39.                     tokenExpire = rs.getString("token_expire");
  40.                     oldTokenText = rs.getString("old_token_text");
  41.                     oldTokenTimeStamp = rs.getString("old_token_time_stamp");
  42.                     oldTokenExpire = rs.getString("old_token_expire");
  43.                     banned = rs.getString("banned");
  44.                     streaming = rs.getInt("streaming");
  45.                 }
  46.                 rs.close();
  47.                 atoken = new AToken(username, tokenText, tokenTimestamp, tokenExpire, oldTokenText, oldTokenTimeStamp,
  48.                         oldTokenExpire, banned, streaming);
  49.                 conn.close();
  50.             } catch (Exception e) {
  51.                 System.out.println(e.getMessage());
  52.                 atoken = null;
  53.                 conn.close();
  54.             }
  55.         } catch (Exception e) {
  56.             System.out.println(e.getMessage());
  57.             atoken = null;
  58.             try {
  59.                 conn.close();
  60.             } catch (Exception ex) {
  61.                 log.error(ex.getMessage());
  62.             }
  63.         } finally {
  64.             if (conn != null) {
  65.                 try {
  66.                     conn.close();
  67.                 } catch (Exception e) {
  68.                     // Do error handling here
  69.                     System.out.println("Closing Connection Error\n" + e.getMessage());
  70.                 }
  71.             }
  72.         }
  73.         if (atoken != null)
  74.             return atoken;
  75.         else {
  76.             log.error("Whyyyyyyy??");
  77.             return null;
  78.         }
  79.     }
  80.  
  81.     public static void setStreaming(String username, int streaming) {
  82.         conn = null;
  83.         try {
  84.             conn = DriverManager.getConnection(connString);
  85.             String query = "UPDATE token SET streaming=" + streaming + " WHERE username='" + username + "'";
  86.             PreparedStatement prepState = null;
  87.             ResultSet rs = null;
  88.             try {
  89.                 prepState = conn.prepareStatement(query);
  90.                 rs = prepState.executeQuery();
  91.                 while (rs.next()) {
  92.                     log.info(
  93.                             "\n_________________________________________________\n\t\tDoes it even go here??\n_________________________________________________");
  94.                 }
  95.                 prepState.close();
  96.                 conn.close();
  97.             } catch (Exception e) {
  98.                 conn.close();
  99.                 log.error(e.getMessage() + "Something here?? When trying to execute the query?");
  100.             }
  101.         } catch (Exception e) {
  102.             try {
  103.                 if (!conn.isClosed()) {
  104.                     conn.close();
  105.                     log.error(e.getMessage() + "Or perhaps here, when creating the query and the connection?");
  106.                 }
  107.             } catch (Exception ex) {
  108.                 log.error(ex.getMessage() + "Or is it here, where I try to close the connection in case of exception?");
  109.             }
  110.         }
  111.     }
  112. }
Add Comment
Please, Sign In to add comment