3.µ¥ÀÌÅÍ º£À̽º Á¤ÀÇ ¹× ·¹ÄÚµå ÆÄÀÏ
board_t.sql |
USE testdb; CREATE TABLE
BOARD_T( NO
INT UNSIGNED NOT NULL PRIMARY KEY auto_increment,
NAME VARCHAR(16),
EMAIL VARCHAR(50), SUBJECT
VARCHAR(80) NOT NULL, CONTENT
TEXT NOT NULL, REGI_DATE
DATE, CNT
INT NOT NULL,
GROUP_ID INT,
DEPTH INT,
GROUP_ORDER INT,
PASSWD VARCHAR(8) ); |
BoardRec.java |
package board; import java.io.*; import java.util.*; public class BoardRec implements Serializable{
public int num; // ¸ñ·Ï¿¡ Ç¥½ÃµÉ ±ÛÀÇ ¿¬¹ø
public int no;
// ±ÛÀÇ index
public String name; // ±ÛÀÛ¼ºÀÚ
public String email; // ±ÛÀÛ¼ºÀÚ À̸ÞÀÏ
public String subject; // ±ÛÁ¦¸ñ
public String content; // ±Û³»¿ë
public String regi_date;
// ±ÛÀÛ¼ºÀÏ public int cnt; // ±ÛÁ¶È¸¼ö
public int group_id; // ±ÛÀÇ ±×·ì ¹øÈ£
public int depth; // ´äº¯±ÛÀÇ
Depth
public int group_order; // ´äº¯±ÛÀÇ ¼ø¼
public String passwd; // ±ÛÀÛ¼º½Ã Æнº¿öµå
// Getter / Setter ¸Þ¼µå(»ý·«) public void setGroup_id(int
group_id){this.group_id = group_id;}
public int getDepth(){return depth;} } |
3.¸ñ·Ï º¸±â ¹× ±Û ³»¿ëÀ» º¸±â À§ÇÑ Å¬·¡½º Á¤ÀÇ(Board.java)
½ÇÁ¦ µ¥ÀÌÅͺ£À̽º ¿¬µ¿ÀÌ ÇÊ¿äÇÑ ºÎºÐÀº ¾Õ¼ ¾ð±ÞÇßµíÀÌ ÀÎÅͺ£À̽º¸¦
±¸ÇöÇÏ¿© ¼³°èÇϵµ·Ï ÇÏ°Ú½À´Ï´Ù. DBenv.java ¸¦ ¹Ì¸® ÄÄÆÄÀÏÇϽñ⠹ٶø´Ï´Ù.
»ç¿ëÀÚ°¡ ¸ñ·Ï¸®½ºÆ®¸¦ ¿äûÇÏ¸é ºóÁî¿¡¼´Â ÇÊ¿äÇÑ µ¥ÀÌÅ͵éÀ»
µ¥ÀÌÅͺ£À̽º¿¡¼ °¡Á®¿É´Ï´Ù. ¹®Á¦´Â ¾î¶² Á¶°ÇÀÇ µ¥ÀÌÅ͸¦ ¾ó¸¶¸¸Å °¡Á® ¿À´À³Ä ÀÔ´Ï´Ù. °Ô´Ù°¡ °¡Á®¿Â µ¥ÀÌÅÍ´Â µ¥ÀÌÅÍ·¹ÄÚµå
ŸÀÔÀÇ ÀÚ·áÇüÀÌ µÉ°ÍÀÌ°í ÀÌ ÀÚ·áµéÀ» Àû´çÈ÷ º¸°üÇØÁÙ ¸Åü°¡ ÇÊ¿äÇÏ°Ô µË´Ï´Ù.
ÀÏ´Ü Á¶°ÇÀº ºóÁîÀÇ
setProperty¸¦ ÀÌ¿ëÇÏ¸é µÉ°ÍÀÌ°í ÀÚ·áÀÇ º¸°üÀº ¹è¿À̳ª º¤Å͸¦ »ç¿ëÇÏ¸é µË´Ï´Ù. ¹è¿°ú º¤ÅÍÀÇ Â÷ÀÌÁ¡À̶ó¸é Å©±âÀÇ Á¦ÇÑÀ̶ó°í ÇÒ ¼ö ÀÖ½À´Ï´Ù.
Áï, ¹è¿Àº Å©±â¸¦ ¹Ì¸®Á¤ÇؾßÇÏ´Â
¹Ý¸é¿¡ º¤ÅÍ´Â ÇÊ¿ä¿¡ µû¶ó ±× Å©±â¸¦ ´Ã¸±¼ö ÀÖ½À´Ï´Ù.
¿©±â¼´Â º¤Å͸¦ ÀÌ¿ëÇϵµ·Ï ÇÏ°Ú½À´Ï´Ù.
¶Ç, ÀÏ´Ü º¤ÅÍ·Î °¡Á®¿Â µ¥ÀÌÅÍ´Â
¿ì¸®°¡ Á¤ÀÇÇÑ µ¥ÀÌÅÍ Å¸ÀÔÀ¸·Î Çüº¯È¯À» ÇØ¾ß ÇÕ´Ï´Ù.
¿ì¸®´Â º¤ÅÍ¿¡ ÀúÀåµÇ´Â ÀÚ·áÇüÀÌ ÀÌ¹Ì µ¥ÀÌÅÍ·¹ÄÚµå ŸÀÔÀ̶ó´Â
ÀüÁ¦ÇÏ¿¡ ÀÛ¾÷À» ÇϹǷÎ
ÀÌ ºÎºÐ¶ÇÇÑ ½±°Ô ÇØ°áÇÒ ¼ö ÀÖÀ» °ÍÀÔ´Ï´Ù.
- Á¶°ÇÀÇ ¼³Á¤
ÇÊ¿äÇÑ Á¶°ÇµéÀ» »ý°¢Çغ¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
¿ì¼± ÆäÀÌÁö,°Ë»ö¾î,°Ë»öÇʵå
±×¸®°í ÇÑ ÆäÀÌÁö¿¡ Ãâ·ÂµÉ ¸ñ·ÏÀÇ ¼ö Á¤µµ ÀÔ´Ï´Ù.
ÀÌ·¯ÇÑ º¯¼öµéÀ»
setProperty¸¦ ÀÌ¿ëÇØ ºóÁî¿¡ ¼³Á¤ÇÏ¸é µÇ°Ú½À´Ï´Ù.
public class Board
implements DBenv{
private int page; // ÆäÀÌÁö ¼³Á¤
private int list_cnt; // Ãâ·ÂµÉ ¸ñ·ÏÀÇ ¼ö
private int total; // Àüü±ÛÀÇ °³¼ö(ÆäÀÌ¡ ±¸Çö¿¡ ÇÊ¿ä)
private String key; // °Ë»ö¾î
private String field; // °Ë»öÇʵå
}
°¢°¢ÀÇ ¸â¹öº¯¼ö¿¡ ÇÊ¿äÇÑ
setter ¸Þ¼µå¸¦ ¸¸µé¸é µË´Ï´Ù.
public
int getTotalPage(){return (int)((total-1)/list_cnt) + 1;}
public
void setPage(int page){this.page = page;}
public
void setList_cnt(int list_cnt){this.list_cnt = list_cnt;}
public
void setField(String field){this.field = field;}
public
void setKey(String key){this.key = key;}
³ª¸ÓÁö ºÎºÐÀº ÀÚ·á½Ç¿¡ ÀÖ´Â ¼Ò½º¸¦ Âü°íÇϽñ⠹ٶø´Ï´Ù.
- ¸ñ·Ï°¡Á®¿À±â
public Vector getList() throws SQLException{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Vector v = new Vector();
String query ;
int recNum; //
·¹ÄÚµåÀÇ ¼ø¹ø
recNum = total;
try{
con = getDBConnection();
stmt = con.createStatement();
query=¡±SELECT
¡° +
¡°NO,NAME,EMAIL,SUBJECT,CONTENT,REGI_DATE,CNT,¡±
+
¡°GROUP_ID,GROUP_ORDER,DEPTH,PASSWD " +
"FROM BOARD_T " + getWhere()+
" ORDER BY GROUP_ID DESC,GROUP_ORDER ASC";
rs = stmt.executeQuery(query);
for(int i=0;i<(page-1)*list_cnt;i++){
recNum--;
rs.next();
}
for(int i=0;i<list_cnt;i++){
BoardRec
br = new BoardRec();
if(!rs.next()){break;}
System.out.println("fecthing~~");
br.num
= recNum;
br.no
= rs.getInt("NO");
br.name
= rs.getString("NAME");
br.email
= rs.getString("EMAIL");
br.subject
= rs.getString("SUBJECT");
br.content
= rs.getString("CONTENT");
br.regi_date
= rs.getString("REGI_DATE");
br.cnt
= rs.getInt("CNT");
br.group_id
= rs.getInt("GROUP_ID");
br.group_order
= rs.getInt("GROUP_ORDER");
br.depth
= rs.getInt("DEPTH");
br.passwd
= rs.getString("PASSWD");
v.addElement(br);
recNum--;
}
}catch(SQLException e){
throw new
SQLException(e.getMessage());
}finally{
if (rs !=
null) rs.close();
if
(stmt != null) stmt.close();
if
(con != null) con.close();
}
return v;
}
ÆäÀÌ¡±¸ÇöÀ» À§ÇØ Á¤ÇØÁø ÆäÀÌÁö(page)¿¡¼ Á¤ÇØÁø ¸ñ·ÏÀÇ °³¼ö(list_cnt)¸¸Å º¤ÅÍ¿¡ ÀúÀåÀ» ÇÑÈÄ À̸¦ ¸®ÅÏÇÏ´Â ¸Þ¼µå ÀÔ´Ï´Ù.
getWhere() ¸Þ¼µå´Â
setProperty¿¡ ÀÇÇØ ¼³Á¤µÈ °Ë»öÁ¶°Ç(°Ë»ö¾î,°Ë»öÇʵå)À» Åä´ë·Î where Àý ÀÌÇÏÀÇ Äõ¸®¹®À» ÀÛ¼ºÇÏ¿© ¸®ÅÏÇÕ´Ï´Ù.
ÀÌÁ¤µµÀÇ ¸Þ¼µå Á¤µµ¸é ¸®½ºÆ®¸¦ Ãâ·ÂÇϴµ¥´Â ¹«¸®°¡ ¾øÀ»
°Í °°½À´Ï´Ù.
¼Ò½º¸¦ º¸½Ã¸é ¾Æ½Ã°ÚÁö¸¸
getWhere() ¸Þ¼µå´Â À¯Æ¿¸®Æ¼ ºóÁîÀÎ repString Ŭ·¡½ºÀÇ Å¬·¡½º ¸Þ¼µåÀÎ filter() ¸Þ¼µå¸¦ ÀÌ¿ëÇÕ´Ï´Ù. Ŭ·¡½º ¸Þ¼µå À̹ǷÎ
ÀÌ Å¬·¡½ºÀÇ ÀνºÅϽº¸¦ ¸¸µéÁö ¾Ê¾Æµµ ¹Ù·Î ÀÌ¿ë°¡´ÉÇÕ´Ï´Ù.