Advertisement
qiTsuk

Why??

Mar 22nd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package com.dkproduction;
  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.wowza.wms.logging.WMSLogger;
  9. import com.wowza.wms.logging.WMSLoggerFactory;
  10.  
  11. public class DBClass {
  12.     private static Connection conn;
  13.     private static final String connString = "jdbc:mysql://localhost:3306/tokentest?user=root";
  14.     private static WMSLogger log;
  15.  
  16.     public DBClass() {
  17.         log = WMSLoggerFactory.getLogger(null);
  18.         log.info("DatabaseClass Awakened!");
  19.     }
  20.  
  21.     // Så snart jeg kalder den her metode fra ModuleMain klassen, så får jeg en Reflection Exception!
  22.     public static Token getTokenByUsername(String username) {
  23.         conn = null;
  24.         Token token = null;
  25.         String tokenText = "", timestamp = "", tokenExpire = "", oldToken = "", oldTokenTimeStamp = "",
  26.                 oldTokenExpire = "", banned = "";
  27.         try {
  28.             conn = DriverManager.getConnection(connString);
  29.             String query = "SELECT * FROM token WHERE username='" + username + "'";
  30.             PreparedStatement prepState = null;
  31.             ResultSet rs = null;
  32.             try {
  33.                 prepState = conn.prepareStatement(query);
  34.                 rs = prepState.executeQuery();
  35.                 while (rs.next()) {
  36.                     tokenText = rs.getString("token_text");
  37.                     timestamp = rs.getString("time_stamp");
  38.                     tokenExpire = rs.getString("token_expire");
  39.                     oldToken = rs.getString("old_token_text");
  40.                     oldTokenTimeStamp = rs.getString("old_token_time_stamp");
  41.                     oldTokenExpire = rs.getString("old_token_expire");
  42.                     banned = rs.getString("banned");
  43.                 }
  44.                 rs.close();
  45.                 token = new Token(username, tokenText, timestamp, tokenExpire, oldToken, oldTokenTimeStamp,
  46.                         oldTokenExpire, banned);
  47.                 return token;
  48.             } catch (Exception e) {
  49.                 System.out.println(e.getMessage());
  50.                 return null;
  51.             }
  52.         } catch (Exception e) {
  53.             System.out.println(e.getMessage());
  54.             return null;
  55.         } finally {
  56.             if (conn != null) {
  57.                 try {
  58.                     conn.close();
  59.                 } catch (Exception e) {
  60.                     // Do error handling here
  61.                     System.out.println("Closing Connection Error\n" + e.getMessage());
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement