<%--
Created by IntelliJ IDEA.
User: leehayeon
Date: 2024. 11. 8.
Time: 11:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript">
const checkfrm = function () {
if (document.frm.dong.value.trim().length < 2 ) {
alert('2자 이상의 동이름을 입력하세요.');
return false;
}
document.frm.submit();
};
</script>
</head>
<body>
<!-- zipcode1.jsp -->
<form action="zipcode1_ok.jsp" method="post" name="frm">
동이름 입력 <input type="text" name="dong" maxlength="5" />
<input type="button" value="동이름 검색" onclick="checkfrm()"/>
</form>
</body>
</html>
-------------------------------------
<%--
Created by IntelliJ IDEA.
User: leehayeon
Date: 2024. 11. 8.
Time: 11:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%
String strDong = request.getParameter("dong");
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "exxyeon";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuilder sb = new StringBuilder();
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, strDong + "%");
rs = pstmt.executeQuery();
sb.append("<table border='1' cellspacing='0'>");
while (rs.next()) {
sb.append(("<tr>"));
sb.append(("<td>" + rs.getString("zipcode") + "</td>"));
sb.append(("<td>" + rs.getString("sido") + "</td>"));
sb.append(("<td>" + rs.getString("gugun") + "</td>"));
sb.append(("<td>" + rs.getString("dong") + "</td>"));
sb.append(("<td>" + rs.getString("ri") + "</td>"));
sb.append(("<td>" + rs.getString("bunji") + "</td>"));
sb.append(("</tr>"));
}
sb.append("</table>");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if (conn != null) conn.close();
if (rs != null) conn.close();
if (pstmt != null) conn.close();
}
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- zipcode1_ok -->
<%= sb.toString() %>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: kevin
Date: 2024-11-08
Time: 오전 11:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
StringBuilder sbHtml = new StringBuilder();
if (request.getParameter("dong") != null) {
String strDong = request.getParameter("dong");
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "exxyeon";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, strDong + "%");
rs = pstmt.executeQuery();
sbHtml.append("<table border='1' cellspacing='0'>");
while (rs.next()) {
sbHtml.append("<tr>");
sbHtml.append("<td>" + rs.getString("zipcode") + "</td>");
sbHtml.append("<td>" + rs.getString("sido") + "</td>");
sbHtml.append("<td>" + rs.getString("gugun") + "</td>");
sbHtml.append("<td>" + rs.getString("dong") + "</td>");
sbHtml.append("<td>" + rs.getString("ri") + "</td>");
sbHtml.append("<td>" + rs.getString("bunji") + "</td>");
sbHtml.append("</tr>");
}
sbHtml.append("<table>");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
%>
<html>
<head>
<title>Title</title>
<script type="text/javascript">
const checkfrm = function () {
if (document.frm.dong.value.trim().length < 2) {
alert('2자 이상의 동이름을 입력하셔야 합니다.');
return false;
}
document.frm.submit();
};
</script>
</head>
<body>
<!-- zipcode.jsp -->
<form action="zipcode.jsp" method="post" name="frm">
동이름 입력 <input type="text" name="dong" maxlength="5"/>
<input type="button" value="동이름 검색" onclick="checkfrm()"/>
</form>
<hr/>
<%=sbHtml.toString()%>
</body>
</html>
데이터베이스를 직접 연결에서 풀링을 통한 연결로 바꾸기 (zipcode.jsp)
<%--
Created by IntelliJ IDEA.
User: leehayeon
Date: 2024. 11. 8.
Time: 12:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.*" %>
<%
StringBuilder sbHtml = new StringBuilder();
if (request.getParameter("dong") != null) {
String strDong = request.getParameter("dong");
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "exxyeon";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource) envCtx.lookup("jdbc/mariadb1");
conn = dataSource.getConnection();
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, strDong + "%");
rs = pstmt.executeQuery();
sbHtml.append("<table border='1' cellspacing='0'>");
while (rs.next()) {
sbHtml.append("<tr>");
sbHtml.append("<td>" + rs.getString("zipcode") + "</td>");
sbHtml.append("<td>" + rs.getString("sido") + "</td>");
sbHtml.append("<td>" + rs.getString("gugun") + "</td>");
sbHtml.append("<td>" + rs.getString("dong") + "</td>");
sbHtml.append("<td>" + rs.getString("ri") + "</td>");
sbHtml.append("<td>" + rs.getString("bunji") + "</td>");
sbHtml.append("</tr>");
}
sbHtml.append("<table>");
} catch (NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
%>
<html>
<head>
<title>Title</title>
<script type="text/javascript">
const checkfrm = function () {
if (document.frm.dong.value.trim().length < 2) {
alert('2자 이상의 동이름을 입력하셔야 합니다.');
return false;
}
document.frm.submit();
};
</script>
</head>
<body>
<!-- zipcode.jsp -->
<form action="zipcode.jsp" method="post" name="frm">
동이름 입력 <input type="text" name="dong" maxlength="5"/>
<input type="button" value="동이름 검색" onclick="checkfrm()"/>
</form>
<hr/>
<%=sbHtml.toString()%>
</body>
</html>