import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class JDBCTestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doIt(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doIt(request, response); } private void doIt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("Shift_JIS"); response.setContentType("text/html; charset=Shift_JIS"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("JDBCTestServlet"); out.println(""); out.println(""); out.println("

"); out.println("タイトルに"); out.println("Java"); out.println("という文字列を含む本は"); out.println("

"); out.println("

"); try { Class.forName("org.hsqldb.jdbcDriver"); String url = "jdbc:hsqldb:hsql://localhost"; Connection con = DriverManager.getConnection(url, "sa", ""); String selectStatement = "select title " + "from books where title like ?"; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, "%" + "Java" + "%"); ResultSet rs = prepStmt.executeQuery(); while (rs.next()) { String title = rs.getString("title"); out.println(title); out.println("
"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } out.println("

"); out.println(""); out.println(""); } }