Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dynamic.graylist;
- import java.sql.*;
- import java.util.logging.Level;
- public class Database {
- private String driver;
- private String dsn;
- private String username;
- private String password;
- public Database() {
- this.driver = "com.mysql.jdbc.Driver";
- this.dsn = ("jdbc:mysql://" + Constants.SQLHost + ":"+ Constants.SQLPort + "/" + Constants.SQLDatabase);
- this.username = Constants.SQLUsername;
- this.password = Constants.SQLPassword;
- try {
- Class.forName(this.driver).newInstance(); } catch (Exception e) {
- Constants.log.log(Level.SEVERE, "[Dynamic Graylist] Driver error: " + e);
- }
- }
- public Connection getConnection() {
- try {
- if ((this.username.equalsIgnoreCase("")) && (this.password.equalsIgnoreCase(""))) {
- return DriverManager.getConnection(this.dsn);
- }
- return DriverManager.getConnection(this.dsn, this.username, this.password);
- } catch (SQLException e) {
- Constants.log.log(Level.SEVERE, "[Dynamic Graylist] Could not create connection: " + e);
- } return null;
- }
- public String getId() {
- Connection conn = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- String g_id = null;
- String u_id = null;
- try {
- conn = getConnection();
- ps = conn.prepareStatement("SELECT * FROM " + Constants.SQLTable);
- rs = ps.executeQuery();
- while (rs.next()) {
- g_id = rs.getString(Constants.SQLColumnGroup);
- u_id = rs.getString(Constants.SQLColumnName);
- System.out.println("Group ID: " + g_id);
- System.out.println("Username: " + u_id);
- if (g_id == "5") {
- System.out.println("Administrator");
- }
- }
- } catch (Exception ex) {
- g_id = null;
- u_id = null;
- } finally {
- if (ps != null) {
- try {
- ps.close();
- } catch (SQLException ex) {
- }
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException ex) {
- }
- }
- }
- return g_id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment