root/trunk/myweb/WebContent/Jdbc2.jsp

Revision 51, 1.3 KB (checked in by yabuki, 2 years ago)
  • Property svn:mime-type set to text/plain
Line 
1<?xml version="1.0" encoding="UTF-8" ?>
2<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7<title>スクリプトレットの利用</title>
8
9</head>
10<body>
11<%
12  Class.forName("com.mysql.jdbc.Driver").newInstance();
13  String uri="jdbc:mysql://localhost/mydb?characterEncoding=UTF-8";
14  Connection conn = DriverManager.getConnection(uri,"test","pass");
15
16  Statement stmt = conn.createStatement();
17  String sql="INSERT INTO samples (sample,memo) "
18              +"VALUES ('JDBC TEST 2','"+(new java.util.Date())+"')";
19  stmt.executeUpdate(sql);
20
21  ResultSet rs=stmt.executeQuery("SELECT * FROM samples");
22%>
23
24<table summary="Samplesの内容">
25<caption>Samplesの内容</caption>
26<tr><th>id</th><th>sample</th><th>memo</th></tr>
27
28<%
29  while (rs.next())
30    out.println("<tr>"
31                 +"<td>"+rs.getInt("id")+"</td>"
32                 +"<td>"+rs.getString("sample")+"</td>"
33                 +"<td>"+rs.getString("memo")+"</td>"
34               +"</tr>");
35%>
36
37</table>
38</body>
39</html>
Note: See TracBrowser for help on using the browser.