Servletでコネクションプールを使うための記述をしてみたのですが、
69行目の「conn = ds.getConnection();」で下記のようなエラーが出てしまいました。
「The method getConnection() is undefined for the type DataSource」
エラーを消すためにquick fixを試してみたり、
最初にdsを定義している24行目の「DataSource ds;」の書き方に問題が無いか再度確認してみたのですが、
原因が掴めていません。
ご助力のほど、よろしくお願い致します。
package info.searchman.lesson.java_mysql; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.activation.DataSource; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SearchServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; DataSource ds; public void init() throws ServletException { try { InitialContext ic = new InitialContext(); ds = (DataSource) ic.lookup("java:comp/env/jdbc/searchman"); } catch (Exception e) { } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // initial settings of DB Connection conn = null; PreparedStatement pstmt = null; ResultSet rset = null; // setting text code request.setCharacterEncoding("Windows-31J"); // get the name in index.jsp String name = request.getParameter("name"); // get the id in index.jsp String id = request.getParameter("id"); // get the sei in index.jsp String sei = request.getParameter("sei"); // get the nen in index.jsp String nen = request.getParameter("nen"); try { // register JDBC Driver // Class.forName("com.mysql.jdbc.Driver"); // // creating Connection // conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/company_db?serverTimezone=UTC&useSSL=false", // "suser", "spass"); conn = ds.getConnection(); // preparing for creating sql StringBuffer sql = new StringBuffer(); // creating sql from name sql.append("select id, name, sei, nen, address from shain_table where name like '%"); sql.append(name + "%'"); // if id is selected, add it if (id != "") { sql.append("and id ='" + id + "'"); } // if sei is selected, add it if (sei != "") { sql.append("and sei ='" + sei + "'"); } // if nen is selected, add it if (nen != "") { sql.append("and nen ='" + nen + "'"); } // display sql System.out.println(sql); // display sql sentence pstmt = conn.prepareStatement(new String(sql)); // execute sql pstmt.execute(); // put the executed result into ResultSet class rset = pstmt.executeQuery(); // transfer the data to the transition page(put it by Attribute) request.setAttribute("kekka", rset); // move on to search.jsp request.getRequestDispatcher("/search.jsp").forward(request, response); // terminate the used objects rset.close(); pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); String status ="Seearch is failed. Please contact the system manager"; request.setAttribute("status", status); request.getRequestDispatcher("/result.jsp").forward(request, response); } finally { try { // just in case, terminate the DB connection with finally statement conn.close(); } catch (Exception e) { } } } }
回答1件
あなたの回答
tips
プレビュー