実現したいこと
eclipseでJDBCからログイン機能の実装をするにあたり
ID・PS等入力間違えていないが、エラーが発生。
これを解消し、ログイン成功できるようにしたい。
発生している問題・分からないこと
写真のようなフォルダ構造にしてサーバーにて実行してみるが、エラーが出てしまう。(1枚目:自分のフォルダ構造・2枚目:教科書の構造・エラー:3枚目)
エラーメッセージ
error
1
該当のソースコード
EmployeeController.java
1package controller; 2 3/** 4 * 社員情報管理コントローラー(メインサーブレット) 5 * 6 */ 7 8import java.io.IOException; 9 10import javax.servlet.RequestDispatcher; 11import javax.servlet.ServletContext; 12import javax.servlet.ServletException; 13import javax.servlet.http.HttpServlet; 14import javax.servlet.http.HttpServletRequest; 15import javax.servlet.http.HttpServletResponse; 16 17import bean.EmployeeBean; 18import service.EmployeeService; 19 20public class EmployeeController extends HttpServlet { 21 public void doPost(HttpServletRequest request, HttpServletResponse response) 22 throws ServletException, IOException { 23 24 try { 25 // 26 String id = request.getParameter("id"); 27 String password = request.getParameter("password"); 28 String name = request.getParameter("name"); 29 String comment = request.getParameter("comment"); 30 31 /* 32 * IDとPassWordと元に、結果をJSPに渡す処理 33 * ※ EmployeeBeanとEmployeeServiceをimportするのを忘れないでください。 34 */ 35 36 // 問② 37 EmployeeService employeeService = new EmployeeService(); 38 // 問③ 39 EmployeeBean employeeBean = employeeService.search(id, password, name, comment); 40 // 問④ 41 request.setAttribute("EmployeeBean", employeeBean); 42 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } finally { 46 ServletContext context = this.getServletContext(); 47 RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp"); 48 dispatcher.forward(request, response); 49 } 50 } 51}
EmployeeService.java
1package service; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.sql.Statement; 9import java.text.SimpleDateFormat; 10import java.util.Calendar; 11 12import bean.EmployeeBean; 13 14/** 15 * ・社員情報管理サービス 16 * 17 */ 18 19public class EmployeeService { 20 21 // 問① 接続情報を記述 22 /** ドライバーのクラス名 */ 23 private static final String POSTGRES_DRIVER = "org.postgresql.Driver"; 24 /** ・JDMC接続先情報 */ 25 private static final String JDBC_CONNECTION = "jdbc:postgresql://localhost:5432/lesson_db"; 26 /** ・ユーザー名 */ 27 private static final String USER = "postgres"; 28 /** ・パスワード */ 29 private static final String PASS = "postgres"; 30 /** ・タイムフォーマット */ 31 private static final String TIME_FORMAT = "yyyy/MM/dd hh:mm:ss"; 32 33 // 問② 入力された値を、INSERTする文 34 /** ・SQL INSERT文 */ 35 private static final String SQL_INSERT = "INSERT INTO employee_table (id, password, name, comment, login_time) VALUES (?, ?, ?, ?, ?);"; 36 37 // 問③ 入力されたIDをキーにして、検索するSELECT文 38 /** ・SQL SELECT文 */ 39 private static final String SQL_SELECT = "SELECT * FROM employee_table WHERE id = ?"; 40 41 EmployeeBean employeeDate = null; 42 43 // 送信されたIDとPassWordを元に社員情報を検索するためのメソッド 44 public EmployeeBean search(String id, String password, String name, String comment) { 45 46 Connection connection = null; 47 Statement statement = null; 48 ResultSet resultSet = null; 49 PreparedStatement preparedStatement = null; 50 51 try { 52 // データベースに接続 53 Class.forName(POSTGRES_DRIVER); 54 connection = DriverManager.getConnection(JDBC_CONNECTION, USER, PASS); 55 statement = connection.createStatement(); 56 57 // ログインタイムの生成 58 Calendar cal = Calendar.getInstance(); 59 SimpleDateFormat sdFormat = new SimpleDateFormat(TIME_FORMAT); 60 61 // preparedStatementで使用するため、String型に変換 62 String login_time = sdFormat.format(cal.getTime()); 63 64 /* 65 * 任意の値を登録できるように、プリペアドステートメントを記述。 66 */ 67 68 // preparedStatementにINSERT文をセット 69 preparedStatement = connection.prepareStatement(SQL_INSERT); 70 71 // 問④ 不足している値をセット 72 preparedStatement.setString(1, id); 73 preparedStatement.setString(2, password); 74 preparedStatement.setString(3, name); 75 preparedStatement.setString(4, comment); 76 preparedStatement.setString(5, login_time); 77 78 // 問⑤ preparedStatementを実行 79 preparedStatement.executeUpdate(); 80 /* 81 * 82 */ 83 // preparedStatementにSELECT文をセット 84 preparedStatement = connection.prepareStatement(SQL_SELECT); 85 86 // 問⑥ preparedStatementを使って、一番目のindexにIDをセット 87 preparedStatement.setString(1, id); 88 // SQLを実行。実行した結果をresultSetに格納。 89 resultSet = preparedStatement.executeQuery(); 90 91 while (resultSet.next()) { 92 // 問⑦ tmpName,tmpComment,tmpLoginTimeに適当な値 93 String tmpName = resultSet.getString("name"); 94 String tmpComment = resultSet.getString("comment"); 95 String tmpLoginTime = resultSet.getString("login_time"); 96 97 // 問⑧ 98 employeeDate = new EmployeeBean(); 99 employeeDate.setName(tmpName); 100 employeeDate.setComment(tmpComment); 101 employeeDate.setLogin_Time(tmpLoginTime); 102 } 103 // forName()で例外発生 104 } catch (ClassNotFoundException e) { 105 e.printStackTrace(); 106 // getConnection()、createStatement()、executeQuery()で例外発生 107 } catch (SQLException e) { 108 e.printStackTrace(); 109 } finally { 110 try { 111 if (resultSet != null) { 112 resultSet.close(); 113 } 114 if (statement != null) { 115 statement.close(); 116 } 117 if (connection != null) { 118 connection.close(); 119 } 120 } catch (SQLException e) { 121 e.printStackTrace(); 122 } 123 } 124 return employeeDate; 125 } 126} 127
index.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ page import="bean.EmployeeBean"%> 4 5<% 6 // 問① EmployeeController.javaから渡されたBean 7 EmployeeBean employeeBean = (EmployeeBean) request.getAttribute("EmployeeBean"); 8%> 9<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10<html> 11<head> 12<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13<title>検索結果</title> 14</head> 15<body> 16 <div align="center"> 17 <% 18 if (employeeBean != null) { 19 %> 20 <table border="1"> 21 <tr> 22 <th>社員名</th> 23 <td><%=employeeBean.getName()%></td> 24 </tr> 25 <tr> 26 <th>コメント</th> 27 <td><%=employeeBean.getComment()%></td> 28 </tr> 29 <tr> 30 <th>ログインタイム</th> 31 <td><%=employeeBean.getLogin_Time()%></td> 32 </tr> 33 </table> 34 <!-- 問② (エラーの場合)--> 35 <% } else { %> 36 エラー:登録内容に誤りがあります 37 <% } %> 38 </div> 39</body> 40</html>
index.html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="UTF-8"> 5 <title>社員情報システム</title> 6 <link rel="stylesheet" type="text/css" href="./css/index.css"> 7</head> 8<body> 9 <form action="/SC3-B/search" method="post"> 10 <div class="wrapper"> 11 <div class="header"> 12 <div class="inner">社員情報システム</div> 13 </div> 14 <ul> 15 <!-- ID入力欄 --> 16 <li class="id"> 17 <label for="id">ID</label> 18 <input type="text" name="id" value="" size="20"> 19 </li> 20 <!-- パスワード --> 21 <li class="password"> 22 <label for="password">PASS</label> 23 <input type="text" name="password" value="" size="20"> 24 </li> 25 <!-- 名前--> 26 <li class="name"> 27 <label for="name">名前</label> 28 <input type="text" name="name" value="" size="20"> 29 </li> 30 <!-- コメント --> 31 <li class="comment"> 32 <label for="comment">コメント</label> 33 <input type="text" name="comment" value="" size="20"> 34 </li> 35 <!-- 検索ボタン --> 36 <li> 37 <input id="button" type="submit" name="button" value="登録"> 38 </li> 39 </ul> 40 <div class="footer"> 41 <div class="inner">hogehoge.inc</div> 42 </div> 43 </div> 44 </form> 45</body> 46</html>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
前回も同じ質問をさせていただいてそちらと同じ結果(Javaのサーバーランタイム環境を間違えている)かと思いJava8に変えたり、Apache tomcat8.5にかえたりしているが同じエラーのままになる。
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2025/07/30 13:00 編集