Created
October 8, 2012 14:36
-
-
Save onelittlenightmusic/3852857 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.testtomcat; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
//import javax.sql.DataSource; | |
import org.apache.tomcat.jdbc.pool.DataSource; | |
import org.apache.tomcat.jdbc.pool.PoolProperties; | |
import org.testinterceptor.TestInterceptor; | |
public class TestServlet extends HttpServlet { | |
public void doGet(HttpServletRequest req, HttpServletResponse res) | |
throws ServletException, IOException { | |
// 要求文字コードのセット | |
req.setCharacterEncoding("Shift_JIS"); | |
// 応答文字コードのセット | |
res.setContentType("text/html; charset=Shift_JIS"); | |
// 出力ストリームの取得 | |
PrintWriter out = res.getWriter(); | |
double radius = Double.parseDouble(req.getParameter("hankei")); | |
double pi = Math.PI; | |
double area = pi * radius * radius; | |
double circum = 2.0 * pi * radius; | |
// クライアント(ブラウザ)への出力 | |
out.println("<html lang=\"ja\">"); | |
out.println("<head><title>円の面積と円周</title>"); | |
out.println("</head><body>"); | |
out.println("<h1>円の面積と円周</h1>"); | |
out.println("<table border=\"1\">"); | |
out.println("<tr><th>半径</th><td>" + radius + "</td></tr>"); | |
out.println("<tr><th>円周</th><td>" + circum + "</td></tr>"); | |
out.println("<tr><th>面積</th><td>" + area + "</td></tr>"); | |
out.println("</table>"); | |
out.println("</body></html>"); | |
try { | |
InitialContext context = new InitialContext(); | |
DataSource ds = (DataSource) context | |
.lookup("java:comp/env/jdbc/MySQLDB"); | |
long th = 100; | |
TestInterceptor.setThresholdStatic(th); | |
// PoolProperties p = new PoolProperties(); | |
// p.setJdbcInterceptors(String.format("org.testinterceptor.TestInterceptor(threshold=%d)",th)); | |
// ds.setPoolProperties(p); | |
Connection con = ds.getConnection(); | |
System.out.println(con.getCatalog()); | |
Statement stmt = con.createStatement(); // (4) | |
ResultSet result = stmt.executeQuery("select * from testtable;"); | |
while (result.next()) { | |
out.println(String.format("%s: %s", result.getInt(1), | |
result.getString(2))); | |
} | |
// PoolProperties p = new PoolProperties(); | |
// p.setUrl("jdbc:mysql://localhost:3306/test"); | |
// p.setDriverClassName("com.mysql.jdbc.Driver"); | |
// p.setUsername("hiro"); | |
// p.setPassword("0922"); | |
// p.setJmxEnabled(true); | |
// p.setTestWhileIdle(false); | |
// p.setTestOnBorrow(true); | |
// p.setValidationQuery("SELECT 1"); | |
// p.setTestOnReturn(false); | |
// p.setValidationInterval(30000); | |
// p.setTimeBetweenEvictionRunsMillis(30000); | |
// p.setMaxActive(100); | |
// p.setInitialSize(10); | |
// p.setMaxWait(10000); | |
// p.setRemoveAbandonedTimeout(60); | |
// p.setMinEvictableIdleTimeMillis(30000); | |
// p.setMinIdle(10); | |
// p.setLogAbandoned(true); | |
// p.setRemoveAbandoned(true); | |
// p.setJdbcInterceptors( | |
// "org.testtomcat.TestInterceptor"); | |
// org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource(); | |
// datasource.setPoolProperties(p); | |
// Connection con2 = ds.getConnection(); | |
// System.out.println(con2.getCatalog()); | |
// Statement stmt2 = con.createStatement(); // (4) | |
// ResultSet result2 = stmt2.executeQuery("select * from testtable;"); | |
// while (result2.next()) { | |
// out.println(String.format("%s: %s", result2.getInt(1), | |
// result2.getString(2))); | |
// } | |
} catch (NamingException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
// 出力ストリームを閉じる | |
out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment