JSP サーブレット mysqlについて質問です。 私が今実現させたいことを簡単に説明します。 画面側にはtextboxがあり、このtextboxは追加ボタンによって、 どんどん追加されていきます。 サーブレット側はこのtextboxの全ての値をDBに書き込みたいです。 例えば、textboxが二つなら、一つ目のINSERTでDBのagi列にagis1とname列にtextbox一個目の値を書き込み、二つ目のINSERTでDBのagi列にagis2とname列にtextboxニ個目の値を書き込む。 ソースを書いてみたのですが、いろいろと違いがあると思いますので、ご教示をお願いします。 なお、name[i]はtextboxの値なので、このtextboxがあるだけINSERTしたいです、そこのソースの書き方も教えていただきたいです。 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] name = request.getParameterValues("sonota"); request.setCharacterEncoding("utf-8"); Connection conn = null; CallableStatement cs = null; PreparedStatement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://localhost/Sampl_db"; String dbUser = "root"; String dbPass = "taratara"; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, dbUser, dbPass); int ages = 0; for (int i = 0; i < 10; i++){ ages += 1; System.out.println(ages); String sql="insert into Sampl_teble (age, name) values (" + ages + ", " + name[i] + ")"; //name[i]があるだけINSERTするように書きたいです。 System.out.println(name[i]); stmt = conn.prepareStatement(sql); int cnt = stmt.executeUpdate(); } stmt.close(); }catch(Exception e){ //exception RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); dispatcher.forward(request, response); } } }
↧