Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<%@page contentType="text/html" pageEncoding="UTF-8"
import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>検索結果</title>
</head>
<body>
<h1>検索結果</h1>
<hr>
<table border="1">
<tr>
<th>著者</th><th>発行年</th><th>タイトル</th>
<th>出版</th>
</tr>
<%
request.setCharacterEncoding("UTF-8");
Connection db = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/BOOKS");
db = ds.getConnection();
ps = db.prepareStatement("SELECT * FROM books WHERE title LIKE ?");
ps.setString(1, "%" + request.getParameter("searcher") + "%");
rs = ps.executeQuery();
while(rs.next()) {
%>
<tr>
<td><%=rs.getString("name") %></td>
<td><%=rs.getString("year") %></td>
<td><%=rs.getString("title") %></td>
<td><%=rs.getString("publisher") %></td>
</tr>
<%
}
} catch(Exception e) {
throw new ServletException(e);
} finally {
try{
if(rs != null) {rs.close();}
if(ps != null) {ps.close();}
if(db != null) {db.close();}
}catch(Exception e) {}
}
%>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment