Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <%@page import="java.sql.*,javax.sql.*,javax.naming.*"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>Postgres JSP</title>
  7. </head>
  8. <body>
  9. <h2>Postgres JSP</h2>
  10. <%
  11. Context initContext = new InitialContext();
  12. DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/public_PostgreSQL");
  13. Connection con = ds.getConnection();
  14. out.println("conn=" + con.toString() + "<br />");
  15. // 全ての行を検索するSQL文を作成
  16. Statement stmt = con.createStatement();
  17. String sql = "select name from person";
  18. // クエリーを実行して結果セットを取得
  19. ResultSet rs = stmt.executeQuery(sql);
  20. // 検索された行数分ループ
  21. while (rs.next()) {
  22. // 結果セットからnameを取得
  23. String name = rs.getString("name");
  24. // 表示
  25. out.println("person.name=" + name);
  26. out.println("<br />");
  27. }
  28. stmt.close();
  29. // データベースから切断
  30. con.close();
  31. %>
  32. </body>
  33. </html>
Add Comment
Please, Sign In to add comment