p.368
p.370
pstmt=conn.prepareStatement(sql); 이부분은 데이터의 종류의 상관없이 동일하게 써준다 (?)
p.378
p.382 예제문제
1. mysql연동 테이블 생성하기
2. form 만들기
3. insert 파일 만들기
4.select 파일 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String url="jdbc:mysql://localhost:3306/korea";
String user="root";
String password="autoset";
PreparedStatement pstmt =null;
Connection conn =null;
ResultSet rs = null;
final String sql ="select idx, id, pwd from T0805 ";
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
pstmt=conn.prepareStatement(sql);
rs= pstmt.executeQuery();
%>
<div align="center">
<br>
<h2> 목 록 보 기 <a href=Form.jsp>(등록)</a></h2>
<table border=1 width=250>
<tr><td>번호</td><td>아이디</td><td>암호</td></tr>
<%
while(rs.next()){
%>
<tr><td> <%=rs.getString("idx")%></td>
<td> <%=rs.getString("id")%></td>
<td><a href=delete.jsp?id=<%=rs.getString("id")%>> <%=rs.getString("pwd")%></a></td></tr>
<%
}
%>
</table>
</div>
<%
}catch(Exception e){
out.print("접속 실패");
// e.printStackTrace();
}finally{
if( pstmt!= null){
pstmt.close();
}
if(conn != null){
conn.close();
}
}
%>
5.delete 파일 만들기
6. 목록보기에서 id로 검색창 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String ch1= request.getParameter("ch1");
String ch2=request.getParameter("ch2");
String url="jdbc:mysql://localhost:3306/korea";
String user="root";
String password="autoset";
PreparedStatement pstmt =null;
Connection conn =null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
if(ch1 ==null || ch1==""||ch2=="" ){
final String sql ="select idx, id, pwd from T0805 ";
pstmt=conn.prepareStatement(sql);
}else{
final String sql ="select idx, id, pwd from T0805 where id like ? ";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"%"+ch2+"%");
}
rs= pstmt.executeQuery();
%>
<div align="center">
<br>
<h2> 목 록 보 기 <a href=Form.jsp>(등록)</a></h2>
<table border=1 width=350>
<tr><td>번호</td><td>아이디</td><td>암호</td></tr>
<%
while(rs.next()){
%>
<tr><td> <%=rs.getString("idx")%></td>
<td> <%=rs.getString("id")%></td>
<td><a href=delete.jsp?id=<%=rs.getString("id")%>> <%=rs.getString("pwd")%></a></td></tr>
<%
}
%>
</table>
<form action=select.jsp>
<select name=ch1>
<option value=id>아이디</option>
</select>
<input type= text name=ch2>
<input type=submit value=검색하기>
</form>
</div>
<%
}catch(Exception e){
out.print("접속 실패");
// e.printStackTrace();
}finally{
if( pstmt!= null){
pstmt.close();
}
if(conn != null){
conn.close();
}
}
%>
Vo 클래스 만들기
<%@ page import="ch10.bean.MemberBean" %> 로 임포트 시켜줘야
new로 객체생성해서 쓸 수 있다
<jsp:useBean> 으로 클래스 불러오고
<jsp:setProperty> 로 vo 의 모든값을 받아옴
위와 같은 방법으로 select.jsp 를 bean 으로 처리하기
public List<MemberVo> selectAll(String ch1, String ch2) {
List<MemberVo> li = new ArrayList<MemberVo>();
try {
conn = DBConn.getConnection();
if ( ch1 == null || ch1 =="" || ch2 == ""){
String sql ="select idx, id, pwd from T0805 ";
pstmt=conn.prepareStatement(sql);
} else {
String sql ="select idx, id, pwd from T0805 where id like ? ";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%"+ch2+"%");
}
rs= pstmt.executeQuery();
while(rs.next()) {
MemberVo m=new MemberVo();
m.setIdx(rs.getInt("idx"));
m.setId(rs.getString("id"));
m.setPwd(rs.getString("pwd"));
li.add(m);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if( rs!= null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( pstmt!= null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return li;
}
멤버빈 클래스의 selectAll 부분 추가해주기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="ch10.bean.*" %>
<%@ page import="java.util.*" %>
<%
String ch1= request.getParameter("ch1");
String ch2=request.getParameter("ch2");
Member service = new MemberBean();
List<MemberVo> li=service.selectAll(ch1, ch2);
%>
<div align="center">
<br>
<h2> 목 록 보 기 <a href=Form.jsp>(등록)</a></h2>
<table border=1 width=350>
<tr><td>번호</td><td>아이디</td><td>암호</td></tr>
<%
for(int i =0; i<li.size(); i++){
MemberVo m= li.get(i);
%>
<tr><td> <%=m.getIdx() %></td>
<td> <%=m.getId() %></td>
<td><a href=delete.jsp?id=<%=m.getId() %>> <%=m.getPwd() %></a></td></tr>
<%
}
%>
</table>
<form action=select.jsp>
<select name=ch1>
<option value=id>아이디</option>
</select>
<input type= text name=ch2>
<input type=submit value=검색하기>
</form>
</div>
select.jsp 내용 수정해주기
delete.jsp 도 해보기
package ch10.bean;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MemberBean implements Member{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public List<MemberVo> selectAll(MemberVo vo) {
String ch1 = vo.getCh1();
String ch2= vo.getCh2();
List<MemberVo> li = new ArrayList<MemberVo>();
try {
conn = DBConn.getConnection();
if ( ch1 == null || ch1 =="" || ch2 == ""){
String sql ="select idx, id, pwd from T0805 ";
pstmt=conn.prepareStatement(sql);
} else {
String sql ="select idx, id, pwd from T0805 where id like ? ";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%"+ch2+"%");
}
rs= pstmt.executeQuery();
while(rs.next()) {
MemberVo m=new MemberVo();
m.setIdx(rs.getInt("idx"));
m.setId(rs.getString("id"));
m.setPwd(rs.getString("pwd"));
li.add(m);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if( rs!= null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( pstmt!= null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return li;
}
@Override
public void insert(MemberVo vo) {
String sql ="insert into T0805 (id, pwd) values(?,?)";
try{
conn= DBConn.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, vo.getId());
pstmt.setString(2, vo.getPwd());
int k= pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
if( pstmt!= null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void delete(MemberVo vo) {
String sql ="delete from T0805 where idx=?";
try{
conn = DBConn.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, vo.getIdx());
pstmt.executeUpdate();
}catch(Exception e){
// e.printStackTrace();
}finally{
if( pstmt!= null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
멤버빈 전체코드 (idx로 값 받아서 삭제하는걸로 수정해서 select.jsp 링크 건곳에도 idx로 수정해줘야함 )
'JAVA' 카테고리의 다른 글
[58일차] 정보처리산업기사 지역구의원투표프로그램 만들기 (0) | 2022.08.11 |
---|---|
[55일차] 자바로 mysql연동, 이미지 추가해서 자료실 만들기 (0) | 2022.08.09 |
[53일차] chap11. 데이터베이스와 jsp 연동 (0) | 2022.08.04 |
[53일차] JSP 교재 p.242 ~ (0) | 2022.08.04 |
[52일차] chap6. p187 switch문 ~ (0) | 2022.08.03 |