관리 메뉴

moozi

haksa 1/31 본문

TIS_2018/응용sw2018_1기

haksa 1/31

moozi 2018. 3. 16. 13:31

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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Haksa extends JFrame{
 
 Connection conn;
 
 public Haksa() {
  this.setTitle("학사관리 시스템");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container c=getContentPane();
  c.setLayout(new FlowLayout());
  
  // 새로 추가된 내용. 학번.
  c.add(new JLabel("학번"));
  JTextField tfID=new JTextField(20);
  c.add(tfID);
  
  c.add(new JLabel("이름"));
  JTextField tfName=new JTextField(20);
  c.add(tfName);
  c.add(new JLabel("학과"));
  JTextField tfDepartment=new JTextField(20);
  c.add(tfDepartment);
  c.add(new JLabel("주소"));
  JTextField tfAddress=new JTextField(20);
  c.add(tfAddress);
  
  //목록
  JTextArea ta=new JTextArea(7,23);
  c.add(new JScrollPane(ta));
  
  //버튼추가
  JButton btnInsert=new JButton("등록");
  c.add(btnInsert);
  btnInsert.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    try {
     
     //Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb","root","1234");
     //System.out.println("연결되었습니다");
     Statement stmt=conn.createStatement();
     
     stmt.executeUpdate("insert into student values('"+tfID.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");
     //stmt.executeUpdate("update student set name='왕건' where id='1234555'");
     //stmt.executeUpdate("delete from student where id='1234555'");
     
     ResultSet rs=stmt.executeQuery("select * from student order by name");
     
     System.out.print("id\t");
     System.out.print("name\t");
     System.out.println("dept");
     System.out.println("========================================");
     
     ta.append("id\t");
     ta.append("name\t");
     ta.append("dept\n");
     ta.append("=========================================\n");
     
     while(rs.next()) {
      System.out.print(rs.getString("id")+"\t");
      System.out.print(rs.getString("name")+"\t");
      System.out.println(rs.getString("dept"));
      
      ta.append(rs.getString("id")+"\t");
      ta.append(rs.getString("name")+"\t");
      ta.append(rs.getString("dept")+"\n");
     }
     
    }catch(Exception ex) {
     ex.printStackTrace();
    }
    
    
    
   }});
  
  
  JButton btnSelect=new JButton("목록");
  c.add(btnSelect);
  btnSelect.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent arg0) {
    try {
     Class.forName("com.mysql.jdbc.Driver");
     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb","root","1234");
     //System.out.println("연결되었습니다");
     Statement stmt=conn.createStatement();
     
     //stmt.executeUpdate("insert into student values('"+tfID.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");
     //stmt.executeUpdate("update student set name='왕건' where id='1234555'");
     //stmt.executeUpdate("delete from student where id='1234555'");
     
     ResultSet rs=stmt.executeQuery("select * from student");
     
     System.out.print("id\t");
     System.out.print("name\t");
     System.out.println("dept");
     System.out.println("========================================");
     
     ta.append("id\t");
     ta.append("name\t");
     ta.append("dept\n");
     ta.append("=========================================\n");
     
     while(rs.next()) {
      System.out.print(rs.getString("id")+"\t");
      System.out.print(rs.getString("name")+"\t");
      System.out.println(rs.getString("dept"));
      
      ta.append(rs.getString("id")+"\t");
      ta.append(rs.getString("name")+"\t");
      ta.append(rs.getString("dept")+"\n");
     }
     
    }catch(Exception e) {
     e.printStackTrace();
    }
    
    
   }});
  
  JButton btnUpdate=new JButton("수정");
  c.add(btnUpdate);
  JButton btnDelete=new JButton("삭제");
  c.add(btnDelete);
  
  
  this.setSize(300,350);
  this.setVisible(true);
  
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb","root","1234");
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  
  /*
  try {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb","root","1234");
   //System.out.println("연결되었습니다");
   Statement stmt=conn.createStatement();
   
   //stmt.executeUpdate("insert into student values('1234555','아무개','컴퓨터공학')");
   //stmt.executeUpdate("update student set name='왕건' where id='1234555'");
   stmt.executeUpdate("delete from student where id='1234555'");
   
   ResultSet rs=stmt.executeQuery("select * from student");
   
   System.out.print("id\t");
   System.out.print("name\t");
   System.out.println("dept");
   System.out.println("========================================");
   
   ta.append("id\t");
   ta.append("name\t");
   ta.append("dept\n");
   ta.append("=========================================\n");
   
   while(rs.next()) {
    System.out.print(rs.getString("id")+"\t");
    System.out.print(rs.getString("name")+"\t");
    System.out.println(rs.getString("dept"));
    
    ta.append(rs.getString("id")+"\t");
    ta.append(rs.getString("name")+"\t");
    ta.append(rs.getString("dept")+"\n");
   }
   
  }catch(Exception e) {
   e.printStackTrace();
  }
  */
  
  
 }
 public static void main(String[] args) {
  new Haksa();

 }

}

'TIS_2018 > 응용sw2018_1기' 카테고리의 다른 글

haksa 3.16  (0) 2018.03.19
자바연습문제15  (0) 2018.03.16
정규화  (0) 2018.03.15
erd  (0) 2018.03.15
오라클연습문제07  (0) 2018.03.14
Comments