 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pat2021;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *The DBClass class is used to manage the connection to the database
 * as well as the processing of SQL Queries
 * @author bee.ntsimane
 * code supplied by Delia Kench 
 */
public class DBClass {
   
    private Connection connection;
    private PreparedStatement statement;
    private ResultSet resultSet;
    
    


    /**
     * Constructor method linking the project to the JavaDB
     */
    public DBClass()
    {
        try
        {
            String host = "jdbc:derby://localhost:1527/Students";
            
            String uName = "btn";
            String uPass = "030109";
            
            connection = DriverManager.getConnection(host,uName,uPass);
        }
        catch(SQLException err)
        {
            System.out.println(err.getMessage());
        }
    }
    
    /**
     * Processes update requests to the database
     * @param update
     * @throws SQLException
     */
    public void update(String update) throws SQLException 
    {

     
            statement = connection.prepareStatement(update);
     
            statement.executeUpdate();
         
            statement.close();
   

        
    }

    /**
     *Processes query statements
     * @param stmt - the SQL query statement
     * @return resultSet - the resultSet resulting from the processing
     * of the query
     * @throws SQLException
     */
    public ResultSet query(String stmt) throws SQLException
    {
        statement = connection.prepareStatement(stmt);
        resultSet = statement.executeQuery();
        return resultSet;
    }
    
}

