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