package org.mynonglam.dao;

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

import org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.NewInfo;

public class NewsInfoDAO {

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;

	public NewsInfoDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public boolean insertNewInfo(NewInfo info) {
		boolean result = false;
		
		String sql = "INSERT INTO newinfo(id, email, idtable, nametable, datecreate) VALUES (?,?,?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, info.getId());
			statement.setString(2, info.getEmail());
			statement.setString(3, info.getIdtable());
			statement.setString(4, info.getNametable());
			statement.setString(5, info.getDatecreate());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean deleteNewInfo(String idtable, String email, String nametable) {
		boolean result = false;
		
		String sql = "DELETE FROM newinfo WHERE email=? AND idtable=? AND nametable=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, email);
			statement.setString(2, idtable);
			statement.setString(3, nametable);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDNewInfoLast() {
		String result = "";
		
		String sql = "SELECT id FROM newinfo ORDER BY id DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

}
