<%@ page language="java" import="java.io.*" import="java.text.*" import="java.util.*" import="java.text.SimpleDateFormat" import="com.mysql.jdbc.*" import="java.sql.DriverManager" import="java.sql.ResultSet" import="java.sql.SQLException" contentType="text/html;charset=utf-8" %> <%! ServletContext app = null; HttpServletRequest req = null; HttpServletResponse res = null; // *************************** // ログ出力 // *************************** public void outlog( String message ) { app.log( String.format("<<JSP>> %s", message ) ); } // *************************** // ヘッダー // *************************** public void header( ) { res.addHeader("Content-Type","text/html; charset=utf-8"); res.addHeader("Expires","Thu, 19 Nov 1981 08:52:00 GMT"); res.addHeader("Cache-Control","text/html; no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); res.addHeader("Pragma","no-cache"); } // *************************** // HTML TD // *************************** public void td( StringBuffer sb, String target ) { sb.append("<td>"); sb.append( target ); sb.append("</td>"); } // *************************** // forms : 値取得 // *************************** public String forms( String fieldName ) { String result; result = req.getParameter( fieldName ); if ( result == null ) { result = ""; } return result; } %> <% // ********************************************************* // 入力値の表示 // GET で setCharacterEncoding を有効にするには // sever.xml => Connector で useBodyEncodingForURI="true" // ********************************************************* request.setCharacterEncoding("utf-8"); // 入力値のエンコーディング app = application; req = request; res = response; header(); String checkMessage = ""; // *************************** // 入力一覧 // *************************** StringBuffer sb = new StringBuffer(); for( String name : Collections.list(request.getParameterNames()) ){ sb.append("<tr>"); td( sb, name ); td( sb, forms( name ) ); sb.append("</tr>"); } if ( forms("send1").equals("送信1") ) { checkMessage += " >> 送信1 がクリックされました"; } if ( forms("send2").equals("送信2") ) { checkMessage += " >>送信2 がクリックされました"; } %> <!DOCTYPE html> <html> <head> <style> table { margin-top: 20px; margin-bottom: 20px; border-collapse: collapse; border:1px solid #ccc; } td { padding: 5px; border: solid #ccc 1px; } </style> </head> <body> <h4><%= checkMessage %></h4> <div><a href="<%= request.getRequestURI() %>">GET 再読み込み</a></div> <h4>フォーム : GET</h4> <form method="get"> <input type="text" name="field_get" value="<%= forms("field_get") %>"> <input type="submit" name="send1" value="送信1"> </form> <h4>フォーム : POST</h4> <form method="post"> <input type="text" name="field_post" value="<%= forms("field_post") %>"> <input type="submit" name="send2" value="送信2"> </form> <table> <%= sb.toString() %> </table> <% Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); // MySQL Connector/J 接続 conn = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost/lightbox?user=root&password=" ); stmt = (Statement) conn.createStatement(); rs = stmt.executeQuery("select * from 社員マスタ"); // select の結果の列情報の取得 ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData(); // 列数 int columnCount = rsmd.getColumnCount(); // 列名 for( int i = 1; i <= columnCount; i++) { out.println(rsmd.getColumnName(i)); } out.println("\n<br>"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); while( rs.next() ) { for( int i = 1; i <= columnCount; i++) { if ( rsmd.getColumnTypeName(i).equals("DATETIME") ) { out.println( sdf.format(rs.getDate(i)) ); } else { out.println( rs.getString(i) ); } out.println(","); } out.println("\n<br>"); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex) { // handle any errors out.println("SQLException: " + ex.getMessage()); out.println("SQLState: " + ex.getSQLState()); out.println("VendorError: " + ex.getErrorCode()); } %> </body> </html>