一覧画面(employeelist.jsp)でDBからテーブルを表示させ、
登録ボタンで登録画面(entry.jsp)に遷移して情報を入力して、登録した情報を
一覧画面に表示させたいのですが、
下記のように記述し、登録画面で情報を入力して登録ボタンを押した際に、下記のようなエラーがでてしまいました。対処のしかたがわからず困っています。
よろしくお願いします。
【エラー】
HTTPステータス 500 - java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
【employeelist.jsp】
<h2>社員一覧</h2> <table border="1"> <tr bgcolor="#afeeee"> <th></th> <th>社員コード</th> <th>氏名</th> <th>氏名カナ</th> <th>性別</th> <th>役職名</th> <th>所属部署名</th> <th>在職情報</th> <form action="UpdateServlet" id="form" name="form"> <c:forEach var="item" items="${list}" varStatus="num"> <tr> <td><input type="radio" name="radio" value="${num.index}"></td> <td>${item.s_id}</td> <td>${item.name }</td> <td>${item.name_kana }</td> <td>${item.sex }</td> <td>${item.clas }</td> <td>${item.depart_name }</td> <td>${item.retired }</td> </tr> </c:forEach> </table> <input type="submit" value="削除" onclick="goServlet();"> <!-- header.jsp --> <input type="button" value="登録" onclick="location.href='DepartServlet'"> <input type="button" value="更新" > <input type="button" value="戻る" onclick="location.href='kintaikanrisystem.jsp'"> </form> </body> </html>
【DepartServlet】
package kadai; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Depart */ public class DepartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DepartServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sql="select depart_id,depart_name from depart"; Connection con =null; Statement smt=null; try{ con=DBManager.getConnection(); smt=con.createStatement(); ResultSet rs =smt.executeQuery(sql); List<Tableclass> list1=new ArrayList<Tableclass>(); while(rs.next()){ Tableclass tableclass=new Tableclass(); tableclass.setDepart_id(rs.getString("depart_id")); tableclass.setDepart_name(rs.getString("depart_name")); list1.add(tableclass); } request.setAttribute("list1",list1); }catch(SQLException e){ throw new ServletException(e); }finally{ if(smt != null){ try{smt.close();}catch(SQLException ignore){} }if(smt != null){ try{con.close();}catch(SQLException ignore){} } } request.getRequestDispatcher("/entry.jsp").forward(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
【entry.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>社員登録</h2> <form action="EntryServlet"> <table border="1"> <!-- ------------------------------------------------------------- --> <tr bgcolor="#afeeee"> <td>社員コード</td> <td><input type="text" name="new_id" maxlength='7' style="ime-mode:disabled"></td> </tr> <!-- ------------------------------------------------------------- --> <tr bgcolor="#afeeee"> <td>氏名(姓)</td> <td><input type="text" name="new_name1" maxlength='20'></td> <td>氏名(名)</td> <td><input type="text" name="new_name2" maxlength='20'></td> </tr> <!-- ------------------------------------------------------------- --> <tr bgcolor="#afeeee"> <td>氏名カナ(姓)</td> <td><input type="text" name="new_kananame1"" maxlength='100'></td> <td>氏名カナ(名)</td> <td><input type="text" name="new_kananame2" maxlength='100'></td> </tr> <!-- ------------------------------------------------------------- --> <tr > <td bgcolor="#afeeee">性別</td> <td><input type="radio" name="sex" value="1" id="man" checked><label for="man">男</label></td> <td><input type="radio" name="sex" value="2" id="woman"><label for="woman">女</label></td> <td><input type="radio" name="sex" value="3" id="other"><label for="other">その他</label></td> </tr> <!-- ------------------------------------------------------------- --> <tr> <td bgcolor="#afeeee">役職名</td> <td><select name="new_class"> <option value="部長">部長 <option value="課長">課長 <option value="主任">主任 <option value="担当">担当 <option value="アシスタント">アシスタント </select></td> </tr> <!-- ------------------------------------------------------------- --> <tr> <td bgcolor="#afeeee">所属部署名</td> <td><select name="new_depart_name"> <c:forEach var="item" items="${list1}"> <option value="${item.depart_id}">${item.depart_name} </c:forEach> </select></td> </tr> <!-- ------------------------------------------------------------- --> </table> <input type="submit" name="entry" value="登録" > <input type="button" name="back" value="戻る" onclick="location.href='KintaikanrisystemServlet'"> </form> </body> </html>
【EntryServlet】
package kadai; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class EntryServlet */ public class EntryServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EntryServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html:charset=UTF-8"); Connection con =null; PreparedStatement smt=null; String message1=null; String message2=null; int a=0; int b=0; try{ con=DBManager.getConnection(); String id=request.getParameter("new_id"); String lname=request.getParameter("new_name1"); String fname=request.getParameter("new_name2"); String lname_kana=request.getParameter("new_kananame1"); String fname_kana=request.getParameter("new_kananame2"); String sex1=request.getParameter("sex"); int sex=Integer.parseInt(sex1); String entry_class=request.getParameter("new_class"); String depart_id=request.getParameter("new_depart_name"); String test=null; Calendar cd=new GregorianCalendar(); SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); String sd1=sd.format(cd.getTime()); int retired=0; if(id.length()<7){ message1="社員コードを半角で7桁入力してください。"; a=1; request.setAttribute("message",message1); } if(lname==null || fname==null || lname_kana==null || fname_kana==null || lname=="" || fname=="" || lname_kana=="" || fname_kana=="") { message2="氏名・氏名カナを入力してください。"; b=1; request.setAttribute("message2",message2); } if(a==0 && b==0){ String sql="insert into employee values(1, 2, 3 ,4 ,5 ,6 ,7 ,8 ,9,10,11)"; smt=con.prepareStatement(sql); smt.setString(1, id); smt.setString(2, lname); smt.setString(3, fname); smt.setString(4, lname_kana); smt.setString(5, fname_kana); smt.setInt(6, sex); smt.setString(7, entry_class); smt.setString(8, depart_id); smt.setString(9, test); smt.setString(10, sd1); smt.setInt(11, retired); int in =smt.executeUpdate(); } }catch(SQLException e){ throw new ServletException(e); }finally{ if(smt != null){ try{smt.close();}catch(SQLException ignore){} }if(smt != null){ try{con.close();}catch(SQLException ignore){} } }if(a==0 && b==0){request.getRequestDispatcher("KintaikanrisystemServlet").forward(request,response);} else if(a==1 || b==1){request.getRequestDispatcher("/entry.jsp").forward(request,response);} } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/07/27 06:01