[JSP]오라클디비 서블릿으로 연결하기
일단 서블릿을 하나 만들어준 후에
프로젝트 마우스 오른쪽 빌드패스를 누르면 나옴
ojdbc버전 에 맞게 넣어줌
웹 디플로이 어셈블리에서 추가해줌
그다음에 sql디벨로퍼에 테이블 하나 만듬
그다음에 입력해줌
package book;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 SelectBookServlet
*/
@WebServlet("/Select.do")
public class SelectAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectAllServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//DB접속
//1.드라이버로딩
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.연결
Connection conn=null;
PreparedStatement pstmt=null;
String url="jdbc:oracle:thin:@localhost:1521:xe";
String sql="select *from booktbl";
ResultSet rs=null;
//웹브라우저 출력될 한글 타입 설정
response.setContentType("text/html; charset=utf-8");
PrintWriter out=response.getWriter();//웹브라우저 출력
try {
conn=DriverManager.getConnection(url,"JAVADB","1234");
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()) {
out.print("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.print("<meta charset=\"UTF-8\">");
out.print("<title>도서 목록 조회</title>");
out.print("</head>");
out.print("<body>");
out.print("<h2>전체 도서 목록</h2>");
out.print("<table border='1'>");
out.print("<tr><th>도서번호</th><th>도서코드</th><th>책 제목</th><th>저자</th><th>가격</th></tr>");
do {
out.print("<tr><td>"+rs.getInt("bno")+"</td><td>"+rs.getString("code")+"</td>"
+ "<td>"+rs.getString("title")+"</td><td>"+rs.getString("writer")+"</td><td>"+rs.getInt("price")+"</td></tr>");
}while(rs.next());
out.print("</table>");
out.print("</body>");
out.print("</html>");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
그다음에 doget에다가 넣어줌(겟방식에 넣으세요)
//db로딩
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.연결
Connection conn=null;
PreparedStatement pstmt=null;
String url="jdbc:oracle:thin:@localhost:1521:xe";
String sql="select *from booktbl";
ResultSet rs=null;
//웹브라우저 출력될 한글 타입 설정
response.setContentType("text/html; charset=utf-8");
PrintWriter out=response.getWriter();//웹브라우저 출력
conn=DriverManager.getConnection(url,"JAVADB","1234");
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()) {
out.print("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.print("<meta charset=\"UTF-8\">");
out.print("<title>도서 목록 조회</title>");
out.print("</head>");
out.print("<body>");
out.print("<h2>전체 도서 목록</h2>");
out.print("<table border='1'>");
out.print("<tr><th>도서번호</th><th>도서코드</th><th>책 제목</th><th>저자</th><th>가격</th></tr>");
do {
out.print("<tr><td>"+rs.getInt("bno")+"</td><td>"+rs.getString("code")+"</td>"
+ "<td>"+rs.getString("title")+"</td><td>"+rs.getString("writer")+"</td><td>"+rs.getInt("price")+"</td></tr>");
}while(rs.next());
out.print("</table>");
out.print("</body>");
out.print("</html>");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}