親フォルダ
001.<%@ page
002.    language="java"
003.    import="java.io.*"
004.    import="java.text.*"
005.    import="java.util.*"
006.    import="java.text.SimpleDateFormat"
007.    import="com.mysql.jdbc.*"
008.    import="java.sql.DriverManager"
009.    import="java.sql.ResultSet"
010.    import="java.sql.SQLException"
011.    contentType="text/html;charset=utf-8" %>
012.<%!
013. 
014.ServletContext app = null;
015.HttpServletRequest req = null;
016.HttpServletResponse res = null;
017. 
018.// ***************************
019.// ログ出力
020.// ***************************
021.public void outlog( String message ) {
022. 
023.    app.log( String.format("<<JSP>> %s", message )  );
024. 
025.}
026. 
027.// ***************************
028.// ヘッダー
029.// ***************************
030.public void header( ) {
031. 
032.    res.addHeader("Content-Type","text/html; charset=utf-8");
033.    res.addHeader("Expires","Thu, 19 Nov 1981 08:52:00 GMT");
034.    res.addHeader("Cache-Control","text/html; no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
035.    res.addHeader("Pragma","no-cache");
036. 
037.}
038. 
039.// ***************************
040.// HTML TD
041.// ***************************
042.public void td( StringBuffer sb, String target ) {
043. 
044.    sb.append("<td>");
045.    sb.append( target );
046.    sb.append("</td>");
047. 
048.}
049. 
050.// ***************************
051.// forms : 値取得
052.// ***************************
053.public String forms( String fieldName ) {
054. 
055.    String result;
056.    result = req.getParameter( fieldName );
057.    if ( result == null ) {
058.        result = "";
059.    }
060.    return result;
061. 
062.}
063. 
064.%>
065.<%
066.// *********************************************************
067.// 入力値の表示
068.// GET で setCharacterEncoding を有効にするには
069.// sever.xml => Connector で useBodyEncodingForURI="true"
070.// *********************************************************
071.request.setCharacterEncoding("utf-8");  // 入力値のエンコーディング
072.app = application;
073.req = request;
074.res = response;
075.header();
076. 
077.String checkMessage = "";
078. 
079.// ***************************
080.// 入力一覧
081.// ***************************
082.StringBuffer sb = new StringBuffer();
083.for( String name : Collections.list(request.getParameterNames()) ){
084.    sb.append("<tr>");
085.    td( sb, name );
086.    td( sb, forms( name ) );
087.    sb.append("</tr>");
088.}
089. 
090. 
091.if ( forms("send1").equals("送信1") ) {
092. 
093.    checkMessage += " >> 送信1 がクリックされました";
094. 
095.}
096.if ( forms("send2").equals("送信2") ) {
097. 
098.    checkMessage += " >>送信2 がクリックされました";
099. 
100.}
101. 
102.%>
103.<!DOCTYPE html>
104.<html>
105.<head>
106.<style>
107.table {
108.    margin-top: 20px;
109.    margin-bottom: 20px;
110.    border-collapse: collapse;
111.    border:1px solid #ccc;
112.}
113.td {
114.    padding: 5px;
115.    border: solid #ccc 1px;
116.}
117.</style>
118.</head>
119.<body>
120.<h4><%= checkMessage %></h4>
121.<div><a href="<%= request.getRequestURI() %>">GET 再読み込み</a></div>
122. 
123.<h4>フォーム : GET</h4>
124.<form method="get">
125.    <input type="text" name="field_get" value="<%= forms("field_get") %>">
126.    <input type="submit" name="send1" value="送信1">
127.</form>
128. 
129.<h4>フォーム : POST</h4>
130.<form method="post">
131.    <input type="text" name="field_post" value="<%= forms("field_post") %>">
132.    <input type="submit" name="send2" value="送信2">
133.</form>
134. 
135.<table>
136.<%= sb.toString() %>
137.</table>
138. 
139.<%
140. 
141.Connection conn = null;
142.Statement stmt = null;
143.ResultSet rs = null;
144. 
145.try {
146.    Class.forName("com.mysql.jdbc.Driver");
147.    // MySQL Connector/J 接続
148.    conn = (Connection) DriverManager.getConnection(
149.        "jdbc:mysql://localhost/lightbox?user=root&password="
150.    );
151. 
152.    stmt = (Statement) conn.createStatement();
153.    rs = stmt.executeQuery("select * from 社員マスタ");
154. 
155.    // select の結果の列情報の取得
156.    ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
157. 
158.    // 列数
159.    int columnCount = rsmd.getColumnCount();
160. 
161.    // 列名
162.    for( int i = 1; i <= columnCount; i++) {
163.        out.println(rsmd.getColumnName(i));
164.    }
165.    out.println("\n<br>");
166. 
167.    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
168.    while( rs.next() ) {
169. 
170.        for( int i = 1; i <= columnCount; i++) {
171. 
172.            if ( rsmd.getColumnTypeName(i).equals("DATETIME") ) {
173.                out.println( sdf.format(rs.getDate(i)) );
174.            }
175.            else {
176.                out.println( rs.getString(i) );
177.            }
178. 
179.            out.println(",");
180. 
181.        }
182.        out.println("\n<br>");
183.    }
184. 
185.    rs.close();
186.    stmt.close();
187.    conn.close();
188. 
189.} catch (SQLException ex) {
190.    // handle any errors
191.    out.println("SQLException: " + ex.getMessage());
192.    out.println("SQLState: " + ex.getSQLState());
193.    out.println("VendorError: " + ex.getErrorCode());
194.}
195. 
196.%>
197. 
198.</body>
199.</html>