자바

 

 

 

오라클 실습용 데이터  

http://braverokmc.dothome.co.kr/m04/oracle/view/410

 

 

 

-- 과목 테이블
drop table subject;
-- number  : 정수 , 실수
-- number (3, 3)
-- char(고정길이), varchar(가변길이)


 

create table subject(
 subject_code number not null primary key,
 subject_name varchar2(50) not null,
 profno number not null,
 point number default 3
);

insert into subject values (1, 'java', 1001, 3);
insert into subject values (2, 'db', 1002, 4);
insert into subject values (3, 'jsp' , 1003, 2);

select * from subject;

commit;


 

select * from professor;

insert into subject (subject_code, subject_name, profno) 
values (4, '안드로이드', 1001);

select * from subject;

 

-- 수강테이블

create table lecture (
  studno number not null,
  subject_code number not null,
  grade VARCHAR2(2),
  primary key(studno, subject_code)
);

insert into lecture values (9411, 1, 'A0');
insert into lecture values (9411, 2, 'A+');
insert into lecture values (9411, 3, 'B0');
insert into lecture values (9412, 3, 'C0');
insert into lecture values (9413, 4, 'F');
insert into lecture values (9413, 4, 'B+');
insert into lecture values (9411, 3, 'A+');
insert into lecture values (9413, 2, 'B0');
insert into lecture values (9413, 1, 'A+');

commit;

select * from lecture;
select * from student;
select * from PROFESSOR;
select * from subject;

 

-- 학번 , 이름, 과목명, 담당교수, 학점, 등급

 

select  l.STUDNO,  s.NAME , sb.SUBJECT_NAME , p.name,  sb.point, l.grade 
  from student s, LECTURE l, professor p, subject sb 
 where s.STUDNO=l.STUDNO and l.SUBJECT_CODE =sb.SUBJECT_CODE and   p.PROFNO = sb.PROFNO;

 

-- A문자열  || B문자열  => 연결

select (studno || ' ' || name) name from student;

 

-- 학번, 이름, 학과, 지도교수, 전화번호
-- 교수이름 => 교수테이블, 학과이름 => 학과테이블


select studno, s.name sname, dname, p.name pname, tel
from student s, professor p, department d
where s.profno=p.profno and s.deptno1=d.deptno order by sname;

 

-- inner join(내부조인) : 양쪽 테이블에 자료가 있는 경우
-- outer join(외부조인 ) : 짝이 맞지 않는 경우

 

select studno, s.name sname, dname, p.name pname, tel
from student s, professor p, department d
where s.profno=p.profno(+) and s.deptno1=d.deptno 
  and studno=9711;

 

-- 새로운 컬럼 추가
-- alter table 테이블이름 add 컬럼 자료형(사이즈);
-- alternate(변경)
 


alter table student add imag_path varchar2(500);

commit;

select * from student;

 

oracle.prop

url=jdbc:oracle:thin:@localhost:1521:xe
driver=oracle.jdbc.OracleDriver
id=java
password=1111

 

 class DBConnectManager

package common.db;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class DBConnectManager {

	public static Connection dbConn(){
		Connection conn=null;
		try{
			//db 접속 정보가 저장되 db.prop 파일을 로딩
			FileInputStream fis =new FileInputStream("db.prop");
			Properties prop=new Properties();
			prop.load(fis);
			String url=prop.getProperty("url");
			String id=prop.getProperty("id");
			String password=prop.getProperty("password");

			//mysql에 접속 처리
			conn=DriverManager.getConnection(url, id, password);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public static Connection oracleConn(){
		Connection conn=null;
		try{
			//db 접속 정보가 저장되 db.prop 파일을 로딩
			FileInputStream fis =new FileInputStream("oracle.prop");
			Properties prop=new Properties();
			prop.load(fis);
			String url=prop.getProperty("url");
			String id=prop.getProperty("id");
			String password=prop.getProperty("password");

			//mysql에 접속 처리
			conn=DriverManager.getConnection(url, id, password);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	
	
	public static void close(Connection conn){
		try {
			if(conn!=null)conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void close(PreparedStatement pstmt){
		try {
			if(pstmt!=null)pstmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void close(ResultSet rs){
		try {
			if(rs!=null)rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void close(Connection conn, PreparedStatement pstmt ){
				
		try{
			if(pstmt!=null)pstmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try {
			if(conn!=null)conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	

	public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs){

		try{
			if(rs!=null)rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}
				
		try{
			if(pstmt!=null)pstmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try {
			if(conn!=null)conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	
	
	public static void close(Connection conn, Statement stmt, ResultSet rs){

		try{
			if(rs!=null)rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}
				
		try{
			if(stmt!=null)stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try {
			if(conn!=null)conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	
	
	
	
}

 

 

 

 

SQL


update student set imag_path=''   where  STUDNO=9411;

select * from student where STUDNO=9413;

select * from department order by dname;

select studno, s.name sname, s.deptno1, p.PROFNO,  s.imag_path, dname, p.name pname, tel 
	 from student s, professor p, department d 
					 where s.profno=p.profno(+) and s.deptno1=d.deptno; 
           
-- 교수 목록
select * from professor;

select  l.STUDNO,  s.NAME , sb.SUBJECT_NAME , p.name pname,  sb.point, l.grade 
  from student s, LECTURE l, professor p, subject sb 
 where s.STUDNO=l.STUDNO and l.SUBJECT_CODE =sb.SUBJECT_CODE and   p.PROFNO = sb.PROFNO
  and l.studno=9411
 ;


 

 

 

class DepartmentDAO

package java21_oracle.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import common.db.DBConnectManager;

public class DepartmentDAO {

	private static DepartmentDAO dao;
	
	private DepartmentDAO(){
		
	}
	
	public static DepartmentDAO getInstance(){
		if(dao==null){
			dao=new DepartmentDAO();
		}
		return dao;
	}

	//학과 목록을 ArrayList에 저장하여 리턴
	public ArrayList<DepartmentDTO> listDepartMent(){
		ArrayList<DepartmentDTO> items=new ArrayList<>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn =DBConnectManager.oracleConn();
			String sql ="select * from department order by dname";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				DepartmentDTO dto =new DepartmentDTO();
				dto.setDeptno(rs.getInt("deptno"));
				dto.setDname(rs.getString("dname"));
				items.add(dto);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(conn, pstmt, rs);
		}
		return items;
	}
	
	
}

 

class DepartmentDTO

package java21_oracle.menu;

public class DepartmentDTO {

	private int deptno; //학과코드
	private String dname; //학과 이름
	
	
	public DepartmentDTO() {
		super();
	}
	
	public DepartmentDTO(int deptno, String dname) {
		super();
		this.deptno = deptno;
		this.dname = dname;
	}
	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;
	}
	@Override
	public String toString() {
		return "DepartmentDTO [deptno=" + deptno + ", dname=" + dname + "]";
	}
	
	
	
	
}

 

class ImageUtil

package java21_oracle.menu;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {
	public static final int RATIO = 0;
    public static final int SAME = -1;
    
    public static void resize(File src, File dest, int width, int height, String formatName) throws IOException {
        Image srcImg = null;
        String suffix = src.getName().substring(src.getName().lastIndexOf('.')+1).toLowerCase();
        if (suffix.equals("bmp") || suffix.equals("png") || suffix.equals("gif")) {
            srcImg = ImageIO.read(src);
        } else {
            // BMP가 아닌 경우 ImageIcon을 활용해서 Image 생성
            // 이렇게 하는 이유는 getScaledInstance를 통해 구한 이미지를
            // PixelGrabber.grabPixels로 리사이즈 할때
            // 빠르게 처리하기 위함이다.
            srcImg = new ImageIcon(src.toURL()).getImage();
        }
        
        int srcWidth = srcImg.getWidth(null);
        int srcHeight = srcImg.getHeight(null);
        
        int destWidth = -1, destHeight = -1;
        
        if (width == SAME) {
            destWidth = srcWidth;
        } else if (width > 0) {
            destWidth = width;
        }
        
        if (height == SAME) {
            destHeight = srcHeight;
        } else if (height > 0) {
            destHeight = height;
        }
        
        if (width == RATIO && height == RATIO) {
            destWidth = srcWidth;
            destHeight = srcHeight;
        } else if (width == RATIO) {
            double ratio = ((double)destHeight) / ((double)srcHeight);
            destWidth = (int)((double)srcWidth * ratio);
        } else if (height == RATIO) {
            double ratio = ((double)destWidth) / ((double)srcWidth);
            destHeight = (int)((double)srcHeight * ratio);
        }
        
        Image imgTarget = srcImg.getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH); 
        int pixels[] = new int[destWidth * destHeight]; 
        PixelGrabber pg = new PixelGrabber(imgTarget, 0, 0, destWidth, destHeight, pixels, 0, destWidth); 
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            throw new IOException(e.getMessage());
        }
        BufferedImage destImg =null;
        if(formatName.toUpperCase().equals("PNG")){
        	destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_ARGB);	
        }else{
        	destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
        }
        destImg.setRGB(0, 0, destWidth, destHeight, pixels, 0, destWidth); 
        
        ImageIO.write(destImg, formatName, dest);
    }


}

 

class LectureDAO

package java21_oracle.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import common.db.DBConnectManager;

public class LectureDAO {

	private static LectureDAO dao;
	
	private LectureDAO(){
		
	}
	
	public static LectureDAO getInstance(){
		if(dao==null){
			dao=new LectureDAO();
		}
		return dao;
	}
	
	public Vector<Object>  listLecture(int studno){
		Vector<Object> items=new Vector<>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DBConnectManager.oracleConn();
			String sql ="select  l.STUDNO,  s.NAME , sb.SUBJECT_NAME , p.name pname,  sb.point, l.grade "
					+ " from student s, LECTURE l, professor p, subject sb "
					+ " where s.STUDNO=l.STUDNO and l.SUBJECT_CODE =sb.SUBJECT_CODE and   p.PROFNO = sb.PROFNO "
					+ " and l.studno=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, studno);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Vector<Object> row=new Vector<>();
				row.add(rs.getInt("studno"));
				row.add(rs.getString("name"));
				row.add(rs.getString("subject_name"));
				row.add(rs.getString("pname"));
				row.add(rs.getDouble("point"));
				row.add(rs.getString("grade"));
				items.add(row);
			}		
			
			System.out.println("학생 사이즈 : " + items.size());
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(conn, pstmt, rs);
		}
		
		return items;
	}
	
	
	
	
}

 

class LectureDTO

package java21_oracle.menu;

public class LectureDTO {

	private int studno;
	private String sname;
	private String subject_name;
	private String pname;
	private double point;
	private String grade;
	
	public LectureDTO() {
		
	}
	public LectureDTO(int studno, String sname, String subject_name, String pname, double point, String grade) {
		super();
		this.studno = studno;
		this.sname = sname;
		this.subject_name = subject_name;
		this.pname = pname;
		this.point = point;
		this.grade = grade;
	}
	public int getStudno() {
		return studno;
	}
	public void setStudno(int studno) {
		this.studno = studno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSubject_name() {
		return subject_name;
	}
	public void setSubject_name(String subject_name) {
		this.subject_name = subject_name;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public double getPoint() {
		return point;
	}
	public void setPoint(double point) {
		this.point = point;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "LectureDTO [studno=" + studno + ", sname=" + sname + ", subject_name=" + subject_name + ", pname="
				+ pname + ", point=" + point + ", grade=" + grade + "]";
	}
	
	
	
}

 

class ProfessorDAO

package java21_oracle.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.DefaultButtonModel;

import common.db.DBConnectManager;

public class ProfessorDAO {

	private static ProfessorDAO dao;
	
	private ProfessorDAO(){
		
	}
	
	public static ProfessorDAO getInstance(){
		if(dao==null){
			dao=new ProfessorDAO();
		}
		return dao;
	}
	
	public ArrayList<ProfessorDTO> listProfessor(){
		ArrayList<ProfessorDTO> items =new ArrayList<>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DBConnectManager.oracleConn();
			String sql="select * from professor order by name";
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				ProfessorDTO dto =new ProfessorDTO();
				dto.setProfno(rs.getInt("profno"));
				dto.setName(rs.getString("name"));
				items.add(dto);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(conn, pstmt, rs);
		}
		return items;
	}
	
	
	
}

 

class ProfessorDTO

package java21_oracle.menu;

public class ProfessorDTO {

	private int profno;
	private String name;
	
	public ProfessorDTO() {
	
	}
	
	public ProfessorDTO(int profno, String name) {
		super();
		this.profno = profno;
		this.name = name;
	}

	public int getProfno() {
		return profno;
	}

	public void setProfno(int profno) {
		this.profno = profno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "ProfessorDTO [profno=" + profno + ", name=" + name + "]";
	}

	
	
	
}



 

class StudentDAO

	package java21_oracle.menu;
	
	import java.sql.Connection;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.util.ArrayList;
	
	import common.db.DBConnectManager;
	
public class StudentDAO {
		
	private static StudentDAO dao;
	
	private StudentDAO(){
		
	}
	
	public static StudentDAO getInstance(){
		if(dao==null){
			dao=new StudentDAO();
		}
		return dao;
	}
	
	
	//학생 목록
	public ArrayList<String> listStudent(){
		ArrayList<String> items=new ArrayList<>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn =DBConnectManager.oracleConn();
			String sql="select (studno || ' ' || name) name from student ";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				items.add(rs.getString("name"));
			}
					
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(null, pstmt, rs);
		}
		return items;
	}
	
	
	//학생 상세 정보
	public StudentDTO detailStudent(int studno){
		StudentDTO dto =new StudentDTO();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DBConnectManager.oracleConn();
			String sql="select studno, s.name sname, s.deptno1,  s.imag_path, dname, p.name pname, tel,  p.PROFNO  "
					+ " from student s, professor p, department d "
					+ " where s.profno=p.profno(+) and s.deptno1=d.deptno and studno=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, studno);
			rs=pstmt.executeQuery();
			if(rs.next()){
				dto.setStudno(rs.getInt("studno"));
				dto.setName(rs.getString("sname"));
				dto.setDeptno1(rs.getInt("deptno1"));
				dto.setDname(rs.getString("dname"));
				dto.setPname(rs.getString("pname"));
				dto.setTel(rs.getString("tel"));
				dto.setProfno(rs.getInt("profno"));
				dto.setImag_path(rs.getString("imag_path"));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(conn, pstmt,rs);
		}
		
		return dto;
	}

	public int updateStudent(StudentDTO dto){
		int result=0;
		Connection conn=null;//db 접속 객체
//Statement, PreparedStatement		
		PreparedStatement pstmt=null;//sql 실행 객체
		try{
			conn =DBConnectManager.oracleConn();//DB 커넥션 리턴
			String sql="update student set name =?, deptno1=?, tel=?, imag_path=?, profno=? where  STUDNO=? ";
			pstmt =conn.prepareStatement(sql); // statement 객체 생성
			pstmt.setString(1, dto.getName());
			pstmt.setInt(2, dto.getDeptno1());
			pstmt.setString(3, dto.getTel());
			pstmt.setString(4, dto.getImag_path());
			pstmt.setInt(5, dto.getProfno());
			pstmt.setInt(6, dto.getStudno());
			result=pstmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnectManager.close(conn, pstmt);
		}
		return result;
	}
		
		
		
		
}







 

 

class StudentDTO

package java21_oracle.menu;

public class StudentDTO {

	private int studno;
	private String name;
	private int grade; //학년
	private String jumin;
	private String tel;
	private int deptno1;
	private int deptno2;
	private int profno;
	
	private String dname;
	private String pname;
	
	private String imag_path;
	
	public StudentDTO() {
		
	}

	public StudentDTO(int studno, String name, int grade, String jumin, String tel, int deptno1, int deptno2,
			int profno) {
		super();
		this.studno = studno;
		this.name = name;
		this.grade = grade;
		this.jumin = jumin;
		this.tel = tel;
		this.deptno1 = deptno1;
		this.deptno2 = deptno2;
		this.profno = profno;
	}

	
	
	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public int getStudno() {
		return studno;
	}

	public void setStudno(int studno) {
		this.studno = studno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public String getJumin() {
		return jumin;
	}

	public void setJumin(String jumin) {
		this.jumin = jumin;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public int getDeptno1() {
		return deptno1;
	}

	public void setDeptno1(int deptno1) {
		this.deptno1 = deptno1;
	}

	public int getDeptno2() {
		return deptno2;
	}

	public void setDeptno2(int deptno2) {
		this.deptno2 = deptno2;
	}

	public int getProfno() {
		return profno;
	}

	public void setProfno(int profno) {
		this.profno = profno;
	}

	
	
	
	public String getImag_path() {
		return imag_path;
	}

	public void setImag_path(String imag_path) {
		this.imag_path = imag_path;
	}

	@Override
	public String toString() {
		return "StudentDTO [studno=" + studno + ", name=" + name + ", grade=" + grade + ", jumin=" + jumin + ", tel="
				+ tel + ", deptno1=" + deptno1 + ", deptno2=" + deptno2 + ", profno=" + profno + ", dname=" + dname
				+ ", pname=" + pname + ", imag_path=" + imag_path + "]";
	}


	
	
	
	
	
	
	
}


 

class TabbedPaneExam

package java21_oracle.menu;

import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import javax.swing.JTable;

public class TabbedPaneExam extends JFrame {

	private JPanel contentPane;
	private JList list;

	private StudentDAO studentDAO;
	private JTextField tfName;
	private JTextField tfStudentNo;
	private JTextField tfTel;
	
	private String img_path;
	private String fileName;
	private String formatName;
	private File file;
	private JLabel lblImage;
	private String lodaing_img; 
	
	static TabbedPaneExam frame;
	private JScrollPane scrollPane;
	
	private DepartmentDAO departmentDAO;
	private JComboBox cboDeptNo;
	private JComboBox cboProfessor;
	
	private ProfessorDAO professorDAO;
	private ArrayList<ProfessorDTO> professorList;
	private JTable infoTable;
	
	private Vector<Object> data, col; //JTable 에 사용할 데이터와 컬럼행
	private JScrollPane scrollPane_1;
	
	private LectureDAO lectureDAO;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame= new TabbedPaneExam();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	public TabbedPaneExam() {
		studentDAO=StudentDAO.getInstance();
		departmentDAO=DepartmentDAO.getInstance();
		professorDAO=ProfessorDAO.getInstance();
		
		professorList=professorDAO.listProfessor();
		
		//수강정보 
		lectureDAO=LectureDAO.getInstance();
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 794, 665);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(194, 87, 552, 437);
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("학생정보", null, panel, null);
		panel.setLayout(null);
		
		lblImage = new JLabel("사진");
		lblImage.setBounds(12, 23, 120, 140);
		panel.add(lblImage);
		
		JLabel lblNewLabel_1 = new JLabel("이름");
		lblNewLabel_1.setBounds(200, 23, 57, 15);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("학번");
		lblNewLabel_2.setBounds(200, 65, 57, 15);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("학과");
		lblNewLabel_3.setBounds(200, 108, 57, 15);
		panel.add(lblNewLabel_3);
		
		JLabel label = new JLabel("지도교수");
		label.setBounds(200, 174, 57, 15);
		panel.add(label);
		
		JLabel lblNewLabel_4 = new JLabel("전화번호");
		lblNewLabel_4.setBounds(200, 231, 57, 15);
		panel.add(lblNewLabel_4);
		
		JButton btnSave = new JButton("저장");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//이미지 저장
				try {
					StudentDTO dto=new StudentDTO();
					
					if(fileName==null){
						if(lodaing_img ==null || lodaing_img.trim().length() ==0){
							//널일 경우 또는 
							dto.setImag_path("");

						}else if(lodaing_img.trim().length() >0){
							//db에 저장 된 이미지가 있을 경우
							dto.setImag_path(lodaing_img);	
						}
						
					}else{
						//업로드한 파일이 있을 경우
						UUID uid=UUID.randomUUID();
						String fName ="img/"+uid.toString()+"_"+fileName;
						ImageUtil.resize(file, new File(fName), 120, 140, formatName);
						System.out.println("이미지를 저장하였습니다.");
						dto.setImag_path(fName);
						
					}
					
					//콤보박스에서 선택한 인덱스 값
					int index =cboDeptNo.getSelectedIndex();
					//리스트.get(인덱스)
					dto.setDeptno1(departmentDAO.listDepartMent().get(index).getDeptno());
					
					
					dto.setStudno(Integer.parseInt(tfStudentNo.getText()));				
					dto.setName(tfName.getText());
					dto.setTel(tfTel.getText());
					
					//교수 수정
					index =cboProfessor.getSelectedIndex();
					dto.setProfno(professorList.get(index).getProfno());
					
					
					int result=studentDAO.updateStudent(dto);
					if(result==1){
						System.out.println("정보를 저장하였습니다.");
						JOptionPane.showMessageDialog(TabbedPaneExam.this, "저장 되었습니다.");
						
						//갱신
						studentList();					
					}
					
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
				
				
			}
		});
		btnSave.setBounds(200, 298, 97, 23);
		panel.add(btnSave);
		
		tfName = new JTextField();
		tfName.setBounds(284, 23, 116, 21);
		panel.add(tfName);
		tfName.setColumns(10);
		
		tfStudentNo = new JTextField();
		tfStudentNo.setEditable(false);
		tfStudentNo.setBounds(284, 62, 116, 21);
		panel.add(tfStudentNo);
		tfStudentNo.setColumns(10);
		
		tfTel = new JTextField();
		tfTel.setBounds(284, 228, 116, 21);
		panel.add(tfTel);
		tfTel.setColumns(10);
		
		JButton btnImage = new JButton("사진등록");
		btnImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc =new JFileChooser();//파일 선택 다이얼로그
				int result=fc.showOpenDialog(TabbedPaneExam.this);
				if(result==JFileChooser.APPROVE_OPTION){//열기 버튼을 누르면 실행
					file=fc.getSelectedFile();//선택한 파일 정보
					
					//이미지 파일의 절대 경로
					img_path=fc.getSelectedFile().getAbsolutePath();
					System.out.println(img_path);
					fileName=img_path.substring(img_path.lastIndexOf("\\")+1);
					System.out.println("파일 이름 : " +fileName);
				    formatName=fileName.substring(fileName.lastIndexOf(".")+1);
					
					System.out.println("포멧이름 : " + formatName);	
					
					
					try{
						//선택한 이미지를 라벨의 아이콘으로 설정
						ImageIcon icon =new ImageIcon(ImageIO.read(file));
						//아이콘에서 이미지를 리턴받음
						Image imageSrc =icon.getImage();
						//이미지를 resize 한 후
						Image imageNew=imageSrc.getScaledInstance(120, 140, Image.SCALE_AREA_AVERAGING);
						//아이콘을 변경시킴
						icon=new ImageIcon(imageNew);
						//라벨에 아이콘을 적용하
						lblImage.setIcon(icon);
						
						
						//이미지 저장
			/*			//Image img = icon.getImage();
						BufferedImage bi = new BufferedImage
								(120,140, BufferedImage.);
						

						Graphics2D g2 = bi.createGraphics();
						g2.drawImage(imageNew, 0, 0, null);
						g2.dispose();
						ImageIO.write(bi, formatName, new File(fileName));*/
						
						
						
					}catch(Exception e2){
						e2.printStackTrace();
					}
				}
			}
		});
		btnImage.setBounds(24, 211, 97, 23);
		panel.add(btnImage);
		
		cboDeptNo = new JComboBox();
		cboDeptNo.setBounds(284, 105, 116, 21);
		panel.add(cboDeptNo);
		
		cboProfessor = new JComboBox();
		cboProfessor.setBounds(284, 171, 116, 21);
		panel.add(cboProfessor);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("수강정보", null, panel_1, null);
		panel_1.setLayout(null);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(12, 10, 523, 388);
		panel_1.add(scrollPane_1);
		
		infoTable = new JTable();
		scrollPane_1.setViewportView(infoTable);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(26, 96, 118, 428);
		contentPane.add(scrollPane);
				
			
		studentList();
		
	
	}

	
	
	
	public void studentList(){
		
		ArrayList<String> studentList=studentDAO.listStudent();
		//ArrayList 를 JList에 입력
		//toArray ArrayList 를 배열로 변환 
		
		//벡터의 컬럼열 생성
		col =new Vector<Object>();
		col.add("학번");
		col.add("이름");
		col.add("과목명");
		col.add("담당교수");
		col.add("학점");
		col.add("등급");
		
		
	
		
		list = new JList(studentList.toArray());
		
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				
				if(e.getValueIsAdjusting()){

					
					
					//학생정보 탭
					
					fileName=null; //업로드 파일 null 값으로 초기화
					
					//System.out.println(list.getSelectedValue());
					//getSelectedValue() 선택한 값
					//split("구분자") 문자열을 구분자를 기준으로 분리시켜 배열을 리턴
					String[] arr=list.getSelectedValue().toString().split(" ");
					System.out.println(arr[0]);
					
					StudentDTO dto=studentDAO.detailStudent(Integer.parseInt(arr[0]));
					if(dto!=null){
						tfName.setText(dto.getName());
						tfStudentNo.setText(dto.getStudno()+"");
						
						int count=0;
						int cboIndex=0;
						//학과 목록 출력
						//콤보박스의 값을 모두 지움
						cboDeptNo.removeAllItems();
						for(DepartmentDTO deptDto : departmentDAO.listDepartMent()){
							//콤보박스에 아이템 추가
							cboDeptNo.addItem(deptDto.getDname());
							if(dto.getDeptno1()==deptDto.getDeptno()){
								cboIndex=count; //콤보박스의 인덱스 저장
								System.out.println("dto.getDEptino1() : " +dto.getDeptno1());
							}
							count++;
						}
						cboDeptNo.setSelectedIndex(cboIndex);			
						
						
						//교수 목록
						cboIndex=0;
						cboProfessor.removeAllItems(); //콤보박스 클리어
						
						for(int i=0; i<professorList.size(); i++){			
							cboProfessor.addItem(professorList.get(i).getName());
							if(dto.getProfno()==professorList.get(i).getProfno()){
								cboIndex=i;
							}
						}
						cboProfessor.setSelectedIndex(cboIndex);
												
						tfTel.setText(dto.getTel());
						
						
						//이미지 경로
						String path =dto.getImag_path();
						lodaing_img=path;
						if(path==null){
							lblImage.setText("사진미등록");
							lblImage.setIcon(null);//아이콘을 표시하지 않음
						}else{//사진이 등록되었을때
							ImageIcon icon =new ImageIcon(path);
							Image imageSrc =icon.getImage();
							//Image imageNew =imageSrc.getScaledInstance(120, 140, Image.SCALE_AREA_AVERAGING);
							lblImage.setIcon(icon);
							
						}

						
						
						//수강정보 탭
						DefaultTableModel model=
								new DefaultTableModel(lectureDAO.listLecture(Integer.parseInt(tfStudentNo.getText())), col);
						infoTable.setModel(model);
						
					}else{
						tfName.setText("");
						tfStudentNo.setText("");
						//cboDeptNo.addItem("");
						//tfProfessor.setText("");
						tfTel.setText("");
					}

					
				
							
		
				}						
				
			}
		});
		
		scrollPane.setViewportView(list);
	}
}

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

죽을 때까지 남의 원망을 듣고 싶은 사람은 남을 신랄하게 비판하는 것을 일삼으면 된다. - D. 카네기

댓글 ( 0)

댓글 남기기

작성
  •    
  •