package ch22;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.sql.*;
public class Ch2205 extends JFrame
{
//재사용 가능하게 모듈처리.
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField txtID = null;
private JLabel jLabel1 = null;
private JTextField txtName = null;
private JTextField txtEmail = null;
private JTextField txtPwd = null;
private JLabel jLabel2 = null;
private JLabel jLabel21 = null;
private JButton btnAdd = null;
/**
* This is the default constructor
*/
public Ch2205()
{
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jLabel21 = new JLabel();
jLabel21.setBounds(new Rectangle(30, 110, 61, 21));
jLabel21.setText("비밀번호");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(30, 80, 41, 21));
jLabel2.setText("이메일");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(30, 50, 41, 21));
jLabel1.setText("이름");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(29, 19, 42, 22));
jLabel.setText("ID");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getTxtID(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getTxtName(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getTxtEmail(), null);
jContentPane.add(jLabel21, null);
jContentPane.add(getTxtPwd(), null);
jContentPane.add(getBtnAdd(), null);
}
return jContentPane;
}
/**
* This method initializes txtID
*
* @return javax.swing.JTextField
*/
private JTextField getTxtID()
{
if (txtID == null)
{
txtID = new JTextField();
txtID.setBounds(new Rectangle(80, 20, 161, 22));
}
return txtID;
}
/**
* This method initializes txtName
*
* @return javax.swing.JTextField
*/
private JTextField getTxtName()
{
if (txtName == null)
{
txtName = new JTextField();
txtName.setBounds(new Rectangle(80, 50, 161, 21));
}
return txtName;
}
/**
* This method initializes txtEmail
*
* @return javax.swing.JTextField
*/
private JTextField getTxtEmail()
{
if (txtEmail == null)
{
txtEmail = new JTextField();
txtEmail.setBounds(new Rectangle(80, 80, 161, 21));
}
return txtEmail;
}
/**
* This method initializes txtPwd
*
* @return javax.swing.JTextField
*/
private JTextField getTxtPwd()
{
if (txtPwd == null)
{
txtPwd = new JTextField();
txtPwd.setBounds(new Rectangle(100, 110, 141, 21));
}
return txtPwd;
}
/**
* This method initializes btnAdd
*
* @return javax.swing.JButton
*/
private JButton getBtnAdd()
{
if (btnAdd == null)
{
btnAdd = new JButton();
btnAdd.setBounds(new Rectangle(150, 140, 91, 21));
btnAdd.setText("추가");
btnAdd.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
// TODO Auto-generated Event stub actionPerformed()
//동작빈의 모델개체를 생성하고 멤버함수를 호출하되
//텍스트박스의 값을 문자열로 전달.
//MVC1 모델.
//장점 :
Adminpro adm = new Adminpro();
AdminBean admbean = new AdminBean();
String id=txtID.getText();
String name=txtName.getText();
String email=txtEmail.getText();
String pwd=txtPwd.getText();
admbean.setId(id);
admbean.setName(name);
admbean.setPwd(pwd);
admbean.setEmail(email);
int ab = adm.write(admbean);
if(ab>0)
{
txtID.setText("");
txtName.setText("");
txtEmail.setText("");
txtPwd.setText("");
}
JOptionPane.showConfirmDialog(null, "수행결과 : "+ab);
}
});
}
return btnAdd;
}
}
********** Adminpro.java ********
package ch22;
import java.sql.*;
import javax.swing.JOptionPane;
public class Adminpro
{
//동작 모듈 관련.. 2가지 개선상황.
//1.개선 : 매갠변수가 늘어날시 다 입력해줘야함. -> 데이타빈으로 교체.
//데이터빈 모델형태 -> getter, setter 사용.
//2개선 : sql이용시 conn부분 중복, 계승이용해서 풀어주면 됨.
//public int write(String id, String name, String email, String pwd)
public int write(AdminBean bean)
{
PreparedStatement pstmt = null;
Connection conn = null;
int rows=0;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection
(
"jdbc:oracle:thin:@localhost:1521:ORCL","jsp","jsp"
);
String sql = "insert into admin(id,name,pwd,email) values (?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, bean.getId());
pstmt.setString(2, bean.getName());
pstmt.setString(3, bean.getPwd());
pstmt.setString(4, bean.getEmail());
rows=pstmt.executeUpdate();
}
catch (ClassNotFoundException e1)
{
// TODO 자동 생성된 catch 블록
e1.printStackTrace();
}
catch (SQLException e1)
{
// TODO 자동 생성된 catch 블록
e1.printStackTrace();
}
finally
{
try
{
pstmt.close();
conn.close();
}
catch (SQLException e1)
{
// TODO 자동 생성된 catch 블록
e1.printStackTrace();
}
}
return rows;
}
}