<NoticeList.java>
* DB Connection ó¸® ¸Þ¼µå( www.webbdox.co.kr ÂüÁ¶)
package tag;
import
java.sql.*;
import
java.io.*;
import
java.util.*;
import
javax.servlet.jsp.PageContext;
public class
NoticeList{
private
final static String JDBC_DRIVER_NAME = "org.gjt.mm.mysql.Driver";
private final static String
DATABASE_URL = "jdbc:mysql://xxx.xxx.x.xx:3306/testdb";
private final static String
DATABASE_USER = "userid";
private final static String
DATABASE_PASSWORD = "password";
¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦
/***************************************************
*
* database
Driver¸¦ ·ÎµåÇÑ´Ù.
*
***************************************************/
public void
loadJDBCDriver(){
try {
Class.forName(JDBC_DRIVER_NAME);
} catch
(Exception e) {}
}
/***************************************************
*
* DB
Connection °´Ã¼¸¦ ¾ò¾î¿Â´Ù.
*
***************************************************/
private Connection
getDBConnection() throws SQLException {
return
DriverManager.getConnection(DATABASE_URL, DATABASE_USER,
DATABASE_PASSWORD);
}
¡¦¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦¡¦
}
DB Connection ºÎºÐÀº À§¿Í °°ÀÌ Ã³¸® ÇÏ¿´½À´Ï´Ù.
¹®Á¦´Â Ŭ·¡½º³»¿¡ Connection¿¡ ÇÊ¿äÇÑ º¯¼öµéÀÌ ¸ðµÎ µé¾î°¡ ÀÖ´Ù´Â Á¡ÀÔ´Ï´Ù.
º¸Åë ÀÌ·± Á¤º¸´Â ÆÄÀÏ·Î ÀúÀåÇÏ°í À̸¦ Àоî¿Í ó¸®ÇÏ´Â ¹æ½ÄÀÌ ÀϹÝÀûÀÔ´Ï´Ù. ÀÏ´ÜÀº ¿¹·Î µéÀº ºÎºÐÀÌ°í Properties Ŭ·¡½º¸¦ ÀÌ¿ëÇÏ´Â ¹æ¹ýµµ
Àִµ¥ ÀÌ ºÎºÐÀº Tip & Tech¿¡ ¿Ã·Á ³õµµ·Ï ÇÏ°Ú½À´Ï´Ù.
<NoticeList.java>
* ¸ñ·Ï °¡Á®¿À±â(ÆäÀÌ¡ ó¸®)
¡¦¡¦¡¦
public class
NoticeList{
¡¦¡¦¡¦
¡¦¡¦¡¦
private int page;
// setProperty ·Î ¼³Á¤µÇ´Â ÆäÀÌÁö(ÇöÀç)
private int list_cnt; // setProperty ·Î
¼³Á¤µÇ´Â ÆäÀÌÁö´ç ¸ñ·ÏÀÇ Ãâ·Â °³¼ö
private int total; // Àüü±Û °³¼ö
private String key; // °Ë»ö¾î
private String field; // °Ë»öÇʵå
private int no; // ±ÛÀбâÀ§ÇÑ
index No
¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦
¡¦¡¦¡¦¡¦¡¦¡¦
/***************************************************
*
* ¸ðµç
DB Field ÀÇ ³»¿ëÀ» °¡Á®¿Â´Ù.
*
***************************************************/
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,SUBJECT,CONTENT,REGI_DATE,CNT FROM NOTICE_T
" + getWhere() +
" ORDER BY REGI_DATE DESC";
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++){
NoticeRec nr = new NoticeRec();
if(!rs.next()){break;}
nr.num = recNum;
nr.no = rs.getInt("NO");
nr.subject = rs.getString("SUBJECT");
nr.content = rs.getString("CONTENT");
nr.regi_date
= rs.getString("REGI_DATE");
nr.cnt = rs.getInt("CNT");
v.addElement(nr); //»ý¼ºµÈ
NoticeRec °´Ã¼¸¦ Vector¿¡ ³Ö´Â´Ù
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;
}
¡¦¡¦¡¦
¡¦¡¦¡¦
}
Á¤ÇØÁø ÆäÀÌÁö ÀÌÀü±îÁö´Â º¤ÅÍ¿¡ ¾î¶² µ¥ÀÌÅ͵µ ´ãÁö ¾Ê½À´Ï´Ù.
ÀÏ´Ü ¼³Á¤µÈ ÆäÀÌÁö¸¦ ¸¸³ª¸é Á¤ÇØÁø °³¼ö ¸¸Å¸¸ º¤ÅÍ¿¡ ´ãµµ·Ï ÇÕ´Ï´Ù.
<Àüü ±ÛÀÇ °³¼ö °¡Á®¿À±â>
¡¦¡¦¡¦
¡¦¡¦¡¦
public class
NoticeList{
¡¦¡¦¡¦
¡¦¡¦¡¦
/***************************************************
*
* Àüü
¸ñ·ÏÀÇ °¹¼ö¸¦ °¡Á®¿Â´Ù.
*
***************************************************/
public int
getTotal() throws SQLException{
Connection
con = null;
Statement
stmt = null;
ResultSet
rs = null;
String
query;
try{
con = getDBConnection();
stmt = con.createStatement();
query = "SELECT COUNT(*) AS TOTAL
FROM NOTICE_T " + getWhere() + " ORDER BY
REGI_DATE DESC";
rs = stmt.executeQuery(query);
while(rs.next()){
total =
rs.getInt("TOTAL");
}
}catch(SQLException
e){
e.getMessage();
}finally{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
}
return
total;
}
}