[Day3] JSP/JDBC 3 [10/28]
--๋ณต์ต--
1. dept ,emp ํ
์ด๋ธ์์
๋ถ์๋ช
์ผ๋ก ๊ฒ์ํ์ฌ ์์๋ ๋ชจ๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฝ๋ฉ์ ํ์ธ์.
ใฑ. PreparedStatement ์ฌ์ฉ.
ใด. ArrayList<EmpDTO> empList ์ฌ์ฉ.
ใท. LIKE ์ฐ์ฐ์๋ฅผ ์ฌ์ฉ.
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<EmpDTO> empList = null;
String pdname = null;
Scanner scanner = new Scanner(System.in);
String sql = " SELECT e.* "
+ " FROM emp e JOIN dept d ON e.deptno = d.deptno "
+ " WHERE d.dname LIKE ? "; // '%AL%'
//
System.out.print("> ๋ถ์๋ช
์
๋ ฅ ? ");
pdname = scanner.next();
//
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.format("%%%s%%", pdname.toUpperCase() ) );
rs = pstmt.executeQuery();
if ( rs.next()) {
empList = new ArrayList<EmpDTO>();
do {
EmpDTO dto = new EmpDTO(
rs.getInt("empno")
, rs.getString("ename")
, rs.getString("job")
, rs.getDate("hiredate")
, rs.getInt("mgr")
, rs.getDouble("sal")
, rs.getDouble("comm")
, rs.getInt("deptno"));
empList.add(dto);
} while ( rs.next() );
} // if
// printEmp( empList );
/*
if( empList == null ) {
System.out.println(" ์ฌ์ ์กด์ฌ X");
}else {
}
*/
try {
Iterator<EmpDTO> ir = empList.iterator();
while (ir.hasNext()) {
EmpDTO dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ์ฌ์ ์กด์ฌ X ");
} catch( Exception e) {
e.printStackTrace();
} //
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
DBConn.close();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // try
} // main
2. ConnectionString.properties ํ์ผ์ Properties ์ปฌ๋ ์ ์ ์ฌ์ฉํด์
hostname, sid, user, password ์์ฑ๊ฐ์ ์ฝ์ด์ DB ์ฐ๋ํด์
HR์ departments ํ ์ด๋ธ์ ๋ถ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฝ๋ฉ์ ํ์ธ์.
ใฑ. PreparedStatement ์ฌ์ฉ.
ใด. ArrayList<DepartmentsDTO> list ์ฌ์ฉ.
ใท. manager_id ๊ฐ NULL ์ธ ๋ถ์ ์ ๋ณด๋ง ์ถ๋ ฅ.
[์คํ๊ฒฐ๊ณผ]
[department_id=140, department_name=Control And Credit, manager_id=0, location_id=1700] [department_id=120, department_name=Treasury, manager_id=0, location_id=1700] [department_id=130, department_name=Corporate Tax, manager_id=0, location_id=1700] [department_id=150, department_name=Shareholder Services, manager_id=0, location_id=1700] [department_id=160, department_name=Benefits, manager_id=0, location_id=1700] [department_id=170, department_name=Manufacturing, manager_id=0, location_id=1700] [department_id=180, department_name=Construction, manager_id=0, location_id=1700] [department_id=190, department_name=Contracting, manager_id=0, location_id=1700] [department_id=200, department_name=Operations, manager_id=0, location_id=1700] [department_id=210, department_name=IT Support, manager_id=0, location_id=1700] [department_id=220, department_name=NOC, manager_id=0, location_id=1700] [department_id=230, department_name=IT Helpdesk, manager_id=0, location_id=1700] [department_id=240, department_name=Government Sales, manager_id=0, location_id=1700] [department_id=250, department_name=Retail Sales, manager_id=0, location_id=1700] [department_id=260, department_name=Recruiting, manager_id=0, location_id=1700] [department_id=270, department_name=Payroll, manager_id=0, location_id=1700] |
1. ConnectionString.properties ์์ hr, lion
2. DepartmentsDTO ์ ์ธ
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<DepartmentsDTO> list = null;
String sql = " SELECT * "
+ " FROM DEPARTMENTS "
+ " WHERE manager_id IS NULL";
// conn = DBConn.getConnection(); scott ๊ณ์ ์ ์.
String dir = System.getProperty("user.dir");
// C:\Class\WorkSpace\JdbcClass\jdbcPro
// System.out.println( dir );
Path path = Paths.get(dir, "src\\com\\util\\ConnectionString.properties");
// System.out.println( path );
Reader reader = null;
Properties p = null;
try {
reader = new FileReader( path.toString() );
p = new Properties();
p.load(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // try
// ์ฐ๊ฒฐ๋ฌธ์์ด( Connection String )
String user = p.getProperty("user") ;
String password = p.getProperty("password") ;
String url = p.getProperty("url") ;
String className = p.getProperty("className") ;
//System.out.println( user );
conn = DBConn.getConnection(url, user, password);
// oracle.jdbc.driver.T4CConnection@4629104a
// System.out.println( conn);
try {
pstmt = conn.prepareStatement(sql);
// ? ๋ฐ์ธ๋ฉ ๋ณ์
rs = pstmt.executeQuery();
if ( rs.next()) {
list = new ArrayList<DepartmentsDTO>();
do {
DepartmentsDTO dto = new DepartmentsDTO(
rs.getInt(1)
, rs.getString(2)
, rs.getInt(3)
, rs.getInt(4));
list.add(dto);
} while ( rs.next() );
} // if
try {
Iterator<DepartmentsDTO> ir = list.iterator();
while (ir.hasNext()) {
DepartmentsDTO dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ๊ฒฐ๊ณผ๋ฌผ X ");
} catch( Exception e) {
e.printStackTrace();
} //
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBConn.close();
} // main
3. HR์ jobs ํ
์ด๋ธ์์ job_id ์ปฌ๋ผ ๋๋ job_title ์ปฌ๋ผ์
'RE' ๋๋ 're' ๋๋ 'Re' ๋๋ 'rE' ( ์ฆ, ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ) ๋ฌธ์์ด์ ํฌํจํ๋ ๋ ์ฝ๋๋ฅผ
๊ฒ์ํด์ ์๋์ ๊ฐ์ด ์ถ๋ ฅํ์ธ์.
JOB_ID JOB_TITLE -------------------------------------------------------------- AD_P[RE]S P[re]sident AD_VP Administration Vice P[re]sident SA_[RE]P Sales [Re]p[re]sentative MK_[RE]P Marketing [Re]p[re]sentative HR_[RE]P Human [Re]sources [Re]p[re]sentative PR_[RE]P Public [Re]lations [Re]p[re]sentative |
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<JobsDTO> list = null;
String sql = " SELECT REGEXP_REPLACE( job_id, 'RE', '[RE]') job_id "
+ " , REGEXP_REPLACE( job_title, '(RE|Re|rE|re)', '[\\1]' ) job_title "
+ " FROM jobs "
+ " WHERE REGEXP_LIKE( job_id , 'RE' , 'i') OR REGEXP_LIKE( job_title , 'RE' , 'i')";
String dir = System.getProperty("user.dir");
Path path = Paths.get(dir, "src\\com\\util\\ConnectionString.properties");
Reader reader = null;
Properties p = null;
try {
reader = new FileReader( path.toString() );
p = new Properties();
p.load(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // try
String user = p.getProperty("user") ;
String password = p.getProperty("password") ;
String url = p.getProperty("url") ;
String className = p.getProperty("className") ;
conn = DBConn.getConnection(url, user, password);
try {
pstmt = conn.prepareStatement(sql);
// ? ๋ฐ์ธ๋ฉ ๋ณ์
rs = pstmt.executeQuery();
if ( rs.next()) {
list = new ArrayList<JobsDTO>();
do {
JobsDTO dto = new JobsDTO(
rs.getString(1)
, rs.getString(2)
, 0
, 0 );
list.add(dto);
} while ( rs.next() );
} // if
try {
Iterator<JobsDTO> ir = list.iterator();
while (ir.hasNext()) {
JobsDTO dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ๊ฒฐ๊ณผ๋ฌผ X ");
} catch( Exception e) {
e.printStackTrace();
} //
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBConn.close();
} // main
JOIN ํด์ ๋ฃ์ด์ผํ๋ DTO๊ฐ ํ์ํ๋ค๋ฉด ํด๋น๋๋ ๋ชจ๋ ์นผ๋ผ์ ์ ์ฅํ DTO๋ฅผ ์ ์ธํ๋ฉด ๋จ!
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
DeptEmpSalgradeDTO dto = null;
ArrayList<DeptEmpSalgradeDTO> list = null;
String sql = " SELECT d.deptno, d.dname, empno, ename, hiredate , sal + NVL(comm, 0) pay , grade "
+ " FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno "
+ " JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal "
+ " ORDER BY d.deptno ASC "; // ; ์ธ๋ฏธ์ฝ๋ก X
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if( rs.next() ) {
list = new ArrayList<DeptEmpSalgradeDTO>();
do {
// deptno, dname, empno, ename, hiredate , pay , grade
int deptno = rs.getInt(1);
String dname = rs.getString(2);
int empno = rs.getInt(3);
String ename = rs.getString(4);
Date hiredate = rs.getDate(5);
double pay = rs.getDouble(6);
int grade = rs.getInt(7);
dto = new DeptEmpSalgradeDTO(deptno, dname, empno, ename, hiredate, pay, grade);
list.add(dto);
}while( rs.next() );
} // if
//
try {
Iterator<DeptEmpSalgradeDTO> ir = list.iterator();
while (ir.hasNext()) {
dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ๊ฒฐ๊ณผ๋ฌผ X ");
} catch( Exception e) {
e.printStackTrace();
} //
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // try
DBConn.close();
} // main
[์คํ๊ฒฐ๊ณผ]
grade losal hisal emp-sal count(*) ------ -------------- --------- --------- 1๋ฑ๊ธ ( 700~1200 ) - 2๋ช 2๋ฑ๊ธ ( 1201~1400 ) - 2๋ช 3๋ฑ๊ธ ( 1401~2000 ) - 2๋ช 4๋ฑ๊ธ ( 2001~3000 ) - 4๋ช 5๋ฑ๊ธ ( 3001~9999 ) - 1๋ช |
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
SalgradeDTO dto = null;
ArrayList<SalgradeDTO> list = null;
String sql = "SELECT grade , losal , hisal "
+ ", count(*) cnt "
+ " FROM salgrade s JOIN emp e ON e.sal BETWEEN s.losal AND s.hisal "
+ " GROUP BY grade , losal , hisal "
+ " ORDER BY grade ASC";
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if( rs.next() ) {
list = new ArrayList<>();
do {
int grade = rs.getInt(1);
int losal = rs.getInt(2);
int hisal = rs.getInt(3);
int cnt = rs.getInt(4);
dto = new SalgradeDTO(grade, losal, hisal, cnt);
list.add(dto);
}while( rs.next() );
} // if
try {
Iterator<SalgradeDTO> ir = list.iterator();
while (ir.hasNext()) {
dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ๊ฒฐ๊ณผ๋ฌผ X ");
} catch( Exception e) {
e.printStackTrace();
} //
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // try
DBConn.close();
} // main
[์คํ๊ฒฐ๊ณผ]
1๋ฑ๊ธ ( 700~1200 ) - 2๋ช
20 RESEARCH 7369 SMITH 800 30 SALES 7900 JAMES 950 2๋ฑ๊ธ ( 1201~1400 ) - 2๋ช 30 SALES 7654 MARTIN 2650 30 SALES 7521 WARD 1750 3๋ฑ๊ธ ( 1401~2000 ) - 2๋ช 30 SALES 7499 ALLEN 1900 30 SALES 7844 TURNER 1500 4๋ฑ๊ธ ( 2001~3000 ) - 4๋ช 10 ACCOUNTING 7782 CLARK 2450 20 RESEARCH 7902 FORD 3000 20 RESEARCH 7566 JONES 2975 30 SALES 7698 BLAKE 2850 5๋ฑ๊ธ ( 3001~9999 ) - 1๋ช 10 ACCOUNTING 7839 KING 5000 |
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null , e_pstmt = null;
ResultSet rs = null, e_rs= null;
SalgradeDTO dto = null;
ArrayList<SalgradeDTO> list = null;
String sql = "SELECT grade , losal , hisal "
+ ", count(*) cnt "
+ " FROM salgrade s JOIN emp e ON e.sal BETWEEN s.losal AND s.hisal "
+ " GROUP BY grade , losal , hisal "
+ " ORDER BY grade ASC";
String sql2 = "SELECT d.deptno, dname, empno, ename, sal , grade "
+ " FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno "
+ " JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal "
+ " WHERE grade = ? ";
//
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if( rs.next() ) {
list = new ArrayList<>();
do {
int grade = rs.getInt(1);
int losal = rs.getInt(2);
int hisal = rs.getInt(3);
int cnt = rs.getInt(4);
dto = new SalgradeDTO(grade, losal, hisal, cnt);
list.add(dto);
// ๋ฑ๊ธ์ ํด๋นํ๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅ.
System.out.println(dto); // 1๋ฑ๊ธ ( 700 ~ 1200 ) - 2๋ช
e_pstmt = conn.prepareStatement(sql2);
e_pstmt.setInt(1, grade);
e_rs = e_pstmt.executeQuery();
if( e_rs.next() ) {
// ์ปฌ๋ ์
ํด๋์ค ์ ์ฅ
do {
int deptno = e_rs.getInt(1);
String dname = e_rs.getString(2);
int empno = e_rs.getInt(3);
String ename = e_rs.getString(4);
double sal = e_rs.getDouble(5);
System.out.printf("\t\t%d\t%s\t%d\t%s\t%.2f\n",
deptno, dname, empno, ename, sal);
}while( e_rs.next() );
}else {
System.out.println("\t\t ์ฌ์ ์กด์ฌ X");
} // if
e_pstmt.close(); //
e_rs.close(); //
}while( rs.next() );
} // if
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // try
DBConn.close();
} // main
ํ๋์ [์ปฌ๋ ์ ํด๋์ค]์ ์ ์ฅ : LinkedHashMap
LinkedHashMap<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>> map
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null , e_pstmt = null;
ResultSet rs = null, e_rs= null;
SalgradeDTO dto = null; // key
ArrayList<DeptEmpSalgradeDTO> list = null; // value
DeptEmpSalgradeDTO desDto = null;
LinkedHashMap<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>> map = null;
String sql = "SELECT grade , losal , hisal "
+ ", count(*) cnt "
+ " FROM salgrade s JOIN emp e ON e.sal BETWEEN s.losal AND s.hisal "
+ " GROUP BY grade , losal , hisal "
+ " ORDER BY grade ASC";
String sql2 = "SELECT d.deptno, dname, empno, ename, sal , grade "
+ " FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno "
+ " JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal "
+ " WHERE grade = ? ";
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if( rs.next() ) {
map = new LinkedHashMap<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>>();
do {
int grade = rs.getInt(1);
int losal = rs.getInt(2);
int hisal = rs.getInt(3);
int cnt = rs.getInt(4);
dto = new SalgradeDTO(grade, losal, hisal, cnt); // key
// ๋ฑ๊ธ์ ํด๋นํ๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅ.
//System.out.println(dto); // 1๋ฑ๊ธ ( 700 ~ 1200 ) - 2๋ช
list = null;
e_pstmt = conn.prepareStatement(sql2);
e_pstmt.setInt(1, grade);
e_rs = e_pstmt.executeQuery();
if( e_rs.next() ) {
list = new ArrayList<DeptEmpSalgradeDTO>();
do {
int deptno = e_rs.getInt(1);
String dname = e_rs.getString(2);
int empno = e_rs.getInt(3);
String ename = e_rs.getString(4);
double sal = e_rs.getDouble(5);
//System.out.printf("\t\t%d\t%s\t%d\t%s\t%.2f\n",
// deptno, dname, empno, ename, sal);
desDto = new DeptEmpSalgradeDTO(deptno, dname, empno, ename, null, sal, grade);
list.add(desDto); // value
}while( e_rs.next() );
map.put(dto, list); // entry ์ถ๊ฐ
}// if
e_pstmt.close(); //
e_rs.close(); //
}while( rs.next() );
} // if
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // try
DBConn.close();
printInfo( map );
} // main
private static void printInfo( LinkedHashMap<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>> map ) {
Set<Entry<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>>> set = map.entrySet();
Iterator<Entry<SalgradeDTO, ArrayList<DeptEmpSalgradeDTO>>> ir = set.iterator();
while (ir.hasNext()) {
Entry<SalgradeDTO,ArrayList<DeptEmpSalgradeDTO>> entry = ir.next();
SalgradeDTO key = entry.getKey();
System.out.println( key );
ArrayList<DeptEmpSalgradeDTO> list = entry.getValue();
Iterator<DeptEmpSalgradeDTO> ir2 = list.iterator();
while (ir2.hasNext()) {
DeptEmpSalgradeDTO dto = ir2.next();
// System.out.printf("\t\t%d\t%s\t%d\t%s\t%.2f\n"
// ,dto.getDeptno(), dname, empno, ename, sal);
System.out.println( dto );
} // while
} // while
}
MVC (model view controller) ํจํด
view : ํ๋ฉด
+ ์๋น์ค : ๋ก์ง ์ฒ๋ฆฌํ๋ ๋ชจ๋ธ๋ค์ ํธ์ถํ๋ ์ญํ
model : ๊ฒฐ๊ณผ๋ฌผ (๋ก์ง์ฒ๋ฆฌ + ๋ฐ์ดํฐ) -> DAO ์์ฒญ -> CRUD -> ์ค๋ผํด
model : ๊ฒฐ๊ณผ๋ฌผ (๋ก์ง์ฒ๋ฆฌ + ๋ฐ์ดํฐ) -> DAO ์์ฒญ -> CRUD -> ์ค๋ผํด
model : ๊ฒฐ๊ณผ๋ฌผ (๋ก์ง์ฒ๋ฆฌ + ๋ฐ์ดํฐ) -> DAO ์์ฒญ -> CRUD -> ์ค๋ผํด
controller : ๋ชจ๋ธ๊ณผ ๋ทฐ ์ํธ์์ฉ ๊ด๋ฆฌ
'๐จโ๐ป Web Development > JDBC | JSP | Servlet' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day1] JSP/Servlet 1 - ๊ฐ์ (0) | 2022.12.06 |
---|---|
[Day5] JSP/JDBC 5 - transaction ์ฒ๋ฆฌ, callable statement, reflection (0) | 2022.11.02 |
[Day4] JSP/JDBC 4 - Board (0) | 2022.11.01 |
[Day2] JSP/JDBC 2 - ๋์ ์ฟผ๋ฆฌ, PreparedStatement (0) | 2022.10.31 |
[Day1] JSP/JDBC 1 - ๊ฐ์, jdbc driver, dto (0) | 2022.10.28 |
์ต๊ทผ๋๊ธ