IT/Jsp

Jsp 데이터베이스 연동

노마드오브 2018. 7. 22. 23:36

1. apache.org 사이트에 가서 톰캣 다운로드, 톰캣 설치

2. 이클립스에서 server추가

3. ojdbc.jar 추가



New > Dynamic Web Project를 선택해서 프로젝트 생성

WebContent에서 New > Jsp파일 생성

 


파일명 : booklist.jsp


<%@ page import="java.sql.*" contentType="text/html;charset=EUC-KR" %>

<%

Class.forName("oracle.jdbc.driver.OracleDriver");

String url="jdbc:oracle:thin:@localhost:1521:XE";

Connection dbconn=DriverManager.getConnection(url,"madang","madang");

Statement stmt = dbconn.createStatement();

ResultSet myResultSet=stmt.executeQuery("select * from book");

%>

<html>


<head>

<meta charset="EUC-KR">

<title>** BOOK LIST **</title>

</head>

<body>

<table border="1">


<%

if (myResultSet != null) {

while(myResultSet.next()){

String W_BOOKID=myResultSet.getString("bookid");

String W_BOOKNAME=myResultSet.getString("bookname");

String W_PUBLISJER=myResultSet.getString("publisher");

String W_PRICE=myResultSet.getString("price");


%>

<tr>

<td><%=W_BOOKID%></td>

<td><a href="bookview.jsp?bookid=<%=W_BOOKID%>"><%=W_BOOKNAME%></a></td>

<td><%=W_PUBLISJER%></td>

<td><%=W_PRICE%></td>

</tr>

<%

}

}


stmt.close();

dbconn.close();

%>


</table>


</body>

</html>






파일명 : bookview.jsp


<%@ page import="java.sql.*" contentType="text/html; charset=EUC-KR" %>

<%

Class.forName("oracle.jdbc.driver.OracleDriver");

String url = "jdbc:oracle:thin:@localhost:1521:XE";

Connection dbconn=DriverManager.getConnection(url,"madang","madang");

Statement stmt = dbconn.createStatement();

String bookid=request.getParameter("bookid");

ResultSet myResultSet = stmt.executeQuery("select * from book where bookid='"+bookid+"'");

if(myResultSet!=null){

myResultSet.next();

%>


<html>

<head>

<meta charset="EUC-KR">

<title> ** Book VIEW ** </title>

</head>

<body>


<table border="1">

<tr>

<td>책제목</td>

<td><%=myResultSet.getString("BOOKNAME")%></td>

</tr>

<tr>

<td>출판사</td>

<td><%=myResultSet.getString("PUBLISHER")%></td>

</tr>

<tr>

<td>가격</td>

<td><%=myResultSet.getString("PRICE")%></td>

</tr>

</table>

<table>

<tr>

<td>

<a href="booklist.jsp">목록</a>

</td>

</tr>

</table>

<%

}

stmt.close();

dbconn.close();

%>


</body>

</html>