ServletとJSPで家計簿アプリを作成しています。
Insert文を発行し、MySQLにユーザ入力値を入れようとすると、
「java.sql.SQLException: No suitable driver」エラーが発生してしまいます。
現状の設定は以下のようになっています。
WEB-INF
lib
mysql-connector-java-8.0.11.jar
上記をクラスパスにも登録しています。
解決のため、ご助力頂けますと幸いです。
BudgetTrackerInsertDao.java
package com.jdbc.budgettracker.dao; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import java.util.Properties; import com.jdbc.budgettracker.core.BudgetTrackerDto; public class BudgetTrackerInsertDao { BudgetTrackerDto btd; // PreparedStatement pstmt = null; ResultSet rset = null; private Connection myConn; private static final String INSERTINTOBUDGETTABLE = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) VALUE (?,?,?,?,?,?);"; private static Connection getConnection() { try { // get db properties Properties props = new Properties(); props.load(new FileInputStream( "/home/yosuke/servlet_budget_tracker_workspace/budget_tracker_servlet/WebContent/WEB-INF/sql/config_budgettracker.properties")); String user = props.getProperty("user"); String password = props.getProperty("password"); String dburl = props.getProperty("dburl"); Connection con = DriverManager.getConnection(dburl, user, password); return con; } catch (Exception e) { throw new IllegalStateException(e); } } //Insert public void insertIntoTable(BudgetTrackerDto btd) throws SQLException { int rowsCount = 0; PreparedStatement pstmt = null; try { // DBに接続 myConn = BudgetTrackerInsertDao.getConnection(); pstmt = myConn.prepareStatement(INSERTINTOBUDGETTABLE); pstmt.setInt(1, btd.getId()); Date date = btd.getDate(); long timeInMilliSeconds = date.getTime(); java.sql.Date sqlDate = new java.sql.Date(timeInMilliSeconds); pstmt.setDate(2, sqlDate); pstmt.setString(3, btd.getStoreName()); pstmt.setString(4, btd.getProductName()); pstmt.setString(5, btd.getProductType()); pstmt.setInt(6, btd.getPrice()); // SQL文発行 pstmt.executeUpdate(); System.out.println("Successfully added"); pstmt.close(); // rowsCount = pstmt.executeUpdate(sql); } catch (SQLException e) { System.out.println("Errorが発生しました!\n" + e + "\n"); } finally { // リソースの開放 if (pstmt != null) { try { pstmt.close(); } catch (SQLException ignore) { } } if (myConn != null) { try { myConn.close(); } catch (SQLException ignore) { } } } } }
BudgetTrackerInsertController.java
package com.jdbc.budgettracker.controller; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jdbc.budgettracker.core.BudgetTrackerDto; import com.jdbc.budgettracker.dao.BudgetTrackerInsertDao; import com.jdbc.budgettracker.dao.BudgetTrackerSearchDao; public class BudgetTrackerInsertController extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rset = null; BudgetTrackerDto budgetTrackerDto; BudgetTrackerInsertDao budgetTrackerInsertDao = null; request.setCharacterEncoding("Windows-31J"); // request id int id = 0; try { id = Integer.parseInt(request.getParameter("id")); } catch (NumberFormatException e) { System.out.println(id + " は数値ではありません。 "); } // request date String dateStr = request.getParameter("date"); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // long timeInMilliSeconds = date.getTime(); // java.sql.Date sqlDate1 = new java.sql.Date(timeInMilliSeconds); // request store name String storeName = request.getParameter("storeName"); // request product name String productName = request.getParameter("productName"); // request product type String productType = request.getParameter("productType"); // request price int price = 0; try { price = Integer.parseInt(request.getParameter("price")); } catch (NumberFormatException e) { System.out.println(price + " は数値ではありません。 "); } budgetTrackerDto = new BudgetTrackerDto(); budgetTrackerDto.setId(id); budgetTrackerDto.setDate(date); budgetTrackerDto.setStoreName(storeName); budgetTrackerDto.setProductName(productName); budgetTrackerDto.setProductType(productType); budgetTrackerDto.setPrice(price); budgetTrackerInsertDao = new BudgetTrackerInsertDao(); try { budgetTrackerInsertDao.insertIntoTable(budgetTrackerDto); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // request.setAttribute("result", rset); // // request.getRequestDispatcher("/search.jsp").forward(request, response); } }
エラー内容
0 は数値ではありません。 Jan 03, 2022 9:05:02 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [InsertController] in context with path [/budget_tracker_servlet] threw exception java.lang.IllegalStateException: java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.0.68:3306/budgettracker?allowPublicKeyRetrieval=true&useSSL=false at com.jdbc.budgettracker.dao.BudgetTrackerInsertDao.getConnection(BudgetTrackerInsertDao.java:36) at com.jdbc.budgettracker.dao.BudgetTrackerInsertDao.insertIntoTable(BudgetTrackerInsertDao.java:47) at com.jdbc.budgettracker.controller.BudgetTrackerInsertController.doPost(BudgetTrackerInsertController.java:93) at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:831) Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.0.68:3306/budgettracker?allowPublicKeyRetrieval=true&useSSL=false at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228) at com.jdbc.budgettracker.dao.BudgetTrackerInsertDao.getConnection(BudgetTrackerInsertDao.java:33) ... 26 more
dburl設定
dburl=jdbc:mysql://192.168.0.68:3306/budgettracker?allowPublicKeyRetrieval=true&useSSL=false