관리 메뉴

moozi

haksa 03/26 본문

TIS_2019/응용sw2019_1기

haksa 03/26

moozi 2019. 3. 26. 16:16

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;


public class Haksa extends JFrame{

static Connection  conn;

static Statement stmt;

static ResultSet rs;

JButton listBtn=null;

JButton insertBtn=null;

JTextField idTxt=null;

JTextField nameTxt=null;

JTextField deptTxt=null;

JTextArea ta=null;

public Haksa() {

this.setTitle("학사관리");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c=this.getContentPane();

c.setLayout(new FlowLayout());

c.add(new JLabel("학번"));

idTxt=new JTextField(20);

c.add(idTxt);

c.add(new JLabel("이름"));

nameTxt=new JTextField(20);

c.add(nameTxt);

c.add(new JLabel("학과"));

deptTxt=new JTextField(20);

c.add(deptTxt);

c.add(new JLabel("주소"));

c.add(new JTextField(20));

ta=new JTextArea(7,25);

c.add(new JScrollPane(ta));

listBtn=new JButton("목록");

listBtn.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent arg0) {

try {

//oracle jdbc드라이버 로드

Class.forName("oracle.jdbc.driver.OracleDriver");

conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","ora_user","hong");// 연결

//statement객체 생성

stmt=conn.createStatement();

//쿼리실행

rs=stmt.executeQuery("select * from student order by id");

//목록초기화

ta.setText("");

//컬럼명출력

ta.append("학번"+"\t"+"이름"+"\t"+"학과"+"\n");

ta.append("======================================\n");

//fetch

while(rs.next()) {

// System.out.print(rs.getString("id")+"-");

// System.out.print(rs.getString("name")+"-");

// System.out.println(rs.getString("dept"));

ta.append(rs.getString("id")+"\t"+rs.getString("name")+"\t"+rs.getString("dept")+"\n");

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

if(rs!=null) { rs.close();}

if(conn!=null) {conn.close();}

} catch (SQLException e) {

e.printStackTrace();

}

}

}});

c.add(listBtn);

insertBtn=new JButton("등록");

insertBtn.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent arg0) {

try {

//oracle jdbc드라이버 로드

Class.forName("oracle.jdbc.driver.OracleDriver");

conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","ora_user","hong");// 연결

//statement객체 생성

stmt=conn.createStatement();

//insert

stmt.executeUpdate("insert into student values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+deptTxt.getText()+"')");

System.out.println("insert into student values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+deptTxt.getText()+"')");

//목록초기화

ta.setText("");

//컬럼명출력

ta.append("학번"+"\t"+"이름"+"\t"+"학과"+"\n");

ta.append("======================================\n");

//쿼리실행

rs=stmt.executeQuery("select * from student order by id");

//fetch

while(rs.next()) {

ta.append(rs.getString("id")+"\t"+rs.getString("name")+"\t"+rs.getString("dept")+"\n");

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

if(rs!=null) { rs.close();}

if(conn!=null) {conn.close();}

} catch (SQLException e) {

e.printStackTrace();

}

}

}});

c.add(insertBtn);

c.add(new JButton("수정"));

c.add(new JButton("삭제"));

this.setSize(300,350);

this.setVisible(true);

}

public static void main(String[] args) {

new Haksa();


}


}



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

3/27 haksa  (0) 2019.03.27
자바연습문제16  (0) 2019.03.26
haksa  (0) 2019.03.26
오라클연습문제07  (0) 2019.03.25
오라클연습문제06  (0) 2019.03.22
Comments