관리 메뉴

moozi

10/07 haksa 본문

TIS_2019/응용sw2019_2기

10/07 haksa

moozi 2019. 10. 7. 15:25

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
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.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Haksa extends JFrame {
JTextArea taList=null;
JTextField tfId=null;
JTextField tfName=null;
JTextField tfDept=null;

DefaultTableModel model=null;
JTable table=null;
Connection conn=null;
public Haksa() {

try {
//DB연결
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "ora_user", "hong");
//System.out.println("연결완료");
}catch(Exception e) {
e.printStackTrace();
}

this.setTitle("학사관리");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


this.addWindowListener(new WindowListener() {

@Override
public void windowActivated(WindowEvent e) {}

@Override
public void windowClosed(WindowEvent e) {}

@Override
public void windowClosing(WindowEvent e) {
try {
if(conn!=null) {
conn.close();
}
}catch(Exception e1) {
e1.printStackTrace();
}
}

@Override
public void windowDeactivated(WindowEvent e) {}

@Override
public void windowDeiconified(WindowEvent e) {}

@Override
public void windowIconified(WindowEvent e) {}

@Override
public void windowOpened(WindowEvent e) {}
});


Container c=this.getContentPane();
c.setLayout(new FlowLayout());

c.add(new JLabel("학번"));
    tfId=new JTextField(14);
    c.add(tfId);
    
    JButton btnSearch=new JButton("검색");
    btnSearch.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

Statement stmt = null;
try {
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from student2 where id='"+tfId.getText()+"'");

// taList.setText("");//초기화
//
// taList.append("=================================================\n");
// taList.append("학번 이름 학과 주소\n");
// taList.append("=================================================\n");
//
// while(rs.next()) {
// taList.append(rs.getString("id")+"\t");
//   taList.append(rs.getString("name")+"\t");
//   taList.append(rs.getString("dept")+"\n");
//  
//   //tfId.setText(rs.getString("id"));
//   tfName.setText(rs.getString("name"));
//   tfDept.setText(rs.getString("dept"));
// }

model.setNumRows(0);

while(rs.next()){
String[] row=new String[3];
row[0]=rs.getString("id");
row[1]=rs.getString("name");
row[2]=rs.getString("dept");

model.addRow(row);

tfName.setText(rs.getString("name"));
  tfDept.setText(rs.getString("dept"));
}


rs.close();

}catch (Exception e1) {
 e1.printStackTrace();
}

}});
    c.add(btnSearch);
    
    c.add(new JLabel("이름"));
tfName=new JTextField(20);
c.add(tfName);

c.add(new JLabel("학과"));
tfDept = new JTextField(20);
c.add(tfDept);

c.add(new JLabel("주소"));
JTextField tfAddress = new JTextField(20);
c.add(tfAddress);

// taList=new JTextArea(15,25);
// JScrollPane sp=new JScrollPane(taList);
// c.add(sp);


String colName[]={"학번","이름","학과"};//표에 출력할 컬럼명
model=new DefaultTableModel(colName,0);//표의 데이터
table = new JTable(model);//테이블에 모델(데이터) 바인딩
//테이블사이즈
table.setPreferredScrollableViewportSize(new Dimension(250,270));
c.add(new JScrollPane(table));

table.addMouseListener(new MouseListener() {

@Override
public void mouseClicked(MouseEvent e) {
table=(JTable)e.getComponent();
model=(DefaultTableModel)table.getModel();
String id=(String)model.getValueAt(table.getSelectedRow(), 0);//id
String name=(String)model.getValueAt(table.getSelectedRow(), 1);//name
String dept=(String)model.getValueAt(table.getSelectedRow(), 2);//dept

tfId.setText(id);
tfName.setText(name);
tfDept.setText(dept);
}

@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
});




JButton btnInsert=new JButton("등록");
btnInsert.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
Statement stmt = null;
try {
stmt=conn.createStatement();

stmt.executeUpdate("insert into student2(id,name,dept) values('"+tfId.getText()+"','"+tfName.getText()+"','"+tfDept.getText()+"')");

JOptionPane.showMessageDialog(null, "등록되었습니다");


}catch (Exception e1) {
 e1.printStackTrace();
}

}});
c.add(btnInsert);

JButton btnList=new JButton("목록");
btnList.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Statement stmt = null;
try {

stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select * from student2");



// taList.setText("");//초기화

// taList.append("=================================================\n");
// taList.append("학번 이름 학과 주소\n");
// taList.append("=================================================\n");
//
// while(rs.next()) {
// taList.append(rs.getString("id")+"\t");
//   taList.append(rs.getString("name")+"\t");
//   taList.append(rs.getString("dept")+"\n");
// }

//목록초기화
model.setNumRows(0);

while(rs.next()) {
String[] row=new String[3];
row[0]=rs.getString("id");
row[1]=rs.getString("name");
row[2]=rs.getString("dept");
model.addRow(row);
}


rs.close();

}catch (Exception e1) {
 e1.printStackTrace();
}

}});
c.add(btnList);

JButton btnUpdate=new JButton("수정");
btnUpdate.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Statement stmt = null;
try {

stmt=conn.createStatement();

stmt.executeUpdate("update student2 set name='"+tfName.getText()+"',dept='"+tfDept.getText()+"' where id='"+tfId.getText()+"'");
JOptionPane.showMessageDialog(null, "수정되었습니다");

}catch (Exception e1) {
 e1.printStackTrace();
}

}});
c.add(btnUpdate);
JButton btnDelete=new JButton("삭제");
btnDelete.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

Statement stmt = null;
ResultSet rs = null;

try {

stmt = conn.createStatement();
stmt.executeUpdate("delete from student2 where id = '"+tfId.getText()+"'");
JOptionPane.showMessageDialog(null, "삭제되었습니다");


// tfId.setText("");
// tfName.setText("");
// tfDept.setText("");
//
// taList.setText("");
// taList.append("=================================================\n");
// taList.append("학번 이름 학과 주소\n");
// taList.append("=================================================\n");

//목록초기화
model.setNumRows(0);

tfId.setText("");
tfName.setText("");
tfDept.setText("");
}
catch(Exception ex) {
ex.printStackTrace();

}
}});


c.add(btnDelete);


this.setSize(300,500);
this.setVisible(true);
}
public static void main(String[] args) {
new Haksa();

}

}

'TIS_2019 > 응용sw2019_2기' 카테고리의 다른 글

자바연습문제12  (0) 2019.10.07
10/7 BookRent  (0) 2019.10.07
jdbc01  (0) 2019.10.07
오라클연습문제06  (0) 2019.10.04
오라클연습문제05  (0) 2019.10.01
Comments