package kr.co.moak;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 MainSecond extends JFrame implements ActionListener{
JTextField txtName1, txtName2, txtEmail;
JTextArea txtMemo;
public MainSecond(){
this.setTitle("주소관리");
this.setSize(800, 600);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 이름(성)
JLabel lblName1 = new JLabel("성(Last Name) :");
lblName1.setBounds(10, 10, 150, 25);
this.add(lblName1);
txtName1 = new JTextField(20);
txtName1.setBounds(170, 10, 150, 25);
this.add(txtName1);
// 이름(이름)
JLabel lblName2 = new JLabel("이름(First Name) :");
lblName2.setBounds(10, 40, 150, 25);
this.add(lblName2);
txtName2 = new JTextField(20);
txtName2.setBounds(170, 40, 150, 25);
this.add(txtName2);
// 이메일
JLabel lblEmail = new JLabel("이메일(Email) :");
lblEmail.setBounds(10, 70, 150, 25);
this.add(lblEmail);
txtEmail = new JTextField(50);
txtEmail.setBounds(170, 70, 150, 25);
this.add(txtEmail);
JButton btnInsert = new JButton("삽입");
btnInsert.setBounds(10, 130, 100, 25);
this.add(btnInsert);
JButton btnSearch = new JButton("검색");
btnSearch.setBounds(120, 130, 100, 25);
this.add(btnSearch);
JButton btnSearchAll = new JButton("전체목록");
btnSearchAll.setBounds(230, 130, 100, 25);
this.add(btnSearchAll);
btnInsert.addActionListener(this); // 회원가입버튼 리스너 등록
btnSearch.addActionListener(this); // 검색버튼 리스너 등록
btnSearchAll.addActionListener(this); // 검색버튼 리스너 등록
// 기타
JLabel lblMemo = new JLabel("기타메모: ");
lblMemo.setBounds(10, 170, 150, 25);
this.add(lblMemo);
txtMemo = new JTextArea(5, 20); // 행 : 열
JScrollPane spMemo = new JScrollPane(txtMemo);
spMemo.setBounds(10, 200, 600, 200);
this.add(spMemo);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainSecond();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String last_name = "hong";
String email = "";
String buf = "";
switch (e.getActionCommand()) {
case "삽입":
last_name = txtName1.getText().toString();
email = txtEmail.getText().toString();
getConn();
insert(last_name, email);
break;
case "검색":
last_name = txtName1.getText().toString();
getConn();
email = getEmail(last_name);
buf = "이름 : " + last_name;
buf += "\n\n이메일 : " + email;
txtMemo.setText(buf);
break;
case "전체목록":
break;
}
}
String DBFileName = "d:\\address.db";
public Connection getConn() {
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
//SQLiteConfig config = new SQLiteConfig();
conn = DriverManager.getConnection("jdbc:sqlite:" + DBFileName);//, config.toProperties())
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public boolean insert(String name, String email) {
boolean ok = false;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConn();
String sql = "INSERT INTO taddr(last_name, email) VALUES(?, ?);";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, email);
int r = ps.executeUpdate();
conn.close();
if(r > 0) {
System.out.println("저장 성공");
}
else {
System.out.println("저장 실패");
}
} catch(Exception e) {
e.printStackTrace();
}
return ok;
}
public String getEmail(String name) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String email = "";
try {
conn = getConn();
String sql = "SELECT * FROM taddr where last_name=?;";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
if(rs.next()) {
email = rs.getString("email");
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return email;
}
}