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>
<%
Connection db = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//データベースへの接続を確立
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/BOOKS");
db = ds.getConnection();
//SELECT命令の準備
ps = db.prepareStatement("SELECT * FROM books ORDER BY id");
//SELECT命令を実行
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