관리 메뉴

nkdk의 세상

14장 jsp db를 실행하자 db에 트랜잭션까지~~ 본문

My Programing/JSP and Servlet

14장 jsp db를 실행하자 db에 트랜잭션까지~~

nkdk 2008. 3. 9. 01:04
dbtran.jsp

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

Connection conn=null;
PreparedStatement pstmt=null, pstmt1=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mysqldb","root","123");
pstmt=conn.prepareStatement("insert into sangdata(sang, su, dan) values(?,?,?)");
} catch (Exception e) {
System.out.println("연결 실패 : " + e);
return;
}
%><%
try{
conn.setAutoCommit(false);
pstmt.setString(1,"신상품");
pstmt.setInt(2,20);
pstmt.setInt(3,500);
int count = pstmt.executeUpdate();
System.out.println(count+"개 추가");
conn.commit();
conn.setAutoCommit(true);
pstmt1 = conn.prepareStatement("select * from sangdata");
rs = pstmt1.executeQuery();
}catch(Exception ex) {
System.out.println("ss"+ex);
}
%>
<html><body><center>** 상품 자료 출력**
<table width=300 border=1>
<tr align=center bgcolor=cyan>
<th>상품명</th><th>수량</th><th>단가</th>
</tr>
<%int i=1;
while(rs.next()) {
if(i%2 ==0) out.println("<tr bgcolor=pink>");

else out.println("<tr bgcolor=white>");
%>
<td><%=rs.getString("sang") %></td>
<td><%=rs.getString("su") %></td>
<td><%=rs.getString("dan") %></td>
</tr>
<%
i++;
}
rs.close(); pstmt.close(); conn.close();
%>

</table>
</center>
</body></html>

jspmon1_1.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.sql.*"%>
<html><body>
<h2><center>jsp문제 1</center></h2>
<center><a href="/jsp/jspmon1.jsp">전체자료</a>&&&&<a href="/jsp/jspmon1_2.html">상품입력</a><br><br>
</center>
<%
Connection conn=null;
PreparedStatement pstmt=null, pstmt1=null;
ResultSet rs=null;
String sang = request.getParameter("sang");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mysqldb","root","123");
pstmt = conn.prepareStatement("select * from sangdata where sang='"+sang+"'");
rs = pstmt.executeQuery();
} catch (Exception e) {
System.out.println("연결 실패 : " + e);
return;
}
%>
<center>** 상품 자료 출력**
<table width=300 border=1>
<tr align=center bgcolor=cyan>
<th>코드</th><th>상품명</th><th>수량</th><th>단가</th><th>단가</th>
</tr>
<%
while(rs.next()) {
out.println("<tr bgcolor=white>");
String code = rs.getString("code");
String sangs = rs.getString("sang");
%>
<td><% out.println("<a href=/jsp/jspmondel.jsp?code="+code+">"+code+"</a>"); %></td>
<td><% out.println("<a href=/jsp/jspmon1_1.jsp?sang="+sangs+">"+sangs+"</a>"); %></td>
<td><%=rs.getString("su") %></td>
<td><%=rs.getString("dan") %></td>
<td><% out.println(Integer.parseInt(rs.getString("su"))*Integer.parseInt(rs.getString("dan"))); %></td>
</tr>
<%
}
%>


</body>
</html>

jspmon1.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.sql.*"%>
<html><body>
<h2><center>jsp문제 1</center></h2>
<center><a href="/jsp/jspmon1.jsp">전체자료</a>&&&&<a href="/jsp/jspmon1_2.html">상품입력</a><br><br>
</center>
<%
Connection conn=null;
PreparedStatement pstmt=null, pstmt1=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mysqldb","root","123");
pstmt = conn.prepareStatement("select * from sangdata");
rs = pstmt.executeQuery();
} catch (Exception e) {
System.out.println("연결 실패 : " + e);
return;
}
%>
<center>** 상품 자료 출력**
<table width=300 border=1>
<tr align=center bgcolor=cyan>
<th>코드</th><th>상품명</th><th>수량</th><th>단가</th><th>단가</th>
</tr>
<%
int sum = 0;
while(rs.next()) {
out.println("<tr bgcolor=white>");
String code = rs.getString("code");
String sang = rs.getString("sang");

%>
<td><% out.println("<a href=/jsp/jspmondel.jsp?code="+code+">"+code+"</a>"); %></td>
<td><% out.println("<a href=/jsp/jspmon1_1.jsp?sang="+sang+">"+sang+"</a>"); %></td>
<td><%=rs.getString("su") %></td>
<td><%=rs.getString("dan") %></td>
<td><% out.println(Integer.parseInt(rs.getString("su"))*Integer.parseInt(rs.getString("dan")));
sum += Integer.parseInt(rs.getString("su"))*Integer.parseInt(rs.getString("dan"));
%></td>
</tr>
<%
}
out.println("<tr><td colspan=4>총계</td><td>"+sum+"</td></tr>");
%>
</body>
</html>

jspmondel.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html><body>
<%
String no = request.getParameter("code");
out.println("<html><body><center><p>");
out.println("<h2>" + no + "번 자료를 정말 삭제할까요?</h2>");
out.println("<form method=post action=/jsp/jspmondelok.jsp>");
out.println("<input type=hidden name=code value=" +no+">");
out.println("<input type=submit value=삭제>");
out.println("<input type=button value=취소 onClick=history.back()>");
out.println("</form></center></body></html>");
%>
</body></html>

jspmon1_2.html

<html><body>
<p><center>
<form method=post action=/jsp/jspmonins.jsp>
<table width=50%>
<tr>
<td width=40%>상품이름</td><td><input type=text name=sang size=20></td>
</tr>
<tr>
<td width=40%>갯수</td><td><input type=text name=su size=20></td>
</tr>
<tr>
<td width=40%>단가</td><td><input type=text name=dan size=20></td>
</tr>
<tr align=center>
<td colspan = 2>
<input type=submit value=" 등 록 ">
<input type=reset value="입력취소">
</td>
</tr>
</table>
</form>
</body>
</html>

jspmonins.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.sql.*" %>
<%
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
pstmt=conn.prepareStatement("insert into sangdata(sang, su, dan) values(?,?,?)");
} catch (Exception e) {
System.out.println("연결실패:"+e);
return;
}

request.setCharacterEncoding("euc-kr");
String sang = request.getParameter("sang");
String su = request.getParameter("su");
String dan = request.getParameter("dan");
try {
conn.setAutoCommit(false);
pstmt.setString(1,sang);
pstmt.setString(2,su);
pstmt.setString(3,dan);
int count = pstmt.executeUpdate();
System.out.println(count+"개 추가");
conn.commit();
conn.setAutoCommit(true);
} catch (Exception e) {
System.out.println("입력 오류 : " + e);
return;
}
response.sendRedirect("/jsp/jspmon1.jsp");

try {
pstmt.close();
conn.close();
} catch (Exception e) {
System.out.println(e+"dd");
}
%>

jspmondelok.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.sql.*" %>
<html><body>
<%
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
pstmt=conn.prepareStatement("delete from sangdata where code=?");
} catch (Exception e) {
System.out.println("연결실패:"+e);
return;
}
String code=request.getParameter("code");
try {
pstmt.setString(1, code);
pstmt.executeUpdate();
} catch (Exception e) {
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
out.println("삭제되지 않았습니다.<a href=/jsp/jspmon1.jsp>돌아가기</a>");
return;
}
pstmt.close();
conn.close();
response.sendRedirect("/jsp/jspmon1.jsp");
%>

</body></html>