package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbFunctions {
	
	public DbFunctions(){}
	
	public boolean checkLogin(String username, String password) {
		PreparedStatement preS = null;
		boolean result = false;

		Connection c = new ConnectionPool().getConnection();
		try {
			preS = c.prepareStatement("SELECT * FROM ACCOUNT WHERE username = ? AND password = ?");
			preS.setString(1, username);
			preS.setString(2, password);   			
			ResultSet rs =  preS.executeQuery();	
			while(rs.next() == true) {
				result = true;				
				break;
			}
			rs.close();
			preS.close();
			c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public void register(String username, String password) {
		PreparedStatement preS = null;

		Connection con = new ConnectionPool().getConnection();
		try {
			preS = con.prepareStatement("INSERT INTO ACCOUNT(username, password, role) VALUES(?, ?, ?)");

			preS.setString(1, username);
			preS.setString(2, password);
			preS.setInt(3, 0);
			preS.executeUpdate();
			preS.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
