자바 스윙GUI, jdbc를 이용한 프로젝트...로그인 버튼 클릭 시 텍스트필드의 내용 전송하는 부분이 막힙니다
조회수 866회
db 연동은 잘 되는데 이래저래 찾아봐도 제가 하고싶은 부분이 막혀서 진행이 안되더라구요 ㅠㅠ 로그인 버튼 누르면 id입력창에 입력한 내용이 db로 전송되고, Joptionpane massage를 사용해서 id를 불러와 id+님 환영합니다! 를 띄워주고싶은데... sql전송하는 부분부터 막혀버리니 너무 머리가 아프네요...도움 주실 분 있을까요?
public class LoginForm extends JFrame{ FormDTO dto = new FormDTO(); //dto 객체 생성
public LoginForm() {
super("Household_login"); //타이틀
setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(null);
JLabel lb_text1 = new JLabel("Login Page");
JLabel lb_id = new JLabel("이름");
JTextField tf_id = new JTextField();
JButton btn_login = new JButton("로그인");
lb_text1.setBounds(160, 30, 100, 30);
lb_id.setBounds(100, 100, 30, 30);
tf_id.setBounds(135, 100, 150, 30);
btn_login.setBounds(220, 180, 120, 50);
setSize(400, 300); //창 크기 설정
p1.add(lb_text1);
p1.add(lb_id);
p1.add(tf_id);
p1.add(btn_login);
add(p1);
Dimension frameSize = getSize();
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
/* 이벤트 코드 */
btn_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object obj = e.getSource();
dto.setName(tf_id.getName());
try {
FormDAO.create(dto); //dto를 dao에 넘겨준다
}catch(Exception e1) {
e1.printStackTrace();
}
new HomeForm();
setVisible(false); //창 안보이게 하기
}
});
}
}
dao
public class FormDAO {
FormDAO(){
}
public static boolean create(FormDTO dto) throws Exception{
// TODO Auto-generated method stub
boolean flag = false;
Connection conn = null;
Statement stmt = null;
String name = dto.getName();
String sql = "INSERT INTO member(name) VALUES";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/household", "root","1234");
System.out.println("DB 연결 완료");
stmt = (Statement) conn.createStatement();
stmt.executeUpdate(sql);
flag = true;
/*
ResultSet srs = stmt.executeQuery("select * from student");
printData(srs, "name", "id", "dept");
srs = stmt.executeQuery("select name, id, dept from student where name='이기자'");
printData(srs, "name", "id", "dept");
*/
} catch (ClassNotFoundException e) {
System.out.println("JDBC 드라이버 로드 에러");
flag = false;
} catch (SQLException e) {
System.out.println("SQL 실행 에러");
flag = false;
}
return flag;
}
}
DTO
public class FormDTO {
private String name;
private String contents;
private String date;
private String group;
private int income;
private int expense;
//이클립스팁 : Getter/Setter 만들기
//우클릭 -> source->Generate Getters And Setters-> [Select AlL] -> [OK]
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getIncome() {
return income;
}
public void setIncome(int income) {
this.income = income;
}
public int getExpense() {
return expense;
}
public void setExpense(int expense) {
this.expense = expense;
}
//DTO 객체 확인
//이클립스팁 : toString() 자동생성: 우클릭 -> source -> Generate toString->[OK]
@Override
public String toString() {
return "FormDTO [name=" + name + ", contents=" + contents + ", date=" + date + ", group=" + group + ", income="
+ income + ", expense=" + expense + "]";
}
}
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력