Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dkproduction.Utils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import com.dkproduction.Model.AToken;
- import com.wowza.wms.logging.WMSLogger;
- import com.wowza.wms.logging.WMSLoggerFactory;
- public class DBClass {
- private static Connection conn;
- private static final String connString = "jdbc:mysql://localhost:3306/tokentest?user=root";
- private static WMSLogger log;
- public DBClass() {
- log = WMSLoggerFactory.getLogger(null);
- log.info("DatabaseClass Awakened!");
- }
- public static AToken getTokenByUsername(String username) {
- conn = null;
- AToken atoken = null;
- String tokenText = "", tokenTimestamp = "", tokenExpire = "", oldTokenText = "", oldTokenTimeStamp = "",
- oldTokenExpire = "", banned = "";
- int streaming = 0;
- try {
- conn = DriverManager.getConnection(connString);
- String query = "SELECT * FROM token WHERE username='" + username + "'";
- PreparedStatement prepState = null;
- ResultSet rs = null;
- try {
- prepState = conn.prepareStatement(query);
- rs = prepState.executeQuery();
- while (rs.next()) {
- tokenText = rs.getString("token_text");
- tokenTimestamp = rs.getString("time_stamp");
- tokenExpire = rs.getString("token_expire");
- oldTokenText = rs.getString("old_token_text");
- oldTokenTimeStamp = rs.getString("old_token_time_stamp");
- oldTokenExpire = rs.getString("old_token_expire");
- banned = rs.getString("banned");
- streaming = rs.getInt("streaming");
- }
- rs.close();
- atoken = new AToken(username, tokenText, tokenTimestamp, tokenExpire, oldTokenText, oldTokenTimeStamp,
- oldTokenExpire, banned, streaming);
- conn.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- atoken = null;
- conn.close();
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- atoken = null;
- try {
- conn.close();
- } catch (Exception ex) {
- log.error(ex.getMessage());
- }
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception e) {
- // Do error handling here
- System.out.println("Closing Connection Error\n" + e.getMessage());
- }
- }
- }
- if (atoken != null)
- return atoken;
- else {
- log.error("Whyyyyyyy??");
- return null;
- }
- }
- public static void setStreaming(String username, int streaming) {
- conn = null;
- try {
- conn = DriverManager.getConnection(connString);
- String query = "UPDATE token SET streaming=" + streaming + " WHERE username='" + username + "'";
- PreparedStatement prepState = null;
- ResultSet rs = null;
- try {
- prepState = conn.prepareStatement(query);
- rs = prepState.executeQuery();
- while (rs.next()) {
- log.info(
- "\n_________________________________________________\n\t\tDoes it even go here??\n_________________________________________________");
- }
- prepState.close();
- conn.close();
- } catch (Exception e) {
- conn.close();
- log.error(e.getMessage() + "Something here?? When trying to execute the query?");
- }
- } catch (Exception e) {
- try {
- if (!conn.isClosed()) {
- conn.close();
- log.error(e.getMessage() + "Or perhaps here, when creating the query and the connection?");
- }
- } catch (Exception ex) {
- log.error(ex.getMessage() + "Or is it here, where I try to close the connection in case of exception?");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment