ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 31일차 Swing을 이용한 회원정보 입출력
    삼성SDS_멀티캠퍼스/MYSQL 2015. 10. 22. 14:08
    반응형


    package model;


    import java.sql.Date;


    public class BoardText {

    private int id;

    private String title;

    private String descri;

    private String author;

    private Date date;


    public int getId() {

    return id;

    }


    public void setId(int id) {

    this.id = id;

    }


    public String getTitle() {

    return title;

    }


    public void setTitle(String title) {

    this.title = title;

    }


    public String getDescri() {

    return descri;

    }


    public void setDescri(String descri) {

    this.descri = descri;

    }


    public String getAuthor() {

    return author;

    }


    public void setAuthor(String author) {

    this.author = author;

    }


    public Date getDate() {

    return date;

    }


    public void setDate(Date date) {

    this.date = date;

    }


    @Override

    public String toString() {

    return "BoardText [id=" + id + ", title=" + title + ", desc=" + descri + ", author=" + author + ", date=" + date

    + "]";

    }


    }








    package dao;


    import java.sql.Connection;

    import java.sql.Date;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.text.SimpleDateFormat;

    import java.util.ArrayList;

    import java.util.List;

    import model.BoardText;

    import test.MyFrame;;


    public class BoardTextDao {

    public static int result;

    private static BoardTextDao instance;

    private Connection connection;

    MyFrame m = null;

    private BoardTextDao() {

    String url = "jdbc:mysql://localhost:3306/sds2";

    String user = "root";

    String password = "mysql";

    try {

    connection = DriverManager.getConnection(url, user, password);

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }


    public static BoardTextDao getInstance() {


    if (instance == null)

    instance = new BoardTextDao();

    return instance;

    }

    public void insertBoardText(BoardText boardText) {

    PreparedStatement pstmt = null;

    try {

    pstmt = connection.prepareStatement("insert into BoardText values(?,?,?,?,?)");

    pstmt.setInt(1, boardText.getId());

    pstmt.setString(2, boardText.getTitle());

    pstmt.setString(3, boardText.getDescri());

    pstmt.setString(4, boardText.getAuthor());

    pstmt.setDate(5, boardText.getDate());

    result = pstmt.executeUpdate();

    result++;

    } catch (SQLException e) {

    e.printStackTrace();

    } finally {

    if (pstmt != null)

    try {

    pstmt.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }


    public void updateBoardText(BoardText boardText) {

    PreparedStatement pstmt = null;


    try {

    pstmt = connection.prepareStatement("update BoardText set title =?, descri =?, author =? where id =?");

    pstmt.setInt(4, boardText.getId());

    pstmt.setString(1, boardText.getTitle());

    pstmt.setString(2, boardText.getDescri());

    pstmt.setString(3, boardText.getAuthor());

    result = pstmt.executeUpdate();

    result++;

    } catch (SQLException e) {

    e.printStackTrace();

    } finally {

    if (pstmt != null)

    try {

    pstmt.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }


    public void deleteBoardText(int id) {

    PreparedStatement pstmt = null;


    try {

    pstmt = connection.prepareStatement("delete from BoardText where id=?");

    pstmt.setInt(1, id);

    result = pstmt.executeUpdate();

    result++;

    } catch (SQLException e) {

    e.printStackTrace();

    } finally {

    if (pstmt != null)

    try {

    pstmt.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }


    public BoardText selectOne(int id) {

    PreparedStatement pstmt = null;

    ResultSet resultSet = null;

    BoardText boardText = new BoardText();

    try {

    pstmt = connection.prepareStatement("select * from BoardText where id = ?");

    pstmt.setInt(1, id);

    resultSet = pstmt.executeQuery();


    if (resultSet.next()) {

    boardText.setId(resultSet.getInt("id"));

    boardText.setTitle(resultSet.getString("title"));

    boardText.setDescri(resultSet.getString("descri"));

    boardText.setAuthor(resultSet.getString("author"));

    boardText.setDate(resultSet.getDate("date"));

    result++;

    }else if (!resultSet.next()) {

    result=0;

    }

    } catch (SQLException e) {

    e.printStackTrace();

    } finally {

    try {

    if (pstmt != null)

    pstmt.close();

    if (resultSet != null)

    resultSet.close();

    } catch (SQLException e) {

    }

    }

    return boardText;

    }


    public List<BoardText> selectAll() {

    PreparedStatement pstmt = null;

    ResultSet resultSet = null;

    List<BoardText> list = new ArrayList<BoardText>();

    try {

    pstmt = connection.prepareStatement("select * from BoardText");

    resultSet = pstmt.executeQuery();

    while (resultSet.next()) {

    BoardText boardText = new BoardText();

    boardText.setId(resultSet.getInt("id"));

    boardText.setTitle(resultSet.getString("title"));

    boardText.setDescri(resultSet.getString("descri"));

    boardText.setAuthor(resultSet.getString("author"));

    boardText.setDate(resultSet.getDate("date"));

    list.add(boardText);

    result++;

    }

    for (BoardText b : list) 

    System.out.println(b);

    } catch (SQLException e) {

    e.printStackTrace();

    } finally {

    try {

    if (pstmt != null)

    pstmt.close();

    if (resultSet != null)

    resultSet.close();

    } catch (SQLException e) {

    }

    }

    return list;

    }

    }




    package test;


    import java.awt.Color;

    import java.awt.Component;

    import java.awt.Font;

    import java.awt.SystemColor;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.awt.event.AdjustmentEvent;

    import java.awt.event.AdjustmentListener;

    import java.sql.Date;

    import java.util.List;

    import java.util.Scanner;


    import javax.swing.Box;

    import javax.swing.ImageIcon;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JList;

    import javax.swing.JScrollBar;

    import javax.swing.JScrollPane;

    import javax.swing.JTextArea;

    import javax.swing.JTextField;

    import javax.swing.SwingConstants;


    import dao.BoardTextDao;

    import model.BoardText;


    public class MyFrame extends JFrame implements ActionListener {

    private JTextField textField;

    private JTextField textField_1;

    private JTextField textField_2;

    private JTextField textField_3;

    private JTextArea area;

    private JButton button, button_1, button_2, button_3, button_4;

    private JLabel lblD;


    static BoardTextDao dao = BoardTextDao.getInstance();

    static BoardText boardText = new BoardText();

    static Scanner scan = new Scanner(System.in);

    static int id = 0;

    static String title = null;

    static String descri = null;

    static String author = null;

    static Date date = null;

    private JLabel lblNewLabel;

    private JButton button_5;


    public MyFrame() {


    getContentPane().setBackground(SystemColor.activeCaption);

    setBackground(SystemColor.activeCaption);

    getContentPane().setLayout(null);


    JLabel Id = new JLabel("ID");

    Id.setFont(new Font("나눔고딕 ExtraBold", Font.PLAIN, 14));

    Id.setForeground(Color.ORANGE);

    Id.setBounds(46, 56, 33, 33);

    getContentPane().add(Id);

    Id.setHorizontalAlignment(SwingConstants.CENTER);


    Component horizontalStrut = Box.createHorizontalStrut(20);

    horizontalStrut.setBounds(0, 0, 386, 1);

    getContentPane().add(horizontalStrut);


    JLabel lblTitle = new JLabel("Title");

    lblTitle.setFont(new Font("나눔고딕 ExtraBold", Font.PLAIN, 14));

    lblTitle.setForeground(Color.ORANGE);

    lblTitle.setBounds(46, 105, 33, 33);

    getContentPane().add(lblTitle);

    lblTitle.setHorizontalAlignment(SwingConstants.CENTER);


    JLabel lblContent = new JLabel("Content");

    lblContent.setFont(new Font("나눔고딕 ExtraBold", Font.PLAIN, 14));

    lblContent.setForeground(Color.ORANGE);

    lblContent.setBounds(31, 155, 64, 33);

    getContentPane().add(lblContent);

    lblContent.setHorizontalAlignment(SwingConstants.CENTER);


    JLabel lblDesc = new JLabel("Author");

    lblDesc.setFont(new Font("나눔고딕 ExtraBold", Font.PLAIN, 14));

    lblDesc.setForeground(Color.ORANGE);

    lblDesc.setBounds(31, 206, 71, 33);

    getContentPane().add(lblDesc);

    lblDesc.setHorizontalAlignment(SwingConstants.CENTER);


    textField_3 = new JTextField();

    textField_3.setBounds(114, 209, 226, 28);

    getContentPane().add(textField_3);

    textField_3.setColumns(10);


    textField_2 = new JTextField();

    textField_2.setBounds(114, 158, 226, 28);

    getContentPane().add(textField_2);

    textField_2.setColumns(10);


    textField_1 = new JTextField();

    textField_1.setBounds(114, 108, 226, 28);

    getContentPane().add(textField_1);

    textField_1.setColumns(10);


    textField = new JTextField();

    textField.setBounds(114, 59, 226, 28);

    getContentPane().add(textField);

    textField.setColumns(10);

    JScrollPane scrollPane = new JScrollPane();

    scrollPane.setBounds(12, 288, 540, 149);

    getContentPane().add(scrollPane);


    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


    area = new JTextArea();

    scrollPane.setViewportView(area);

    area.setLineWrap(true);

    area.setEditable(false);

    scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

    public void adjustmentValueChanged(AdjustmentEvent e) {

    JScrollBar src = (JScrollBar) e.getSource();

    int pos = area.getText().length();

    area.setCaretPosition(pos);

    }

    });


    button = new JButton("입력\r\n");

    button.setBounds(394, 10, 158, 23);

    getContentPane().add(button);

    button.addActionListener(this);


    button_1 = new JButton("수정");

    button_1.setBounds(394, 60, 158, 23);

    getContentPane().add(button_1);

    button_1.addActionListener(this);


    button_2 = new JButton("삭제");

    button_2.setBounds(394, 110, 158, 23);

    getContentPane().add(button_2);

    button_2.addActionListener(this);


    button_3 = new JButton("조회");

    button_3.setBounds(394, 160, 158, 23);

    getContentPane().add(button_3);

    button_3.addActionListener(this);


    button_4 = new JButton("전체조회");

    button_4.setBounds(394, 210, 158, 23);

    getContentPane().add(button_4);

    button_4.addActionListener(this);


    button_5 = new JButton("전체삭제");

    button_5.setBounds(394, 255, 158, 23);

    getContentPane().add(button_5);

    button_5.addActionListener(this);


    lblD = new JLabel("결과창\r\n");

    lblD.setIcon(null);

    lblD.setFont(new Font("굴림", Font.PLAIN, 15));

    lblD.setHorizontalAlignment(SwingConstants.CENTER);

    lblD.setForeground(Color.ORANGE);

    lblD.setBounds(43, 249, 351, 35);

    getContentPane().add(lblD);


    lblNewLabel = new JLabel("");

    lblNewLabel.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\aurora2.jpg"));

    lblNewLabel.setBounds(0, 0, 564, 449);

    getContentPane().add(lblNewLabel);


    this.setTitle("우기 우기");

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);

    this.setSize(580, 487);

    }


    public static void main(String[] args) {

    new MyFrame();

    }


    @Override

    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == button) {


    boardText.setId(0);

    boardText.setTitle(String.valueOf(textField_1.getText()));

    boardText.setDescri(String.valueOf(textField_2.getText()));

    boardText.setAuthor(String.valueOf(textField_3.getText()));

    dao.insertBoardText(boardText);

    if (dao.result > 0) {

    lblD.setText("입력 성공");

    textField.setText("");

    } else if (dao.result == 0)

    lblD.setText("입력 실패(이미 존재하는 ID인지 확인하세요)");

    } else if (e.getSource() == button_1) {

    if (textField.getText().equals("")) {

    lblD.setText("수정 하려는 ID를 입력하세요");

    area.setText("");

    } else {


    boardText.setTitle(String.valueOf(textField_1.getText()));

    boardText.setDescri(String.valueOf(textField_2.getText()));

    boardText.setAuthor(String.valueOf(textField_3.getText()));

    boardText.setId(Integer.parseInt(textField.getText()));

    dao.updateBoardText(boardText);

    if (dao.result > 0) {

    lblD.setText("수정 성공");

    textField.setText("");

    } else if (dao.result == 0)

    lblD.setText("수정 실패");

    }

    } else if (e.getSource() == button_2) {

    if (textField.getText().equals("")) {

    lblD.setText("삭제 하려는 ID를 입력하세요");

    area.setText("");

    } else {

    boardText.setId(Integer.parseInt(textField.getText()));

    dao.deleteBoardText(boardText.getId());

    if (dao.result > 0) {

    lblD.setText("삭제 성공");

    textField.setText("");

    } else

    lblD.setText("삭제 실패");

    }

    } else if (e.getSource() == button_3) {

    if (textField.getText().equals("")) {

    area.setText("");

    lblD.setText("조회 하려는 ID를 입력하세요");

    } else {

    boardText.setId(Integer.parseInt(textField.getText()));

    area.setText(dao.selectOne(boardText.getId()).toString());

    if (dao.result > 0) {

    lblD.setText("조회 성공");

    } else {

    lblD.setText("조회 실패 (레코드 ID를 확인해주세요)");

    area.setText("");

    }

    }

    } else if (e.getSource() == button_4) {

    area.setText("");

    dao.selectAll();

    List<BoardText> list = dao.selectAll();

    for (BoardText i : list) {

    area.append(i.toString());

    area.append("\n");

    }

    if (list.size() > 0)

    lblD.setText("전체조회 성공");

    else if (area.getText().trim().equals(""))

    lblD.setText("전체조회 실패");

    } else if (e.getSource() == button_5) {

    area.setText("");

    dao.AlldeleteBoardText();

    if (dao.result > 0)

    lblD.setText("전체삭제 성공");

    else

    lblD.setText("전체삭제 실패");

    }

    }

    }



















    DB에 입력을 할 때는 자동으로 ID번호가 증가합니다.


    수정은 기존에 입력된 ID번호만 제대로 적어서 내용변경후 수정 누르면 되고


    삭제도 기존에 입력된 ID번호만 쓰면 삭제되고


    조회도 기존에 입력된 ID번호만 쓰면 레코드 한개만 조회됩니다.


    전체조회는 모든 레코드를 조회합니다.


    전체삭제는 모든 레코드를 삭제합니다.


    전과 같이 프로젝트에 lib폴더 만들어서 mysql.jar넣으시고 하면됩니다







    반응형
Designed by Tistory.