import java.io.*;
import java.sql.*;
import java.util.Date;

public class SearchItem
{
    	int totalListNum;
    	int page;
    	int totalPage;
    	int pageListNum;

	public SearchItem()
	{
	}

	public void loadDriver() throws ClassNotFoundException, Exception
	{
  		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
	}

	public BoardData[] search(String SearchCode , String SearchString) throws SQLException
	{
    		Date date;
    		int i;
     		String url="jdbc:oracle:thin:@localhost:1521:etri";
    		Connection con;
    		Statement stmt;
    		String SQLParam = "%"+SearchString+"%";

      		// SELECT
    		String query = "select BNUM, BDATE, BNAME, BEMAIL, BTITLE from board ";
    		if(SearchCode.indexOf("namesearch") != -1)
    		{
         		query = query + "where BNAME like '" + hangulTo(SQLParam.trim()) + "' ";
         		System.out.println(query);
      		}
     		else
        			query = query + "where BTITLE like '" + SQLParam.trim() + "' ";

      		// RECORD COUNT
     		String CountQuery = "select count(*) as rc from board ";
     		if(SearchCode.indexOf("namesearch") != -1)
     		{
         		CountQuery = CountQuery + "where BNAME like '" + SQLParam.trim() + "' ";
         		System.out.println(CountQuery);
      		}
     		else
        			CountQuery = CountQuery + "where BTITLE like '" + SQLParam.trim() + "' ";

          	con = DriverManager.getConnection(url, "scott", "tiger");
        		stmt = con.createStatement();
	   		// Record Count
        		ResultSet rc = stmt.executeQuery(CountQuery);
     	      	rc.next();
	         int RecordCount = rc.getInt("rc");
	         totalListNum = RecordCount;
 	   		// 게시물 수 체크
	         if(totalListNum == 0)
	                	return null;
 	   		// 전체 몇페이지인지를 구하고,
	   		// 한 화면에 5개의 게시물 표시
            	totalPage = totalListNum/HTMLLib.LINENUM+1;

	   		// pageListNum : 현재 페이지에 표시될 게시물 숫자
	          if(totalListNum!=0 && pageListNum==0)
                  	pageListNum=HTMLLib.LINENUM;

	   		// 게시물수만큼의 BoardData변수 db생성
	          BoardData[] bd = new BoardData[totalListNum];

	   		// db에 리스트화일을 열어 저장
        	  	i = 0;
	         ResultSet rs = stmt.executeQuery(query);

	         while(rs.next())
	         {
		        	bd[i] = new BoardData(rs.getInt("BNUM"), rs.getDate("BDATE"),
        	         	    rs.getString("BTITLE"),  rs.getString("BNAME"), rs.getString("BEMAIL"), null, null);
           			i++;
        	   	}
        	   	stmt.close();
         	con.close();

         	return bd;
	}

	public int getTotalListNum()
	{
		return totalListNum;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public int getPageListNum()
	{
		return pageListNum;
	}

   	// 데이타베이스에 쓸때
   	private String toHangul(String str)
   	{
      		if (str == null)
             		return null;
      		try
      		{
         		return new String(str.getBytes("8859_1"), "KSC5601");
      		}
      		catch (UnsupportedEncodingException e)
      		{
      		}
      		return null;
   	}

	//한글을 데이터베이스의 유니코드로 변환
   	private String hangulTo(String str)
   	{
      		if (str == null)
            		return null;
      		try
      		{
            		return new String(str.getBytes("KSC5601"), "8859_1");
      		}
      		catch (UnsupportedEncodingException e)
      		{
      		}
      		return null;
   	}
}