画面のtextboxからDB(Mysql)にinsertするときの質問です。 皆さんのおかげでinsertすることができました。 しかし、textboxが一個で値も一つなのに、 insertするとDBには2行同じ値がinsertされます。 下記のソースのどこがいけないのでしょうか? ご教示をお願いします。 「ソース」 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_table (name) values (?)"; stmt = conn.prepareStatement(sql); for (String n : name10) { stmt.setString(1, n); stmt.executeUpdate(); } int cnt = stmt.executeUpdate(); stmt.close(); RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); dispatcher.forward(request, response); }catch(Exception e){ } } }
↧