관리 메뉴

moozi

04/06 haksa 본문

TIS_2021/인공지능2021_1기

04/06 haksa

moozi 2021. 4. 6. 15:45
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JTextArea;
import javax.swing.JTextField;

public class Haksa 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;
	
	public Haksa() {
		this.setTitle("학사관리");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c=this.getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(new JLabel("학번"));
		this.tfId=new JTextField(20);
		c.add(this.tfId);
		
		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);
		
		this.taList=new JTextArea(10,23);
		c.add(new JScrollPane(this.taList));
		
		this.btnInsert=new JButton("등록");
		c.add(this.btnInsert);
		this.btnInsert.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				try {
					//oracle jdbc드라이버 로드
					Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load
					//Connection
					Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ora_user","hong");// 연결
					System.out.println("연결완료");
					Statement stmt=conn.createStatement();
					
					//insert
					stmt.executeUpdate("insert into student(id,name,dept) values('"+tfId.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");		
					
					//목록초기화
					taList.setText("");
					ResultSet rs=stmt.executeQuery("select * from student");
					
					taList.append("학번\t");
					taList.append("이름\t");
					taList.append("학과\n");
					taList.append("================================================\n");
					while(rs.next()) {
//						System.out.println(rs.getString("name"));
//						System.out.println(rs.getString("id"));
//						System.out.println(rs.getString("dept"));
						taList.append(rs.getString("id")+"\t");
						taList.append(rs.getString("name")+"\t");
						taList.append(rs.getString("dept")+"\n");
					}	
					rs.close();
					stmt.close();
					conn.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 {
					//oracle jdbc드라이버 로드
					Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load
					//Connection
					Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ora_user","hong");// 연결
					System.out.println("연결완료");
					Statement stmt=conn.createStatement();				
					
					//목록초기화
					taList.setText("");
					ResultSet rs=stmt.executeQuery("select * from student");
					
					taList.append("학번\t");
					taList.append("이름\t");
					taList.append("학과\n");
					taList.append("================================================\n");
					while(rs.next()) {
//						System.out.println(rs.getString("name"));
//						System.out.println(rs.getString("id"));
//						System.out.println(rs.getString("dept"));
						taList.append(rs.getString("id")+"\t");
						taList.append(rs.getString("name")+"\t");
						taList.append(rs.getString("dept")+"\n");
					}	
					rs.close();
					stmt.close();
					conn.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 {
					//oracle jdbc드라이버 로드
					Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load
					//Connection
					Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ora_user","hong");// 연결
					System.out.println("연결완료");
					Statement stmt=conn.createStatement();					
					
					//update
					stmt.executeUpdate("update student set name='"+tfName.getText()+"',dept='"+tfDepartment.getText()+"' where id='"+tfId.getText()+"'");
					
					//delete
					//stmt.executeUpdate("delete from student where id='0893012'");
					
					
					//목록초기화
					taList.setText("");
					
					
					ResultSet rs=stmt.executeQuery("select * from student");
					
					taList.append("학번\t");
					taList.append("이름\t");
					taList.append("학과\n");
					taList.append("================================================\n");
					while(rs.next()) {
//						System.out.println(rs.getString("name"));
//						System.out.println(rs.getString("id"));
//						System.out.println(rs.getString("dept"));
						taList.append(rs.getString("id")+"\t");
						taList.append(rs.getString("name")+"\t");
						taList.append(rs.getString("dept")+"\n");
					}	
					rs.close();
					stmt.close();
					conn.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 {
						//oracle jdbc드라이버 로드
						Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load
						//Connection
						Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ora_user","hong");// 연결
						System.out.println("연결완료");
						Statement stmt=conn.createStatement();					
												
						//delete
						stmt.executeUpdate("delete from student where id='"+tfId.getText()+"'");
												
						//목록초기화
						taList.setText("");
						
						
						ResultSet rs=stmt.executeQuery("select * from student");
						
						taList.append("학번\t");
						taList.append("이름\t");
						taList.append("학과\n");
						taList.append("================================================\n");
						while(rs.next()) {
//							System.out.println(rs.getString("name"));
//							System.out.println(rs.getString("id"));
//							System.out.println(rs.getString("dept"));
							taList.append(rs.getString("id")+"\t");
							taList.append(rs.getString("name")+"\t");
							taList.append(rs.getString("dept")+"\n");
						}	
						rs.close();
						stmt.close();
						conn.close();
					}catch(Exception e1) {
						e1.printStackTrace();
					}
				}
			}});
		
		this.setSize(300, 500);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new Haksa();
	}

}

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

04/07 bookrent  (0) 2021.04.07
04/07 haksa  (0) 2021.04.07
oracle 연결  (0) 2021.04.06
년도,상품별 판매갯수합계 판매되지 않은 상품도 출력하기  (0) 2021.03.31
자바연습문제풀이  (0) 2021.03.29
Comments