20220323 º¹½À
Address, Lego °ü¸®½Ã½ºÅÛ ¹Ýº¹ ¿¬½À
1. Java Build Path
-> Classpath (Add External JARs..) -> mysql-connector-java-8.0.27.jar Ãß°¡
2. Deployment Assembly
-> Add -> Java Build Path Entries -> mysql-connector-java-8.0.27.jar Ãß°¡
3. DB ¿¬°á/ÇØÁ¦ÇÏ´Â connect()/disconnect() ¸Þ¼µå ÀÛ¼º.
Connection conn=null;
PreparedStatement pstmt=null;
String jdbc_driver = "com.mysql.cj.jdbc.Driver";
String jdbc_url="jdbc:mysql://127.0.0.1:3306/db53";
String id="root";
String pw="123456";
try {
Class.forName(jdbc_driver);
conn=DriverManager.getConnection(jdbc_url, id, pw);
} catch (ClassNotFoundException e) {
e.getStackTrace();
} catch (SQLException e) {
e.getStackTrace();
}
4. ¸â¹öº¯¼ö(getter/setter) Ŭ·¡½º »ý¼º & ÇÊ¿äÇÑ ¸Þ¼µå ¸ð¾Æ³õÀº Ŭ·¡½º »ý¼º.
-> 3¹øÀÇ DB¿¬°á/ÇØÁ¦ ¸Þ¼µå ¸¸µé¸é¼ Ʋ ÀÛ¼º.
private String name;
private int num;
5. index.jsp ÀÛ¼º ( ¹Ù·Î ctl.jsp·Î À̵¿ )
-> pageContext.forward("ctl.jsp?command=select");
6. ctl.jsp ÀÛ¼º
select -> DB¿¡¼ µ¥ÀÌÅÍ °¡Á®¿Í¼ list.jsp¿¡ Ãâ·Â.
insert -> form.jsp¿¡¼ ÀÔ·ÂµÈ µ¥ÀÌÅ͸¦ DB¿¡ ÀÔ·Â.
edit -> list.jsp¿¡¼ ¼±ÅÃµÈ Ç׸ñ¸¸ DB¿¡¼ ÂüÁ¶ÇÏ¿© edit_form.jsp¿¡ Ãâ·Â.
update -> edit_form.jsp¿¡¼ ¼öÁ¤µÈ µ¥ÀÌÅ͸¦ DB¿¡ ÀÔ·Â.
delete -> edit_form.jsp¿¡¼ ¼±ÅÃµÈ primary key¸¦ ÂüÁ¶ÇÏ¿© ÇØ´ç µ¥ÀÌÅÍ »èÁ¦.
default -> ÆĶó¹ÌÅÍ ¿À·ù¸¦ ³ªÅ³»´Â ¹®±¸ Ãâ·Â.
[«³«Ô«ÚéÄ]
<% request.setCharacterEncoding("UTF-8"); %>
<%@ page import="java.util.*" %>
<jsp:useBean id="x" class="package.x" />
<jsp:setProperty property="*" name="x" />
String command = request.getParameter("command");
request.setAttribute("x", x);
pageContext.forward("x");
response.sendRedirect("x");
out.println("<script>alert('Please check Parameter out ^^')</script>");
7. list.jsp ÀÛ¼º
-> Insert ¹öÆ°.
-> À̸§ Ŭ¸¯½Ã ÇØ´ç Ç׸ñÀÇ µ¥ÀÌÅ͵éÀ» edit_formÀ¸·Î °¡Á®°¥ ¼ö ÀÖ´Â ¸µÅ©.
[«³«Ô«ÚéÄ]
<table width=100% border=1>
<tr>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
</tr>
<% for( DataType x : Arr ) { %>
<tr>
<td><%=x.getPrimaryKey %></td>
<td><%=x.getXxx %></td>
<td><a href="x.jsp?command=edit&x=<%=x.getPrimaryKey%>"><%=x.getXXX %></a></td>
<td><%=x.getXxx %></td>
<td><%=x.getXxx %></td>
</tr>
<% } %>
</table>
<a href="/Project/Folder/X.jsp">LinkName</a>
<form method="post" action="x.jsp">
<input type="submit" value="Button Name">
</form>
8. form.jsp ÀÛ¼º
-> hiddenÀ¸·Î ³Ñ°Ü¾ßÇÒ °ªµé ÁÖÀÇ.
-> head ¾È¿¡ script¸¦ »ç¿ëÇÏ¿© 'À¯È¿¼º °Ë»ç' ½ÇÇà.
[«³«Ô«ÚéÄ]
<input type="hidden" name="command" value="insert">
<input type="text" name="x">
<input type="radio" name="YorN" value="Y">Y
<input type="radio" name="YorN" value="N">N
<select name="x">
<option value="x" selected>x
<option value="x">x
<option value="x">x
</select>
<input type="button" value="Button Name" onClick=Function Name()>
<script type="text/javascript">
function FunctionName(){
alert('À¯È¿¼º °Ë»ç ÁøÇà');
document.FormName.submit();
}
</script>
9. edit_form.jsp ÀÛ¼º
-> hiddenÀ¸·Î Ä¿¸Çµå, primaryKey µî ³Ñ°ÜÁÖ±â ÁÖÀÇ.
[«³«Ô«ÚéÄ] - value°ª ¼³Á¤ ÇÊ¿ä.
<input type="text" name="x" value=<%=X.getXxx %> readonly>
<input type="submit" value="Update!"> <input type="button" value="Delete!" onClick=delCheck()>
<script type="text/javascript">
function delCheck(){
result = confirm('Á¤¸»·Î »èÁ¦ÇϽðڽÀ´Ï±î ?');
if(result == true)
{
document.FormName.command.value="delete";
document.FormName.submit();
} else {
return;
}
}
</script>
10. jsp ÆäÀÌÁö ¸¸µé ¶§
-> ctl.jsp¿¡ À̵¿°æ·Î¿Í ÇÊ¿äÇÑ ¸Þ¼µå ¼±¾ð.
-> ÇÊ¿äÇÑ ¼ø¼´ë·Î ¸Þ¼µå Ŭ·¡½º¿¡ ÇÔ¼ö ÀÛ¼º.
-> connect(), try~catch~ ÁÖÀÇ
[«³«Ô«ÚéÄ]
pstmt=conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
}
pstmt.executeUpdate();
String sql="insert into TABLE(x,x) values(?,?)";
String sql ="select * from TABLE";
String sql = "select * from TABLE where PRIMARYKEY=?"
String sql = "update TABLE set x=?,x=? where PRIMARYKEY=?";
String sql = "delete from TABLE where PRIMARYKEY=?"