관리 메뉴

nkdk의 세상

자바 Language 27일째 사번 및 사번이름 jtABLE 본문

My Programing/JAVA

자바 Language 27일째 사번 및 사번이름 jtABLE

nkdk 2008. 3. 8. 01:20
package db;

import java.sql.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.*;

import pac.DBConnectionMgr;

public class JTableMon2 extends JFrame{
Object data[][]=new String[0][4];
Object data2[][]=new String[0][4];
DBConnectionMgr pool;
Connection conn;
Statement stmt;
ResultSet rs;
DefaultTableModel mod, mod2; // 테이블 데이타 모델을 클래스를 선언한다.
JTable tab, tab2;
ArrayList list = new ArrayList();

String title[]={"사번", "이름", "부서명", "직급"};
String title2[]={"사원이름", "고객이름", "고객전화", "고객주민"};

public JTableMon2() {
accDb();
display();
accSql();
tab.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ea) {
// 객체 얻기
JTable tab=(JTable)ea.getComponent(); //<- 받아온 객체를 테이블에 받아옴
if(tab.getSelectedColumn()==0)
accDsql(1);
if(tab.getSelectedColumn()==2)
accDsql(2);
if(tab.getSelectedColumn()==3)
accDsql(3);
}
});
}

public void accSql() {
try {
stmt=conn.createStatement();
// mod.setnumrow(0) -> 초기화
rs=stmt.executeQuery("select sawon_no, sawon_name, nvl(sawon_jik, '임시') sawon_jik, nvl(buser_name,'프리') buser_name from sawon, buser where buser_num = buser_no(+)");
while(rs.next()) {
String s_no=rs.getString("sawon_no");
String s_name=rs.getString("sawon_name");
String s_jik=rs.getString("sawon_jik");
String b_name=rs.getString("buser_name");
String imsi[]={s_no, s_name, b_name, s_jik};
mod.addRow(imsi);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "SQL오류1:"+ex);
}finally{
try{
rs.close(); stmt.close();
}catch (Exception e) {
JOptionPane.showMessageDialog(this, "SQL닫음오류:"+e);
}
}
}

public void accDsql(int l) {
mod2.setRowCount(0);
String sql = "";
String im1 = "아라리";
try {
stmt=conn.createStatement();
if (l==1) {
sql = "select sawon_name, gogek_name, gogek_tel, gogek_jumin from sawon, gogek where sawon_no = gogek_damsano " +
"and gogek_damsano = "+ mod.getValueAt(tab.getSelectedRow(), tab.getSelectedColumn());
}
else if (l==2) {
sql = "select sawon_name, gogek_name, gogek_tel, gogek_jumin from sawon, gogek, buser where buser_no(+) = buser_num" +
" and sawon_no = gogek_damsano and nvl(buser_name,'프리') = '"+ mod.getValueAt(tab.getSelectedRow(), tab.getSelectedColumn())+"'";
} else if (l==3) {
sql = "select sawon_name, gogek_name, gogek_tel, gogek_jumin from sawon, gogek where " +
" sawon_no = gogek_damsano and nvl(sawon_jik,'임시') = '"+ mod.getValueAt(tab.getSelectedRow(), tab.getSelectedColumn())+"'";
} else { return; }
rs=stmt.executeQuery(sql);
while(rs.next()) {
String s_no=rs.getString("sawon_name");
String s_name=rs.getString("gogek_name");
String s_ibsa=rs.getString("gogek_tel");
String s_sex=rs.getString("gogek_jumin");
String imsi[]={"", s_name, s_ibsa, s_sex};
String il[]={s_no, s_name, s_ibsa, s_sex};
if (!im1.equals(s_no)) {
mod2.addRow(il);
im1=s_no;
} else mod2.addRow(imsi);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "SQL오류1:"+ex);
}finally{
try{
rs.close(); stmt.close();
}catch (Exception e) {
JOptionPane.showMessageDialog(this, "SQL닫음오류:"+e);
}
}
}

public void display() {
mod=new DefaultTableModel(data,title);
mod2=new DefaultTableModel(data2,title2);
tab=new JTable(mod);
tab2=new JTable(mod2);
tab.getTableHeader().setReorderingAllowed(false); // 테이블 고정 시키기
tab2.getTableHeader().setReorderingAllowed(false); // 테이블 이동 안 시키기, 타이틀 이동 안 시키기
JLabel jl=new JLabel("부서정보");
JPanel pn1=new JPanel();
pn1.setLayout(new GridLayout(2,1));
JPanel pn2=new JPanel(new BorderLayout());

JPanel pn3=new JPanel();
pn3.setLayout(new BorderLayout());
pn2.add("North", jl);
pn2.add("Center", new JScrollPane(tab));

jl=new JLabel("직원정보");
pn3.add("North", jl);
pn3.add("Center", new JScrollPane(tab2));

pn1.add(pn2);
pn1.add(pn3);

this.getContentPane().add(pn1, "Center");

this.setBounds(200, 200, 400, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void accDb() {
try {
pool = DBConnectionMgr.getInstance();
} catch (Exception e) {
System.out.println("커넥션 오류: " + e);
}
try {
conn = pool.getConnection(); // 이렇게 하면 연결 까지 완료 됨.
} catch (Exception e) {
System.out.println("객체 생성 오류: " + e);
}
}

public static void main(String[] args) {
new JTableMon2();
}
}

이 정도가 되겠네요. 오쯔까레 사마데시따~ :)