DB-Java 연동 (가변배열 사용)

Yoon·2022년 1월 6일
0

DB

목록 보기
3/7

전체 소스 코드(javaexp.A08_db.A02_DatabaseDao)

package javaexp.A08_db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javaexp.a04_vo.Emp;

public class A02_DatabaseDao {
	// ex) 연결 처리 객체 만들기.
	// 1. 공통 필드 선언
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	
	// 2. 공통 메서드 선언
	public void setConn() {
		// 1) 드라이버 연결
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("오류:"+e.getMessage());
		}
		
		// 2) 특정 서버 연결
		String info="jdbc:oracle:thin:@localhost:1521:xe";
		try {
			con = DriverManager.getConnection(info,"scott","tiger");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("오류:"+e.getMessage());
		}
		
		System.out.println("접속 성공");
	}
	// 조회 처리 메서드 구현 1단계(출력)
	public void empList() {
		// 1. 연결공통메서드 호출
			
		try {
			setConn();
			// 2. Statement 객체 생성 (Connection ==> Statement)
			stmt = con.createStatement();
			String sql = "SELECT * FROM EMP";
			// 3. ResultSet 객체 생성.sql의 결과
			rs = stmt.executeQuery(sql);
			// 4. while()을 통해 결과 내용 처리 sql의 결과는 처리
			// rs.next() 행단위로 이동하게 처리..
			int rowNum = 1;
			while(rs.next()) {
				System.out.print(rowNum+++"행"+"\t");
				// rs.get데이터유형("컬럼명/alias명");
				System.out.print(rs.getInt("empno")+"\t");
				// rs.getInt(1), rs.getString(2)
				System.out.print(rs.getString("ename")+"\t");
				System.out.print(rs.getString("job")+"\t");
				System.out.print(rs.getInt("mgr")+"\t");
				System.out.print(rs.getDate("hiredate")+"\t");
				System.out.print(rs.getDouble("sal")+"\t");
				System.out.print(rs.getDouble("comm")+"\t");
				System.out.print(rs.getInt("deptno")+"\t");
				System.out.println();
			}
			// 5. 자원 해제
			rs.close();
			stmt.close();
			con.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			// 6. 예외 처리..
			System.out.println("오류:"+e.getMessage());
			if(rs!=null) rs = null; // 강제로 자원해제..
			if(stmt!=null) stmt = null; // 강제로 자원해제..
			if(con!=null) con = null; // 강제로 자원해제..
		}

	}
	
	// ex) A03_DatabaseDao.java를 만들고,
	//		필드, 공통메서드, public void deptList()로 선언하여
	//		sql을 select * from dept로 처리된 행을 출력하는 내용까지 처리.
	
	// 조회 처리 메서드 구현 1단계(출력)
	// 리턴 VO 만들고, ArrayList<VO> 선언하기. 자동 import : ctrl+shift+o
	public ArrayList<Emp> empList(String ename) {
		// 1. 연결공통메서드 호출
		// 리턴할 객체 선언.
		ArrayList<Emp> emplist = new ArrayList<Emp>();
		// 메서드의 마지막부분 return emplist;
		try {
			setConn();
			// 2. Statement 객체 생성 (Connection ==> Statement)
			stmt = con.createStatement();
			
			String sql = "SELECT *\r\n"
					+ "FROM emp\r\n"
					+ "WHERE ename LIKE '%'||'"+ename+"'||'%'";
			// 3. ResultSet 객체 생성.sql의 결과
			rs = stmt.executeQuery(sql);
			// 4. while()을 통해 결과 내용 처리 sql의 결과는 처리
			// rs.next() 행단위로 이동하게 처리..
			int rowNum = 1;
			
			// 행단위로 단위 객체를 생성하여 ArrayList에 할당 처리..
			while(rs.next()) {
				System.out.print(rowNum+++"행"+"\t");
				// rs.get데이터유형("컬럼명/alias명");
				System.out.print(rs.getInt("empno")+"\t");
				// rs.getInt(1), rs.getString(2)
				System.out.print(rs.getString("ename")+"\t");
				System.out.print(rs.getString("job")+"\t");
				System.out.print(rs.getInt("mgr")+"\t");
				System.out.print(rs.getDate("hiredate")+"\t");
				System.out.print(rs.getDouble("sal")+"\t");
				System.out.print(rs.getDouble("comm")+"\t");
				System.out.print(rs.getInt("deptno")+"\t");
				System.out.println();
				
				Emp emp = new Emp();
				emp.setEmpno(rs.getInt("empno"));
				emp.setEname(rs.getString("ename"));
				emp.setJob(rs.getString("job"));
				emp.setMgr(rs.getInt("mgr"));
				emp.setHiredate(rs.getDate("hiredate"));
				emp.setSal(rs.getDouble("sal"));
				emp.setComm(rs.getDouble("comm"));
				emp.setDeptno(rs.getInt("deptno"));
				emplist.add(emp); // 단위객체를 emplist에 할당처리. 
			}
			// 5. 자원 해제
			rs.close();
			stmt.close();
			con.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			// 6. 예외 처리..
			System.out.println("오류:"+e.getMessage());
			if(rs!=null) rs = null; // 강제로 자원해제..
			if(stmt!=null) stmt = null; // 강제로 자원해제..
			if(con!=null) con = null; // 강제로 자원해제..
			
		}
		
		return emplist;
	
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 객체 생성 및 메서드 처리..
		int num = 1;
		A02_DatabaseDao dao = new A02_DatabaseDao();
		dao.empList();
		System.out.println("데이터의 크기: "+dao.empList("A").size());
		System.out.println("# ArrayList<Emp>를 통한 화면 출력 #");
		for(Emp emp : dao.empList("A")) {
			System.out.print(num+" ");
			num++;
			System.out.println(emp.getEname()+"\t"+emp.getJob());
		}
	}

}

vo 코드 (javaexp.a04_vo.Emp)

package javaexp.a04_vo;

import java.util.Date;

public class Emp {
	private int empno;
	private String ename;
	private String job;
	private int mgr;
	private Date hiredate;
	private double sal;
	private double comm;
	private int deptno;
	public Emp() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Emp(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}
	public int getEmpno() {
		return empno;
	}
	public void setEmpno(int empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getMgr() {
		return mgr;
	}
	public void setMgr(int mgr) {
		this.mgr = mgr;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public double getSal() {
		return sal;
	}
	public void setSal(double sal) {
		this.sal = sal;
	}
	public double getComm() {
		return comm;
	}
	public void setComm(double comm) {
		this.comm = comm;
	}
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	
	
}
profile
나의 공부 일기

0개의 댓글