Advertisement
Guest User

kmin

a guest
Dec 12th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. package mange;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.sql.*;
  6. import javax.swing.*;
  7.  
  8. import javax.swing.text.BadLocationException;
  9. import javax.swing.text.PlainDocument;
  10.  
  11. class MemberFrame extends JFrame {
  12.     ImageIcon icon;
  13.     JPanel p,buttonPanel;
  14.     JTextField[] tf=new JTextField[4];
  15.     JLabel[] label=new JLabel[4];
  16.     String[] s={"ID:","이름:","주소:","전화번호:"};
  17.     JButton[] button=new JButton[4];
  18.     String[] b={" 회원조회 ","회원수정","회원삭제"," 추가 "   };
  19.     JTextArea ta;
  20.     JScrollPane sp;
  21.  
  22.     String p_id;
  23.     String p_name;
  24.     String p_addr;
  25.     String p_tel;
  26.    
  27.     int a;
  28.  
  29.     Connection con = null;
  30.     Statement stmt = null;
  31.     ResultSet rs=null;
  32.  
  33.     public MemberFrame(){
  34.         this.setTitle("회원관리");
  35. //      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.         //this.setSize(300, 200);
  37.         this.createComponent();
  38.         this.addComponent();
  39.         this.pack();
  40.         this.setVisible(true);
  41.     }
  42.  
  43.     public void createComponent(){
  44.         MemberHandler mh=new MemberHandler();
  45.         p=new JPanel(new GridLayout(0,2));
  46.         buttonPanel=new JPanel();
  47.         icon = new ImageIcon("3.jpg");
  48.         for(int i=0;i<4;i++){
  49.             tf[i]=new JTextField();
  50.             label[i]=new JLabel(s[i]);
  51.         }
  52.         tf[0].addActionListener(mh);
  53.         for(int i=0;i<4;i++){
  54.             button[i]=new JButton(b[i]);
  55.             button[i].addActionListener(mh);
  56.         }
  57.         ta=new JTextArea(10,40);
  58.         ta.setEditable(false);
  59.         ta.setFont(new Font("돋움", Font.BOLD, 20));
  60.         ta.setBackground(Color.LIGHT_GRAY);
  61.         sp=new JScrollPane(ta);
  62.     }
  63.  
  64.     public void addComponent(){
  65.         for(int i=0;i<4;i++){
  66.             p.add(label[i]);
  67.             label[i].setFont(new Font("돋움", Font.BOLD, 17));
  68.             p.add(tf[i]);
  69.         }
  70.         for(int i=0;i<4;i++){
  71.             buttonPanel.add(button[i]);
  72.         }
  73.         this.add(p,BorderLayout.NORTH);
  74.         this.add(sp,BorderLayout.CENTER);
  75.         this.add(buttonPanel,BorderLayout.SOUTH);
  76.     }
  77.  
  78.     class MemberHandler implements ActionListener {
  79.         public void actionPerformed(ActionEvent e) {
  80.             makeConnection();
  81.             String sql="";
  82.             try{
  83.                 stmt=con.createStatement();
  84.                 if(e.getSource()==tf[0]){
  85.                     if(isExist()){
  86.                         getData();
  87.                         tf[1].setText(p_name);
  88.                         tf[2].setText(p_addr);
  89.                         tf[3].setText(p_tel);
  90.                     }else{
  91.                         tf[1].requestFocus();
  92.                     }
  93.                 }
  94.                 if(e.getSource()==button[0]){
  95.                     viewData();
  96.                 }
  97.                 else if(e.getSource()==button[1]){
  98.                     updateData();
  99.                     viewData();
  100.                 }
  101.                 else if(e.getSource()==button[2]){
  102.                     deleteData();
  103.                     viewData();
  104.                 }
  105.                 else if(e.getSource()==button[3]){
  106.                     INSERTData();
  107.                     viewData();
  108.                 }
  109.             }catch(SQLException sqle){System.out.println(sqle.getMessage());}
  110.             disConnection();
  111.         }
  112.     }
  113.  
  114.     public void viewData() throws SQLException{
  115.         String rows;
  116.         String sql="";
  117.         ta.setText("");
  118.         sql="SELECT * FROM members order by id";
  119.         try{
  120.             rs=stmt.executeQuery(sql);
  121.             rs.last();
  122.             int count=rs.getRow();
  123.             System.out.println("count="+count);
  124.             rs.beforeFirst();
  125.             while(rs.next()){
  126.                 getData();
  127.                 rows="ID: "+p_id.toUpperCase()+" 이름: "+p_name.toUpperCase()+" :주소 "+p_addr+" H.P: "+p_tel+"\n";
  128.                 ta.append(rows);           
  129.             }
  130.         }catch(SQLException sqle){System.out.println("viewData: SQL Error");}
  131.         clearTextField();
  132.         tf[0].requestFocus();      
  133.     }
  134.  
  135.     public void updateData() throws SQLException{
  136.         setData();
  137.         String sql="";
  138.         sql="UPDATE members SET name='"+p_name+"' WHERE id='"+p_id+"'";
  139.         System.out.println(sql);
  140.         stmt.executeUpdate(sql);
  141.         sql="UPDATE members SET address='"+p_addr+"' WHERE id='"+p_id+"'";
  142.         System.out.println(sql);
  143.         stmt.executeUpdate(sql);
  144.         sql="UPDATE members SET tel='"+p_tel+"' WHERE id='"+p_id+"'";
  145.         System.out.println(sql);
  146.         stmt.executeUpdate(sql);
  147.         clearTextField();
  148.         tf[0].requestFocus();
  149.     }
  150.  
  151.     public void deleteData() throws SQLException{
  152.         setData();
  153.         String sql="";
  154.         sql="DELETE FROM members WHERE id='"+p_id+"'";
  155.         System.out.println(sql);
  156.         stmt.executeUpdate(sql);
  157.         clearTextField();
  158.         tf[0].requestFocus();
  159.     }
  160.  
  161.     public void INSERTData() throws SQLException{
  162.         setData();
  163.         String sql="";
  164.         sql="INSERT INTO members (id,name,address,tel) values ";
  165.         sql+="('"+p_id.toUpperCase()+"','"+p_name.toUpperCase()+"','"+p_addr+"','"+p_tel+"')";
  166.         System.out.println(sql);
  167.         int isINSERTED=stmt.executeUpdate(sql);
  168.         stmt.executeUpdate("set character_set_results=utf8");
  169.         if(isINSERTED==1)
  170.             System.out.println("INSERT Successfully.");
  171.         else
  172.             System.out.println("INSERT Failed.");
  173.         clearTextField();
  174.         tf[0].requestFocus();  
  175.     }
  176.     public boolean isExist(){
  177.         String sql="";
  178.         boolean isExist=false;
  179.         sql="SELECT * FROM members WHERE id='"+tf[0].getText()+"'";
  180.         try{
  181.             rs=stmt.executeQuery(sql);
  182.             if(rs.next())
  183.                 isExist=true;
  184.         }catch(SQLException sqle){System.out.println("isExist: SQL Error");}
  185.         return isExist;
  186.     }
  187.  
  188.     public void getData() throws SQLException {
  189.         p_id=fromMySQL(rs.getString("id"));
  190.         p_name=fromMySQL(rs.getString("name"));
  191.         p_addr=fromMySQL(rs.getString("address"));
  192.         p_tel=fromMySQL(rs.getString("tel"));
  193.     }
  194.  
  195.     public void setData() throws SQLException {
  196.         p_id=toMySQL(tf[0].getText());
  197.         p_name=toMySQL(tf[1].getText());
  198.         p_addr=toMySQL(tf[2].getText());
  199.         p_tel=toMySQL(tf[3].getText());    
  200.     }
  201.  
  202.     public void clearTextField(){
  203.         for(int i=0;i<4;i++){
  204.             tf[i].setText("");
  205.         }
  206.     }
  207.  
  208.     public String toMySQL(String str){
  209.         try{
  210.             if (str != null)
  211.                 return new String(str.getBytes("KSC5601"), "8859_1");
  212.             else
  213.                 return null;
  214.         } catch (Exception e) {e.printStackTrace();return null;}
  215.     }
  216.  
  217.     public String fromMySQL(String str){
  218.         try{
  219.             if (str != null)
  220.                 return new String(str.getBytes("8859_1"),"KSC5601");
  221.             else
  222.                 return null;
  223.         } catch (Exception e) {e.printStackTrace();return null;}
  224.     }
  225.  
  226.     public Connection makeConnection(){
  227.         String url="jdbc:mysql://localhost/comicbook_db";
  228.         String id="root";
  229.         String password="twiceioi";
  230.         try{
  231.             Class.forName("com.mysql.jdbc.Driver");
  232.             System.out.println("드라이브 적재 성공");
  233.             con=DriverManager.getConnection(url, id, password);
  234.             System.out.println("데이터베이스 연결 성공");
  235.         }catch(ClassNotFoundException e){
  236.             System.out.println("드라이버를 찾을 수 없습니다");
  237.             e.getStackTrace();
  238.         }catch(SQLException e){
  239.             System.out.println("연결에 실패하였습니다");         
  240.         }
  241.         return con;
  242.     }
  243.  
  244.     public void disConnection() {
  245.         try{
  246.             rs.close();
  247.             stmt.close();
  248.             con.close();
  249.         }catch(SQLException e){System.out.println(e.getMessage());}
  250.     }
  251.  
  252. }
  253.  
  254. public class Members {
  255.     public static void main(String[] args){
  256.         new MemberFrame();
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement