studentAll.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="studentAll.do"> 전체 학생 조회</a>
</body>
</html>
------------------------------------------------------------------------------------------
FronCon1.java
package com.test.ex;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FronCon1
*/
@WebServlet("*.do")
public class FronCon1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FronCon1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
try {
actionDo(request,response);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
try {
actionDo(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,SQLException {
System.out.println("actionDo");
String uri = request.getRequestURI();
String conPath = request.getContextPath();
String command=uri.substring(conPath.length());
if(command.equals("/studentAll.do")){
response.setContentType("text/html; charset=EUC-KR");
PrintWriter writer = response.getWriter();
writer.println("<html><head></head><body>");
Service svc = new StudentAllService();
ArrayList<StudentDTO> sdtos = svc.execute(request, response);
for (int i=0; i < sdtos.size(); i++){
StudentDTO sdto = sdtos.get(i);
String no = sdto.getHakbun();
String pw = sdto.getPw();
String name = sdto.getName();
String hp = sdto.getHp();
writer.println(no + ","+pw+","+name+","+hp+"<hr/>");
}
writer.println("</body></html>");
}
}
}
--------------------------------------------------------------------------------------------------
Service.java
package com.test.ex;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Service {
public ArrayList <StudentDTO> execute(HttpServletRequest request, HttpServletResponse response)throws SQLException;
}
StudentAllService.java
package com.test.ex;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentAllService implements Service{
public StudentAllService(){
}
public ArrayList <StudentDTO> execute(HttpServletRequest request, HttpServletResponse response) throws SQLException{
StudentDAO sdao = new StudentDAO();
return sdao.select();
}
}
---------------------------------------------------------------------------------------------------------------------
StudentDAO.java
package jsp_Ex03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.*;
public class StudentDAO {
Connection dbconn;
PreparedStatement ps;
ResultSet rs;
String url ="jdbc:oracle:thin:@localhost:1521:XE";
String uid ="scott", pwd = "1234";
public StudentDAO(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
dbconn = DriverManager.getConnection(url,uid,pwd);
System.out.println("데이터베이스 연결!!");
} catch (Exception e) {
e.printStackTrace();
}
}// 생성자
public int insertStudent(StudentDTO sdto) throws SQLException{
String hakbun = sdto.getHakbun();
String pw = sdto.getPw();
String name = sdto.getName();
String hp = sdto.getHp();
int n = this.insertStudent(hakbun, pw, name, hp);
return n;
}
public int insertStudent(String hakbun, String pw, String name, String hp)
throws SQLException{
try{
String sql="insert into student values(?,?,?,?)";
ps=dbconn.prepareStatement(sql);
ps.setString(1, hakbun);
ps.setString(2, pw);
ps.setString(3, name);
ps.setString(4, hp);
int n = ps.executeUpdate();
return n;
}finally{
if(ps !=null) ps.close();
if(dbconn !=null) dbconn.close();
}
}
public ArrayList<StudentDTO> select() throws SQLException{
ArrayList <StudentDTO> sdtos = new ArrayList<StudentDTO>();
try{
String sql = "select * from student";
ps = dbconn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
StudentDTO sdto = new StudentDTO();
sdto.setHakbun(rs.getString("no"));
sdto.setPw(rs.getString("pw"));
sdto.setName(rs.getString("name"));
sdto.setHp(rs.getString("hp"));
sdtos.add(sdto);
}
}finally{
if(rs !=null) rs.close();
if(ps !=null) ps.close();
if(dbconn !=null) dbconn.close();
}
return sdtos;
}
public StudentDTO[] findSt(String name) throws SQLException{
try{
String sql="select * from student where name=?";
ps=dbconn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
StudentDTO stArray[] = createArray(rs);
return stArray;
}finally{
if(rs !=null) rs.close();
if(ps!=null) ps.close();
if(dbconn !=null) dbconn.close();
}
}
public StudentDTO[] createArray(ResultSet rs) throws SQLException{
Vector<StudentDTO> v = new Vector<StudentDTO>();
while(rs.next()){
String hakbun=rs.getString(1);
String pw = rs.getString(2);
String name = rs.getString(3);
String hp = rs.getString(4);
StudentDTO rowSt = new StudentDTO(hakbun, pw, name, hp);
v.add(rowSt);
}//while End
int vSize = v.size();
StudentDTO stArray[] = new StudentDTO[vSize];
v.copyInto(stArray); //벡터에 저장된 데이터를 stArray배열 객체에 복사
return stArray;
}
public int delStudent(String name) throws SQLException{
try{
String sql="delete from student where name=?";
ps=dbconn.prepareStatement(sql);
ps.setString(1, name);
int n = ps.executeUpdate();
return n;
}finally{
if(ps !=null) ps.close();
if(dbconn !=null) dbconn.close();
}
}
}
'기존카테고리 > JSP' 카테고리의 다른 글
MVC 패턴 (0) | 2017.06.21 |
---|---|
리다이렉트 방식과 요청디스패치 방식의 차이 (0) | 2017.06.21 |
FrontController 패턴 (0) | 2017.06.15 |
공지사항 - 파일 쓰기, 읽기 (0) | 2017.06.13 |
JSTL (0) | 2017.06.13 |