Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 안드로이드 개발 2.0 강좌
- 스카이 안드로이드폰 시리우스 K양 동영상
- android
- 안드로이드폰
- 영어
- 안드로이드 배경화면
- SKY 시리우스
- 아이폰 바탕화면
- 아이폰 배경화면
- 안드로이드 바탕화면
- 스카이 안드로이드폰 시리우스
- objective-c
- 구글안드로이드
- 안드로이드
- 구글 안드로이드 개발
- 하루 한마디 영어
- 하루한마디영어
- 스마트폰 배경화면
- Form Stuff
- 안드로이드 개발
- 인기있는 블로그 만들기
- MapView
- 안드로이드 2.0 개발
- 안드로이드2.0
- 안드로이드 개발 강좌
- 구글 안드로이드
- 안드로이드2.0개발
- sky 시리우스폰
- 안드로이드 개발 2.0
- 안드로이드개발
Archives
- Today
- Total
moozi
10/05 haksa 본문
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.JTextField;
import javax.swing.table.DefaultTableModel;
public class Haksa extends JFrame {
public JTextField tfId;
public JTextField tfName;
public JTextField tfDepartment;
public JTextField tfAddress;
public JButton btnInsert;
public JButton btnSelect;
public JButton btnUpdate;
public JButton btnDelete;
public JButton btnSearch;
Connection conn;
DefaultTableModel model;
JTable table;
public Haksa() {
this.setTitle("학사관리프로그램");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//DB Connection
try {
//orable jdbc드라이버 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myoracle","ora_user","hong");// 연결
System.out.println("연결완료");
}catch(Exception e) {
e.printStackTrace();
}
//윈도우종료시 db connection close
this.addWindowListener(new WindowListener() {
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
try {
if(conn!=null) {
conn.close();//connection close
}
}catch(Exception e1) {
e1.printStackTrace();
}
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}});
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("학번"));
this.tfId=new JTextField(14);
c.add(tfId);
this.btnSearch=new JButton("검색");
c.add(btnSearch);
this.btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
//orable jdbc드라이버 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myoracle","ora_user","hong");// 연결
System.out.println("연결완료");
//목록초기화
model.setNumRows(0);//model에서 행의 수를 0로 설정
//Statement객체생성
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student2 where id='"+tfId.getText()+"'");
while(rs.next()) {
tfId.setText(rs.getString("id"));
tfName.setText(rs.getString("name"));
tfDepartment.setText(rs.getString("dept"));
String[] row=new String[3];//컬럼의 갯수가 3
row[0]=rs.getString("id");
row[1]=rs.getString("name");
row[2]=rs.getString("dept");
model.addRow(row);
}
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}});
c.add(new JLabel("이름"));
this.tfName=new JTextField(20);
c.add(tfName);
c.add(new JLabel("학과"));
this.tfDepartment=new JTextField(20);
c.add(tfDepartment);
c.add(new JLabel("주소"));
this.tfAddress=new JTextField(20);
c.add(tfAddress);
String colName[]={"학번","이름","학과"};
model=new DefaultTableModel(colName,0);
table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(250,120));
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
table=(JTable)e.getComponent();
model=(DefaultTableModel)table.getModel();
tfId.setText((String)model.getValueAt(table.getSelectedRow(), 0));
tfName.setText((String)model.getValueAt(table.getSelectedRow(), 1));
tfDepartment.setText((String)model.getValueAt(table.getSelectedRow(), 2));
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}});
c.add(new JScrollPane(table));
this.btnInsert=new JButton("등록");
btnInsert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("등록버튼클릭됨");
//등록처리. 오라클연동해서 처리.
try {
//Statement객체 생성
Statement stmt=conn.createStatement();
//insert
stmt.executeUpdate("insert into student2 values('"+tfId.getText()+"','"+tfName.getText()+"','"+tfDepartment.getText()+"')");
}catch(Exception e) {
e.printStackTrace();
}
//목록
list();
//등록완료메시지
JOptionPane.showMessageDialog(null, "등록되었습니다");
}});
c.add(btnInsert);
this.btnSelect=new JButton("목록");
this.btnSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//목록
list();
}});
c.add(btnSelect);
this.btnUpdate=new JButton("수정");
this.btnUpdate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
//Statement객체 생성
Statement stmt=conn.createStatement();
//update
stmt.executeUpdate("update student2 set name='"+tfName.getText()+"',dept='"+tfDepartment.getText()+"' where id='"+tfId.getText()+"'");
}catch(Exception e) {
e.printStackTrace();
}
//목록
list();
//수정완료메시지
JOptionPane.showMessageDialog(null, "수정되었습니다");
}});
c.add(btnUpdate);
this.btnDelete=new JButton("삭제");
this.btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//삭제여부Confirm
int result=JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?");
if(result==JOptionPane.NO_OPTION || result==JOptionPane.CANCEL_OPTION || result==JOptionPane.CLOSED_OPTION) {
return;//종료
}
try {
//Statement객체 생성
Statement stmt=conn.createStatement();
//delete
stmt.executeUpdate("delete from student2 where id='"+tfId.getText()+"'");
}catch(Exception e) {
e.printStackTrace();
}
//목록
list();
//입력항목초기화
tfId.setText("");
tfName.setText("");
tfDepartment.setText("");
//삭제완료메시지
JOptionPane.showMessageDialog(null, "삭제되었습니다");
}});
c.add(btnDelete);
this.setSize(300, 350);
this.setResizable(false);//사이즈변경안함
this.setVisible(true);
}
//목록
public void list() {
try {
//Statement객체 생성
Statement stmt=conn.createStatement();
//목록초기화
model.setNumRows(0);
ResultSet rs=stmt.executeQuery("select * from student2");
while(rs.next()) {
String[] row = new String[3]; //컬럼의 갯수가 3
row[0]=rs.getString("id");
row[1]=rs.getString("name");
row[2]=rs.getString("dept");
model.addRow(row);
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Haksa();
}
}
'TIS_2020 > 빅데이터2020_1기' 카테고리의 다른 글
10/5 Haksa.java , Student.java (0) | 2020.10.05 |
---|---|
10/5 BookRent (0) | 2020.10.05 |
10/05 oracle jdbc 기초 (0) | 2020.10.05 |
oracle jdbc select기초 (0) | 2020.09.29 |
oracle jdbc연결테스트 (0) | 2020.09.29 |
Comments