Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mange;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import javax.swing.*;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.PlainDocument;
- class MemberFrame extends JFrame {
- ImageIcon icon;
- JPanel p,buttonPanel;
- JTextField[] tf=new JTextField[4];
- JLabel[] label=new JLabel[4];
- String[] s={"ID:","이름:","주소:","전화번호:"};
- JButton[] button=new JButton[4];
- String[] b={" 회원조회 ","회원수정","회원삭제"," 추가 " };
- JTextArea ta;
- JScrollPane sp;
- String p_id;
- String p_name;
- String p_addr;
- String p_tel;
- int a;
- Connection con = null;
- Statement stmt = null;
- ResultSet rs=null;
- public MemberFrame(){
- this.setTitle("회원관리");
- // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //this.setSize(300, 200);
- this.createComponent();
- this.addComponent();
- this.pack();
- this.setVisible(true);
- }
- public void createComponent(){
- MemberHandler mh=new MemberHandler();
- p=new JPanel(new GridLayout(0,2));
- buttonPanel=new JPanel();
- icon = new ImageIcon("3.jpg");
- for(int i=0;i<4;i++){
- tf[i]=new JTextField();
- label[i]=new JLabel(s[i]);
- }
- tf[0].addActionListener(mh);
- for(int i=0;i<4;i++){
- button[i]=new JButton(b[i]);
- button[i].addActionListener(mh);
- }
- ta=new JTextArea(10,40);
- ta.setEditable(false);
- ta.setFont(new Font("돋움", Font.BOLD, 20));
- ta.setBackground(Color.LIGHT_GRAY);
- sp=new JScrollPane(ta);
- }
- public void addComponent(){
- for(int i=0;i<4;i++){
- p.add(label[i]);
- label[i].setFont(new Font("돋움", Font.BOLD, 17));
- p.add(tf[i]);
- }
- for(int i=0;i<4;i++){
- buttonPanel.add(button[i]);
- }
- this.add(p,BorderLayout.NORTH);
- this.add(sp,BorderLayout.CENTER);
- this.add(buttonPanel,BorderLayout.SOUTH);
- }
- class MemberHandler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- makeConnection();
- String sql="";
- try{
- stmt=con.createStatement();
- if(e.getSource()==tf[0]){
- if(isExist()){
- getData();
- tf[1].setText(p_name);
- tf[2].setText(p_addr);
- tf[3].setText(p_tel);
- }else{
- tf[1].requestFocus();
- }
- }
- if(e.getSource()==button[0]){
- viewData();
- }
- else if(e.getSource()==button[1]){
- updateData();
- viewData();
- }
- else if(e.getSource()==button[2]){
- deleteData();
- viewData();
- }
- else if(e.getSource()==button[3]){
- INSERTData();
- viewData();
- }
- }catch(SQLException sqle){System.out.println(sqle.getMessage());}
- disConnection();
- }
- }
- public void viewData() throws SQLException{
- String rows;
- String sql="";
- ta.setText("");
- sql="SELECT * FROM members order by id";
- try{
- rs=stmt.executeQuery(sql);
- rs.last();
- int count=rs.getRow();
- System.out.println("count="+count);
- rs.beforeFirst();
- while(rs.next()){
- getData();
- rows="ID: "+p_id.toUpperCase()+" 이름: "+p_name.toUpperCase()+" :주소 "+p_addr+" H.P: "+p_tel+"\n";
- ta.append(rows);
- }
- }catch(SQLException sqle){System.out.println("viewData: SQL Error");}
- clearTextField();
- tf[0].requestFocus();
- }
- public void updateData() throws SQLException{
- setData();
- String sql="";
- sql="UPDATE members SET name='"+p_name+"' WHERE id='"+p_id+"'";
- System.out.println(sql);
- stmt.executeUpdate(sql);
- sql="UPDATE members SET address='"+p_addr+"' WHERE id='"+p_id+"'";
- System.out.println(sql);
- stmt.executeUpdate(sql);
- sql="UPDATE members SET tel='"+p_tel+"' WHERE id='"+p_id+"'";
- System.out.println(sql);
- stmt.executeUpdate(sql);
- clearTextField();
- tf[0].requestFocus();
- }
- public void deleteData() throws SQLException{
- setData();
- String sql="";
- sql="DELETE FROM members WHERE id='"+p_id+"'";
- System.out.println(sql);
- stmt.executeUpdate(sql);
- clearTextField();
- tf[0].requestFocus();
- }
- public void INSERTData() throws SQLException{
- setData();
- String sql="";
- sql="INSERT INTO members (id,name,address,tel) values ";
- sql+="('"+p_id.toUpperCase()+"','"+p_name.toUpperCase()+"','"+p_addr+"','"+p_tel+"')";
- System.out.println(sql);
- int isINSERTED=stmt.executeUpdate(sql);
- stmt.executeUpdate("set character_set_results=utf8");
- if(isINSERTED==1)
- System.out.println("INSERT Successfully.");
- else
- System.out.println("INSERT Failed.");
- clearTextField();
- tf[0].requestFocus();
- }
- public boolean isExist(){
- String sql="";
- boolean isExist=false;
- sql="SELECT * FROM members WHERE id='"+tf[0].getText()+"'";
- try{
- rs=stmt.executeQuery(sql);
- if(rs.next())
- isExist=true;
- }catch(SQLException sqle){System.out.println("isExist: SQL Error");}
- return isExist;
- }
- public void getData() throws SQLException {
- p_id=fromMySQL(rs.getString("id"));
- p_name=fromMySQL(rs.getString("name"));
- p_addr=fromMySQL(rs.getString("address"));
- p_tel=fromMySQL(rs.getString("tel"));
- }
- public void setData() throws SQLException {
- p_id=toMySQL(tf[0].getText());
- p_name=toMySQL(tf[1].getText());
- p_addr=toMySQL(tf[2].getText());
- p_tel=toMySQL(tf[3].getText());
- }
- public void clearTextField(){
- for(int i=0;i<4;i++){
- tf[i].setText("");
- }
- }
- public String toMySQL(String str){
- try{
- if (str != null)
- return new String(str.getBytes("KSC5601"), "8859_1");
- else
- return null;
- } catch (Exception e) {e.printStackTrace();return null;}
- }
- public String fromMySQL(String str){
- try{
- if (str != null)
- return new String(str.getBytes("8859_1"),"KSC5601");
- else
- return null;
- } catch (Exception e) {e.printStackTrace();return null;}
- }
- public Connection makeConnection(){
- String url="jdbc:mysql://localhost/comicbook_db";
- String id="root";
- String password="twiceioi";
- try{
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("드라이브 적재 성공");
- con=DriverManager.getConnection(url, id, password);
- System.out.println("데이터베이스 연결 성공");
- }catch(ClassNotFoundException e){
- System.out.println("드라이버를 찾을 수 없습니다");
- e.getStackTrace();
- }catch(SQLException e){
- System.out.println("연결에 실패하였습니다");
- }
- return con;
- }
- public void disConnection() {
- try{
- rs.close();
- stmt.close();
- con.close();
- }catch(SQLException e){System.out.println(e.getMessage());}
- }
- }
- public class Members {
- public static void main(String[] args){
- new MemberFrame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement