BeanForm
<h1>jsp6/beanForm2.jsp</h1>
<form action="BeanPro2.jsp"method="post">
번호<input type="text"name="num">
이름<input type="text"name="name">
<input type="submit"value="학생 등록">
BeanPro
<body>
<h1>jsp6/beanPro2.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
int num=Integer.parseInt(request.getParameter("num"));
// BeanTest2 bt2=new BeanTest2();
// bt2.setName(name);
// bt2.setNum(num);
//out.print("BeanTest2 잡아 빈에 저장된 name 값"+bt2.getName());
%>
<jsp:useBean id="bt2"class="bean.BeanTest2"/>
<%-- <jsp:setProperty property="num"nam="bt2"param="num"/> --%>
<%-- <jsp:setProperty property="name"nam="bt2"param="name"/> --%>
<jsp:setProperty property="*"name="bt2"/>
<%
BeanDAO2 dbdao2=new BeanDAO2();
dbdao2.insertStudent(bt2);
%>
BeanDAO2
package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BeanDAO2 {
public void insertStudent(BeanTest2 bt2) {
System.out.println("BeanDAO2 insertStudent()" + bt2.getName());
System.out.println("BeanDAO2 insertStudent()" + bt2.getNum());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
PreparedStatement pstmt = null;
String dbUrl = "jdbc:mysql://localhost:3306/jspdb5";
String dbUser = "root";
String dbPass = "1234";
con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
String sql = "insert into student(num, name) values(?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bt2.getNum());
pstmt.setString(1, bt2.getName());
pstmt.executeUpdate();
} catch (Exception e) {
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BeanTest2
package bean;
public class BeanTest2 {
private int num;
private String name;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
publicStringgetName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ㅣ읽느라 수고 많으셨어요~ㅣ
부족한 글을 읽어주셔서 감사드립니다
아직 부족한 게 많으니
틀린 곳이 있다면
조언의 말씀 꼭 부탁드립니다!!!!
[jsp] 자바빈_멤버 (insert/delete/login/update/list/main 만들기) (0) | 2020.08.31 |
---|---|
[JSP] 자바빈(JavaBean)?/ 자바빈 사용이유/ 자바DTO(Data Transfer Object) (0) | 2020.07.31 |
[JSP] DB랑 연동해서 jsp 게시판 만들기(이클립스) (0) | 2020.07.18 |
jsp_애플리케이션 application 내장객체, 세션 (0) | 2020.07.16 |
JSP에서 DB과 연동해서delete /update/ select/ insert 작업을 어떻게 하는 걸까? (0) | 2020.07.14 |