1) dept vo 객체 생성
package javaexp.a04_vo;
//(1)vo객체 생성
public class Dept {
private int deptno;
private String dname;
private String loc;
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
2) DeptList 메서드 제작
public ArrayList<Dept> deptList(String loc) {
ArrayList<Dept> dlist = new ArrayList<Dept>();
try {
setConn();
//(3) sql 줄바꿈해서 쌍따옴표 전까지 복사해서 붙여넣으면 알아서 띄어쓰기됨..!
String sql = "SELECT *\n"
+ "FROM dept \n"
+ "WHERE loc LIKE '%'||'A'||'%'";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
int row = 1;
while (rs.next()) {
// vo객체의 int string string 선언
// (rs.getInt(1), rs.getString(2),rs.getString(3)) ==>생성자의 매개변수
dlist.add( new Dept(rs.getInt(1), rs.getString(2),rs.getString(3) ) );
// dept 컬럼 데이터 가져와서 출력
System.out.println("행"+row+++"\t");
System.out.print(rs.getInt("deptno")+"\t");
System.out.print(rs.getString("dname")+"\t");
System.out.println(rs.getString("loc")+"\n");
}
//(4) 자원해제
rs.close();
stmt.close();
con.close();
//(5) 예외처리
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
if (rs != null) {
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//리턴
return dlist;
}
3) String sql
=> 조회할 sql문 줄바꿈 해서 복사 붙여넣기
4) 자원해제
5) 예외처리
6) 데이터 조회
public static void main(String[] args) {
// TODO Auto-generated method stub
A03_DatabaseDao dao = new A03_DatabaseDao();
dao.deptList();
==========결과==========
줄 바꿈 애교..
7) JSP 파일 생성
7-1) java에서 생성한 java class호출
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="javaexp.a08_db.A03_DatabaseDao"
// (1)package이름.dao파일이름
import="javaexp.a04_vo.Dept"
// (2)vo객체생성 파일 이름
import="java.util.ArrayList"
// (3)dao파일 내 import
%>
7-2) dao 객체 생성 메서드 호출
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
//(7-2)dao 객체 생성 후 메소드 담기
<%
A03_DatabaseDao dao = new A03_DatabaseDao();
%>
<table align="center" border>
<tr><th>부서번호</th><th>부서명</th><th>부서위치</th></tr>
<%for(Dept d:dao.deptList("")) {%>
<tr><td><%=d.getDeptno() %></td>
<td><%=d.getDname() %></td>
<td><%=d.getLoc() %></td>
</tr>
<%} %>
</table>
</body>
</html>
==========결과==========
완~전 신기..
스스로 생각해서 짜면 어려울 듯..
연습이 더 필요하다@@@
그리고 티스토리 코드블럭 정렬 맘에 안든다ㅠ
'공부 > JAVA' 카테고리의 다른 글
2022.01.25 - spring 시작! (0) | 2022.01.25 |
---|---|
2022.01.06 - ORACLE / JAVA 조회처리 (0) | 2022.01.06 |
2022.01.06 - ORACLE / JAVA 연동 (0) | 2022.01.06 |
2021.12.30 - java 2회차.. 찐 답답.. (0) | 2021.12.30 |
2021.11.02 - 국비수업 java 4일차 (0) | 2021.11.07 |