관리 메뉴

moozi

04/07 haksa 본문

TIS_2021/인공지능2021_1기

04/07 haksa

moozi 2021. 4. 7. 10:03
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Haksa2 extends JFrame{

	JTextField tfId=null;
	JTextField tfName=null;
	JTextField tfDepartment=null;
	JTextField tfAddress=null;
	
	JTextArea taList=null;
	
	JButton btnInsert=null;
	JButton btnSelect=null;
	JButton btnUpdate=null;
	JButton btnDelete=null;
	
	JButton btnSearch=null;
	
	Connection conn=null;
	
	Statement stmt=null;
	
	JTable table=null;
	DefaultTableModel model=null;
	
	
	public Haksa2() {
		this.setTitle("학사관리");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//DB Connection
		try {
			//oracle jdbc드라이버 로드
			Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load
			//Connection
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ora_user","hong");// 연결
			System.out.println("연결완료");
			stmt=conn.createStatement();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		//DB Close. 윈도우가 종료될 때 실행.
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {}

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

			@Override
			public void windowClosed(WindowEvent e) {}

			@Override
			public void windowIconified(WindowEvent e) {}

			@Override
			public void windowDeiconified(WindowEvent e) {}

			@Override
			public void windowActivated(WindowEvent e) {}

			@Override
			public void windowDeactivated(WindowEvent e) {}
		});
		
		
		
		
		Container c=this.getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(new JLabel("학번"));
		this.tfId=new JTextField(14);
		c.add(this.tfId);
		
		this.btnSearch=new JButton("검색");
		c.add(this.btnSearch);
		this.btnSearch.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					stmt=conn.createStatement();
					//목록초기화
					model.setNumRows(0);//모델의 행의 수 0
					ResultSet rs=stmt.executeQuery("select * from student where id='"+tfId.getText()+"'");
					
					while(rs.next()) {
						tfName.setText(rs.getString("name"));
						tfDepartment.setText(rs.getString("dept"));
						String[] row=new String[3];
						row[0]=rs.getString("id");
						row[1]=rs.getString("name");
						row[2]=rs.getString("dept");
						
						model.addRow(row);						
					}	
					rs.close();
					stmt.close();
					
				}catch(Exception e1) {
					e1.printStackTrace();
				}
				
			}});
		
		
		c.add(new JLabel("이름"));
		this.tfName=new JTextField(20);
		c.add(this.tfName);
		
		c.add(new JLabel("학과"));
		this.tfDepartment=new JTextField(20);
		c.add(this.tfDepartment);
		
		c.add(new JLabel("주소"));
		this.tfAddress=new JTextField(20);
		c.add(this.tfAddress);
		
		String[] colName= {"학번","이름","학과"};//컬럼명
		this.model=new DefaultTableModel(colName,0);//모델
		this.table=new JTable(this.model);//테이블에 모델적용
		this.table.setPreferredScrollableViewportSize(new Dimension(250,200));//테이블크기
		c.add(new JScrollPane(this.table));
		//테이블의 특정행을 선택해서 TextField에 값을 입력
		this.table.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				//이벤트가 발생한 컴포넌트(table)을 구한다.
				table=(JTable)e.getComponent();
				//table의 모델을 구한다.
				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 mousePressed(MouseEvent e) {}

			@Override
			public void mouseReleased(MouseEvent e) {}

			@Override
			public void mouseEntered(MouseEvent e) {}

			@Override
			public void mouseExited(MouseEvent e) {}
			});
		
		
		
		
		
		
		
		this.btnInsert=new JButton("등록");
		c.add(this.btnInsert);
		this.btnInsert.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				try {					
					Statement stmt=conn.createStatement();
					
					//insert
					stmt.executeUpdate("insert into student(id,name,dept) values('"+tfId.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");		
					
					//목록초기화
					model.setNumRows(0);//model의 행의 수 0 
					ResultSet rs=stmt.executeQuery("select * from student");
					
					while(rs.next()) {
						String[] row=new String[3];
						row[0]=rs.getString("id");
						row[1]=rs.getString("name");
						row[2]=rs.getString("dept");
						
						model.addRow(row);
					}	
					rs.close();
					stmt.close();
					
				}catch(Exception e1) {
					e1.printStackTrace();
				}
				
				
				JOptionPane.showMessageDialog(null, "등록되었습니다");
				
			}});
		
		this.btnSelect=new JButton("목록");
		c.add(this.btnSelect);
		this.btnSelect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {					
					Statement stmt=conn.createStatement();				
					
					//목록초기화
					model.setNumRows(0);//model의 행의수를 0으로.
					ResultSet rs=stmt.executeQuery("select * from student");					
					
					while(rs.next()) {
												
						String[] row=new String[3];
						row[0]=rs.getString("id");
						row[1]=rs.getString("name");
						row[2]=rs.getString("dept");
						
						model.addRow(row);
					}	
					rs.close();
					stmt.close();
					
				}catch(Exception e1) {
					e1.printStackTrace();
				}
				
			}});
		
		this.btnUpdate=new JButton("수정");
		c.add(this.btnUpdate);
		this.btnUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Statement stmt=conn.createStatement();					
					
					//update
					stmt.executeUpdate("update student set name='"+tfName.getText()+"',dept='"+tfDepartment.getText()+"' where id='"+tfId.getText()+"'");
					
					//목록초기화
					model.setNumRows(0);//model의 행의 수 0					
					
					ResultSet rs=stmt.executeQuery("select * from student where id='"+tfId.getText()+"'");					
					
					while(rs.next()) {
						
						String[] row=new String[3];
						row[0]=rs.getString("id");
						row[1]=rs.getString("name");
						row[2]=rs.getString("dept");
						
						model.addRow(row);
					}	
					rs.close();
					stmt.close();
					
				}catch(Exception e1) {
					e1.printStackTrace();
				}
				
			}});
		
		this.btnDelete=new JButton("삭제");
		c.add(this.btnDelete);
		this.btnDelete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int result=JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?","알림",JOptionPane.YES_NO_OPTION);
				System.out.println(result);
				if(result==JOptionPane.YES_OPTION) {
					//삭제처리
					try {						
						Statement stmt=conn.createStatement();					
												
						//delete
						stmt.executeUpdate("delete from student where id='"+tfId.getText()+"'");
												
						//목록초기화
						model.setNumRows(0);//행의 수 0						
						
						ResultSet rs=stmt.executeQuery("select * from student");						
						
						while(rs.next()) {
							
							String[] row=new String[3];
							row[0]=rs.getString("id");
							row[1]=rs.getString("name");
							row[2]=rs.getString("dept");
							
							model.addRow(row);
						}	
						rs.close();
						stmt.close();
						
					}catch(Exception e1) {
						e1.printStackTrace();
					}
				}
			}});
		
		this.setSize(300, 500);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new Haksa2();
	}

}

'TIS_2021 > 인공지능2021_1기' 카테고리의 다른 글

04/07 haksa3  (0) 2021.04.07
04/07 bookrent  (0) 2021.04.07
04/07 haksa  (0) 2021.04.07
04/06 haksa  (0) 2021.04.06
oracle 연결  (0) 2021.04.06
년도,상품별 판매갯수합계 판매되지 않은 상품도 출력하기  (0) 2021.03.31
0 Comments
댓글쓰기 폼