상세 컨텐츠

본문 제목

[jsp] 자바빈 /DTO/ DB연동_ 테스트

JSP_

by 쫑메이 2020. 8. 10. 00:46

본문

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 


[jsp]자바빈_ BeanForm


 

 

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="학생 등록">

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

[jsp]자바빈_ BeanPro


 

 

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);

%>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


[jsp]자바빈_ BeanDAO2


 

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();

}

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


[jsp] 자바빈_ BeanTest2


 

 

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;

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

ㅣ읽느라 수고 많으셨어요~ㅣ

 

 

 

 

 

 

 

 

 


 


 

 

 

 

 

 

 

 


부족한 글을 읽어주셔서 감사드립니다

아직 부족한 게 많으니

틀린 곳이 있다면

조언의 말씀 꼭 부탁드립니다!!!!

 

 

 

 

 

 


 

 

반응형

관련글 더보기