관리 메뉴

moozi

10/05 haksa 본문

TIS_2020/빅데이터2020_1기

10/05 haksa

moozi 2020. 10. 5. 10:21
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Haksa extends JFrame {
	public JTextField tfId;
	public JTextField tfName;
	public JTextField tfDepartment;
	public JTextField tfAddress;
	
	public JButton btnInsert;
	public JButton btnSelect;
	public JButton btnUpdate;
	public JButton btnDelete;
	public JButton btnSearch;
	
	Connection conn;
	
	DefaultTableModel model;
	JTable table;
		
	public Haksa() {
		this.setTitle("학사관리프로그램");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//DB Connection
		try {
			//orable jdbc드라이버 로드
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//Connection
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myoracle","ora_user","hong");// 연결
			System.out.println("연결완료");
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		//윈도우종료시 db connection close
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent e) {
				try {
					if(conn!=null) {
						conn.close();//connection close
					}
				}catch(Exception e1) {
					e1.printStackTrace();
				}
				
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}});
		
		
		Container c=this.getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(new JLabel("학번"));
		this.tfId=new JTextField(14);
		c.add(tfId);
		
		this.btnSearch=new JButton("검색");
		c.add(btnSearch);
		this.btnSearch.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					//orable jdbc드라이버 로드
					Class.forName("oracle.jdbc.driver.OracleDriver");
					//Connection
					Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myoracle","ora_user","hong");// 연결
					System.out.println("연결완료");
					
					//목록초기화					
					model.setNumRows(0);//model에서 행의 수를 0로 설정					
					
					//Statement객체생성
					Statement stmt = conn.createStatement();					
					
					ResultSet rs = stmt.executeQuery("select * from student2 where id='"+tfId.getText()+"'");
				
					while(rs.next()) {								
						tfId.setText(rs.getString("id"));
						tfName.setText(rs.getString("name"));
						tfDepartment.setText(rs.getString("dept"));
						
						String[] row=new String[3];//컬럼의 갯수가 3
					    row[0]=rs.getString("id");
					    row[1]=rs.getString("name");
					    row[2]=rs.getString("dept");
					    model.addRow(row);	
					}			
					rs.close();
					stmt.close();
					conn.close();			
				} catch(Exception e) {
					e.printStackTrace();
				}
				
			}});
		
		
		c.add(new JLabel("이름"));
		this.tfName=new JTextField(20);
		c.add(tfName);
		
		c.add(new JLabel("학과"));
		this.tfDepartment=new JTextField(20);
		c.add(tfDepartment);
		
		c.add(new JLabel("주소"));
		this.tfAddress=new JTextField(20);
		c.add(tfAddress);
		
		String colName[]={"학번","이름","학과"};
		model=new DefaultTableModel(colName,0);
		table = new JTable(model);
		table.setPreferredScrollableViewportSize(new Dimension(250,120));
		table.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {				
				table=(JTable)e.getComponent();
				model=(DefaultTableModel)table.getModel();
				
				tfId.setText((String)model.getValueAt(table.getSelectedRow(), 0));
				tfName.setText((String)model.getValueAt(table.getSelectedRow(), 1));
				tfDepartment.setText((String)model.getValueAt(table.getSelectedRow(), 2));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				
				
			}});
		c.add(new JScrollPane(table));	
		
		
				
			
		this.btnInsert=new JButton("등록");
		btnInsert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("등록버튼클릭됨");
				//등록처리. 오라클연동해서 처리.
				try {
									
					//Statement객체 생성
					Statement stmt=conn.createStatement();
					
					//insert
					stmt.executeUpdate("insert into student2 values('"+tfId.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");
					
					
				}catch(Exception e) {
					e.printStackTrace();
				}
				
				//목록
				list();				
				
				//등록완료메시지
				JOptionPane.showMessageDialog(null, "등록되었습니다");
			}});
		c.add(btnInsert);
		this.btnSelect=new JButton("목록");
		this.btnSelect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				//목록
				list();
			}});
		
		c.add(btnSelect);
		
		this.btnUpdate=new JButton("수정");
		this.btnUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					//Statement객체 생성
					Statement stmt=conn.createStatement();
					
					//update
					stmt.executeUpdate("update student2 set name='"+tfName.getText()+"',dept='"+tfDepartment.getText()+"' where id='"+tfId.getText()+"'");
					
					
				}catch(Exception e) {
					e.printStackTrace();
				}
				
				//목록
				list();
				
				//수정완료메시지
				JOptionPane.showMessageDialog(null, "수정되었습니다");
				
			}});
		c.add(btnUpdate);
		
		this.btnDelete=new JButton("삭제");
		this.btnDelete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				//삭제여부Confirm
				int result=JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?");
				if(result==JOptionPane.NO_OPTION || result==JOptionPane.CANCEL_OPTION || result==JOptionPane.CLOSED_OPTION) {
					return;//종료
				}
				
				try {
					//Statement객체 생성
					Statement stmt=conn.createStatement();
					
					//delete
					stmt.executeUpdate("delete from student2 where id='"+tfId.getText()+"'");
										
				}catch(Exception e) {
					e.printStackTrace();
				}
				
				//목록
				list();
				//입력항목초기화
				tfId.setText("");
				tfName.setText("");
				tfDepartment.setText("");
				
				//삭제완료메시지
				JOptionPane.showMessageDialog(null, "삭제되었습니다");
				
			}});
		
		c.add(btnDelete);
		
		this.setSize(300, 350);
		this.setResizable(false);//사이즈변경안함
		this.setVisible(true);
	}
		
	//목록
	public void list() {
		try {			
			//Statement객체 생성
			Statement stmt=conn.createStatement();			
			
			//목록초기화
			model.setNumRows(0);
			
			ResultSet rs=stmt.executeQuery("select * from student2");
			
			while(rs.next()) {				
				String[] row = new String[3]; //컬럼의 갯수가 3
			    row[0]=rs.getString("id");
			    row[1]=rs.getString("name");
			    row[2]=rs.getString("dept");
			    model.addRow(row);
			}		
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Haksa();
	}

}

'TIS_2020 > 빅데이터2020_1기' 카테고리의 다른 글

10/5 Haksa.java , Student.java  (0) 2020.10.05
10/5 BookRent  (0) 2020.10.05
10/05 oracle jdbc 기초  (0) 2020.10.05
oracle jdbc select기초  (0) 2020.09.29
oracle jdbc연결테스트  (0) 2020.09.29
Comments