import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class MemberTest extends JFrame implements ActionListener, ListSelectionListener
{
JPanel leftPanel, rightPanel;
JPanel left1, left2, left3, left4, left5, left6, left7;
JLabel left1lb, left2lb,left3lb, left4lb, left5lb, left6lb, left7lb, rightlb;
JTextField left1tf, left2tf, left3tf, left4tf;
JRadioButton left1rb, left2rb;
ButtonGroup group;
JComboBox<String> leftcb;
String[] data = {"등산", "여행"};
JButton b1, b2, b3, b4;
JList<String> list;
Vector<String> listdata = new Vector<>();
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String id = "system";
String password = "nam";
Connection con;
PreparedStatement ps;
ResultSet rs;
public MemberTest()
{
leftPanel = new JPanel();
rightPanel = new JPanel();
left1 = new JPanel();
left2 = new JPanel();
left3 = new JPanel();
left4 = new JPanel();
left5 = new JPanel();
left6 = new JPanel();
left7 = new JPanel();
left1lb = new JLabel(" 회원가입 ");
left2lb = new JLabel(" 아 이 디 : ");
left3lb = new JLabel(" 비밀번호: ");
left4lb = new JLabel(" 전화번호: ");
left5lb = new JLabel(" 주소 : ");
left6lb = new JLabel(" 취미 : ");
left7lb = new JLabel(" 성별 : ");
rightlb = new JLabel(" 모든 회원 보기 ");
left1tf = new JTextField(8);
left2tf = new JTextField(8);
left3tf = new JTextField(8);
left4tf = new JTextField(8);
leftcb = new JComboBox<String>(data);
left1rb = new JRadioButton("남");
left2rb = new JRadioButton("여");
group = new ButtonGroup();
group.add(left1rb);
group.add(left2rb);
b1 = new JButton("입력");
b2 = new JButton("수정");
b3 = new JButton("삭제");
b4 = new JButton("검색");
list = new JList<String>(listdata);
left1.add(left2lb);
left1.add(left1tf);
left2.add(left3lb);
left2.add(left2tf);
left3.add(left4lb);
left3.add(left3tf);
left4.add(left5lb);
left4.add(left4tf);
left5.add(left6lb);
left5.add(leftcb);
left6.add(left7lb);
left6.add(left1rb);
left6.add(left2rb);
left7.add(b1);
left7.add(b2);
left7.add(b3);
left7.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
list.addListSelectionListener(this);
leftPanel.setLayout(new GridLayout(8,1));
leftPanel.add(left1lb);
leftPanel.add(left1);
leftPanel.add(left2);
leftPanel.add(left3);
leftPanel.add(left4);
leftPanel.add(left5);
leftPanel.add(left6);
leftPanel.add(left7);
rightPanel.setLayout(new BorderLayout());
rightPanel.add("North", rightlb);
rightPanel.add(list);
setLayout(new GridLayout(1,2));
this.add(leftPanel);
this.add(rightPanel);
connection();
getAllMember();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == b1)
this.insertMember();
else if(source == b2)
this.updataMember();
else if(source == b3)
this.deleteMember();
}
public void valueChanged(ListSelectionEvent le)
{
String data = list.getSelectedValue();
String split[] = data.split(" ");
left1tf.setText(split[0]);
left2tf.setText(split[1]);
left3tf.setText(split[2]);
left4tf.setText(split[3]);
leftcb.setSelectedItem(split[4]);
if(split[5].equals("m"))
left1rb.setSelected(true);
else if(split[5].equals("f"))
left2rb.setSelected(true);
}
public void connection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url, id, password);
}
catch(Exception e)
{
}
}
public void insertMember()
{
String sql = "INSERT into member values(?,?,?,?,?,?)";
try
{
ps = con.prepareStatement(sql);
ps.setString(1, left1tf.getText().trim());
ps.setInt(2, Integer.parseInt(left2tf.getText().trim()));
ps.setString(3, left3tf.getText().trim());
ps.setString(4, left4tf.getText().trim());
ps.setString(5, leftcb.getSelectedItem().toString());
if(left1rb.isSelected())
ps.setString(6, "m");
else if(left2rb.isSelected())
ps.setString(6, "f");
ps.executeUpdate();
ps.close();
}
catch(Exception e)
{
}
this.getAllMember();
}
public void updataMember()
{
String sql = "UPDATE member set phone=?, address=? where id=?";
try
{
ps = con.prepareStatement(sql);
ps.setString(1, left3tf.getText().trim());
ps.setString(2, left4tf.getText().trim());
ps.setString(3, left1tf.getText().trim());
ps.executeUpdate();
ps.close();
}
catch(Exception e)
{
}
this.getAllMember();
}
public void deleteMember()
{
String sql = "DELETE from member where id=?";
try
{
ps = con.prepareStatement(sql);
ps.setString(1, left1tf.getText());
ps.executeUpdate();
ps.close();
}
catch(Exception e)
{
}
this.getAllMember();
}
public void getAllMember()
{
String sql = "SELECT * from member";
listdata.removeAllElements();
try
{
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next())
{
String AllMember = "";
AllMember += rs.getString(1)+" ";
AllMember += rs.getInt(2)+" ";
AllMember += rs.getString(3)+" ";
AllMember += rs.getString(4)+" ";
AllMember += rs.getString(5)+" ";
AllMember += rs.getString(6)+"\n";
listdata.add(AllMember);
}
rs.close();
ps.close();
list.setListData(listdata);
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
new MemberTest();
}
}
댓글 없음:
댓글 쓰기