package pack;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
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.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DbTest10_ex1 extends JPanel implements ActionListener {
JLabel lbl1 = new JLabel("고객명 : ");
JLabel lbl2 = new JLabel("주민번호 : ");
JLabel lbl3 = new JLabel(" - ");
JLabel lbl4 = new JLabel("담당직원 정보 ");
JTextField _gogekName, _jumin1, _jumin2;
JButton btn;
JTextArea ta = new JTextArea(5, 40);
// DB
DBConnectionMgr pool;
Connection conn;
Statement stmt;
ResultSet rs;
public DbTest10_ex1() {
_gogekName = new JTextField("이나라", 6);
_jumin1 = new JTextField("850612", 6);
_jumin2 = new JTextField("1156789", 6);
btn = new JButton("확인");
JPanel pn1 = new JPanel();
pn1.add(lbl1);
pn1.add(_gogekName);
pn1.add(lbl2);
pn1.add(_jumin1);
pn1.add(lbl3);
pn1.add(_jumin2);
pn1.add(btn);
this.add(pn1);
btn.addActionListener(this);
JPanel pn2 = new JPanel();
pn2.add(lbl4);
this.add(pn2);
JPanel pn3 = new JPanel();
pn3.add(ta);
this.add(pn3);
}
@Override
public void actionPerformed(ActionEvent arg0) {
try {
//DB 셋팅
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
stmt = conn.createStatement();
//변수 셋팅
StringBuilder sql = new StringBuilder();
String gogekName = _gogekName.getText();
String jumin1 = _jumin1.getText();
String jumin2 = _jumin2.getText();
//sql셋팅
sql.append("select sawon_name, buser_name, buser_tel, sawon_jik, sawon_gen");
sql.append(" from sawon");
sql.append(" inner join buser on sawon.buser_num = buser.buser_no");
sql.append(" inner join gogek on sawon.sawon_no = gogek.gogek_damsano");
sql.append(" where gogek_name='" + gogekName + "'");
sql.append(" and gogek_jumin='" + jumin1 + "-" + jumin2 + "'");
rs = stmt.executeQuery(sql.toString());
// boolean check = stmt.execute(sql.toString());
rs = stmt.getResultSet();
// System.out.println(sql.toString());
//검증
if(gogekName.equals("") || jumin1.equals("") || jumin2.equals("")){
JOptionPane.showMessageDialog(this, "입력자료를 모두 채우세요", "경고", JOptionPane.INFORMATION_MESSAGE);
return;
}else if(!rs.next()){
JOptionPane.showMessageDialog(this, "입력자료 불일치!", "경고", JOptionPane.INFORMATION_MESSAGE);
return;
//출력
}else{
StringBuilder list = new StringBuilder();
list.append("직원명 : " + rs.getString("sawon_name") + "\n");
list.append("부서명 : " + rs.getString("buser_name") + "\n");
list.append("부서전화 : " + rs.getString("buser_tel") + "\n");
list.append("직급 : " + rs.getString("sawon_jik") + "\n");
list.append("성별 : " + rs.getString("sawon_gen"));
ta.append(list.toString());
}
} catch (Exception e) {
System.out.println("process err: " + e);
}finally{
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
} catch (Exception e2) {
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new DbTest10_ex1());
frame.setBounds(200, 200, 500, 210);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
'자바 DB 연동' 카테고리의 다른 글
simpleDateFormat : sql 날짜 형식을 자바에서 다루기 (0) | 2014.05.01 |
---|---|
자바에서 간단한 윈도우 프로그램 짜기 (0) | 2013.06.19 |
WindowListener 자바에서 윈도우창 만들기 (0) | 2013.06.19 |
CallableStatement - 프로시져 사용하기 (2) | 2013.06.15 |
OracleTypes.CURSOR 에러 해결하기 (0) | 2013.06.15 |