Unlucky4ever

Untitled

Aug 12th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package com.dynamic.graylist;
  2.  
  3. import java.sql.*;
  4. import java.util.logging.Level;
  5.  
  6. public class Database {
  7.     private String driver;
  8.     private String dsn;
  9.     private String username;
  10.     private String password;
  11.    
  12.    
  13.     public Database() {
  14.         this.driver = "com.mysql.jdbc.Driver";
  15.         this.dsn = ("jdbc:mysql://" + Constants.SQLHost + ":"+ Constants.SQLPort + "/" + Constants.SQLDatabase);
  16.         this.username = Constants.SQLUsername;
  17.         this.password = Constants.SQLPassword;
  18.        
  19.         try {
  20.             Class.forName(this.driver).newInstance(); } catch (Exception e) {
  21.                 Constants.log.log(Level.SEVERE, "[Dynamic Graylist] Driver error: " + e);
  22.         }
  23.     }
  24.    
  25.     public Connection getConnection() {
  26.         try {
  27.             if ((this.username.equalsIgnoreCase("")) && (this.password.equalsIgnoreCase(""))) {
  28.                 return DriverManager.getConnection(this.dsn);
  29.             }
  30.            
  31.             return DriverManager.getConnection(this.dsn, this.username, this.password);
  32.         } catch (SQLException e) {
  33.             Constants.log.log(Level.SEVERE, "[Dynamic Graylist] Could not create connection: " + e);
  34.         } return null;
  35.     }
  36.     public String getId() {
  37.         Connection conn = null;
  38.         ResultSet rs = null;
  39.         PreparedStatement ps = null;
  40.         String g_id = null;
  41.         String u_id = null;
  42.         try {
  43.             conn = getConnection();
  44.             ps = conn.prepareStatement("SELECT * FROM " + Constants.SQLTable);
  45.             rs = ps.executeQuery();
  46.            
  47.             while (rs.next()) {
  48.                 g_id = rs.getString(Constants.SQLColumnGroup);
  49.                 u_id = rs.getString(Constants.SQLColumnName);
  50.                 System.out.println("Group ID: " + g_id);
  51.                 System.out.println("Username: " + u_id);
  52.                 if (g_id == "5") {
  53.                     System.out.println("Administrator");
  54.                 }
  55.             }
  56.         } catch (Exception ex) {
  57.             g_id = null;
  58.             u_id = null;
  59.         } finally {
  60.             if (ps != null) {
  61.                 try {
  62.                     ps.close();
  63.                 } catch (SQLException ex) {
  64.                 }
  65.             }
  66.             if (rs != null) {
  67.                 try {
  68.                     rs.close();
  69.                 } catch (SQLException ex) {
  70.                 }
  71.             }
  72.             if (conn != null) {
  73.                 try {
  74.                     conn.close();
  75.                 } catch (SQLException ex) {
  76.                 }
  77.             }
  78.         }
  79.         return g_id;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment