package admin.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.ConnectionPool;
import model.ThongTinSanPham;
public class AnnounceMod {

	 public AnnounceInfo getAnnounce() throws SQLException {
		 PreparedStatement getannounce = null;
      AnnounceInfo result = new AnnounceInfo("","");
      Connection con = new ConnectionPool().getConnection();
      
       getannounce = con.prepareStatement("select * from announce");
       ResultSet rs = getannounce.executeQuery();    
       while (rs.next()) {
       	String date = rs.getString(2);
        String content = rs.getString(3);
           AnnounceInfo announce = new AnnounceInfo(date, content);
           result = announce;
       }

       con.close();
       return result;
   }
	 
	 public boolean CreateAnnounce(AnnounceInfo an) {
		 PreparedStatement createan = null;
			boolean result = false;

			Connection con = new ConnectionPool().getConnection();
			try {
				
				createan = con.prepareStatement("insert into announce(date, content)  values(?, ?)");
				createan.setString(1, an.getDate());
				createan.setString(2, an.getContent());
				
				int number = createan.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	 
	    public int recordsan() throws SQLException {
	    	PreparedStatement recornumb = null;
	    	int record = 0;
			Connection con = new ConnectionPool().getConnection();
			
				recornumb = con.prepareStatement("select count(id) from announce");
		        ResultSet rs2 = recornumb.executeQuery();
		        while (rs2.next()) { 	        	
		        	record = rs2.getInt(1);
		        }
				con.close();
			return record;
		}
	 
	 	public boolean Update(String date, String content) {
	 		PreparedStatement update = null;
			boolean result = false;

			Connection con = new ConnectionPool().getConnection();
			try {

				update = con.prepareStatement("update announce set date = ?, content = ?");
				update.setString(1, date);
				update.setString(2, content);
				int number = update.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	 
	public static void main(String[] args) throws SQLException {
		System.out.println(new AnnounceMod().Update("09/09/2011","LuanXT"));
	}
}
