/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author smuRf
 */
public class Database {
    private static String serverName;
    private static String username;
    private static String password;
    
    private Connection connection;
    
    public Database(String user, String pass, String url)
    {
        username = user;
        password = pass;
        serverName = url;
        
        connection = getConnection();
    }
    
    public boolean isConnectionUp()
    {
        return connection == null ? false : true;
    }
    
    public Connection getConnection()
    {
        Connection conn = null;
        try {
            Properties connectionProps = new Properties();
            connectionProps.put("user", username);
            connectionProps.put("password", password);

            conn = (Connection) DriverManager.getConnection("jdbc:mysql://" + serverName, connectionProps);

            //System.err.println("connected to database");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Could not connect to database", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return conn;
    }
    
    public void closeConnection()
    {
        try {
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int insert(String command)
    {
        try {
            Statement st = (Statement) connection.createStatement();
            st.executeUpdate(command, Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = st.getGeneratedKeys();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return -1;
    }
    
    public void update(String command)
    {
        try {
            Statement st = (Statement) connection.createStatement();
            st.executeUpdate(command);
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ResultSet select(String command)
    {
        try {
            String query = command;
            
            Statement st = (Statement) connection.createStatement();
            ResultSet rs = st.executeQuery(query);
            
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public void execute(String command)
    {
        try {
            Statement st = (Statement) connection.createStatement();
            st.execute(command);
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
