- 객체생성
java -> MemberBean member = new MemberBean();
jsp -> <jsp:useBean class="전체패키지명.MemberBean" id="member" />
- setter()
java -> member.setName("자바학생");
jsp -> <jsp.setProperty name="member" property="name" value="자바학생" />
- getter()
java -> member.getName();
jsp -> <jsp.getProperty name="member" property="name" />
public class MemberBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public static Connection getConnection() {
Connection conn = null;
try {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "이름";
String password = "비밀번호";
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("드라이버 로딩 실패");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("DB 접속 오류");
} catch(Exception e) {
e.printStackTrace();
System.out.println("알 수 없는 오류");
}
return conn;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.koreait.web.beans.MemberBean;
public class MemberDAO {
// db 접근에 관한 비즈니스 로직
Connection conn;
PreparedStatement pstm;
ResultSet rs;
public int getAge(String name) {
String sql = "SELECT AGE FROM MEMBER WHERE name = ?";
int age = 0;
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(sql);
pstm.setString(1, name);
rs = pstm.executeQuery();
if( rs.next() ) {
age = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("DB 접속 오류");
} catch(Exception e) {
e.printStackTrace();
System.out.println("알 수 없는 오류");
}
return age;
}
public ArrayList<MemberBean> selectAll() {
String sql = "SELECT * FROM MEMBER";
ArrayList<MemberBean> result = new ArrayList<>();
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while(rs.next()) {
MemberBean member = new MemberBean();
member.setName(rs.getString(1));
member.setAge(rs.getInt(2));
result.add(member);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("DB 접속 오류");
} catch(Exception e) {
e.printStackTrace();
System.out.println("알 수 없는 오류");
}
return result;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 자바빈즈 태그 -->
<!-- MemberBean 클래스로 객체 생성 -->
<jsp:useBean class="com.koreait.web.beans.MemberBean" id="member" />
<!-- setter() -->
<jsp:setProperty property="name" name="member" value="김자바"/>
<jsp:setProperty property="age" name="member" value="20"/>
<!-- getter() -->
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
</tr>
<tr>
<td><jsp:getProperty property="name" name="member"/></td>
<td><jsp:getProperty property="age" name="member"/></td>
</tr>
</table>
</body>
</html>
출처
https://media.giphy.com/media/dwmNhd5H7YAz6/giphy.gif
https://media.giphy.com/media/3o6Mb9EC7mNqXl9x7y/giphy.gif