관리 메뉴

nkdk의 세상

자바 Language 26일째 테이블에 마우스를 클릭하면 값 받아서 아 본문

My Programing/JAVA

자바 Language 26일째 테이블에 마우스를 클릭하면 값 받아서 아

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

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

import pac.DBConnectionMgr;

public class JTableMon1 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;

String title[]={"부서번호", "부서명", "부서전화", "위치"};
String title2[]={"사번", "이름", "입사일", "성별"};

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

}
});
}

public void accSql() {

try {
stmt=conn.createStatement();
// mod.setnumrow(0) -> 초기화
rs=stmt.executeQuery("select * from buser");
while(rs.next()) {
String b_no=rs.getString("buser_no");
String b_name=rs.getString("buser_name");
String b_tel=rs.getString("buser_tel");
String b_loc=rs.getString("buser_loc");
String imsi[]={b_no, b_name, b_tel, b_loc};
mod.addRow(imsi);
}
String im[]={"00","임시직","00-000-0000","기타"};
mod.addRow(im);
} 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() {
mod2.setRowCount(0);
try {
stmt=conn.createStatement();
// mod.setnumrow(0) -> 초기화
rs=stmt.executeQuery("select sawon_no, sawon_name, to_char(sawon_ibsail,'YYYY-MM-DD') sawon_ibsail, sawon_sex " +
"from sawon, buser where buser_no(+) = buser_num and " +
"nvl(buser_name,'임시직') = '"+ mod.getValueAt(tab.getSelectedRow(), tab.getSelectedColumn())+"'");
while(rs.next()) {
String s_no=rs.getString("sawon_no");
String s_name=rs.getString("sawon_name");
String s_ibsa=rs.getString("sawon_ibsail");
String s_sex=rs.getString("sawon_sex");
String imsi[]={s_no, s_name, s_ibsa, s_sex};
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);
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, 400);
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 JTableMon1();
}
}