5 Replies Latest reply: Aug 22, 2007 11:15 AM by DrClap RSS

    Database insertion problem

    807600
      hi..
      I have one multiple select box in my JSP page. I want to insert values to database from this select box, but its not inserting anything. Iam using My SQL 4.
      Here iam listing my code....


      String s1;
                String driver = "org.gjt.mm.mysql.Driver";
                Connection con=null;
                PreparedStatement pstmt=null;
                String sql="INSERT INTO my_det(name,person) VALUES(?,?)";
                s1=request.getParameter("a");
                String[] s2=request.getParameterValues("n");
           try
           {
                Class.forName(driver).newInstance();
                String url="jdbc:mysql://localhost:3306/mydb";
                con=DriverManager.getConnection(url,"vin","vin");
                pstmt=con.prepareStatement(sql);
                pstmt.setString(1, s1);
                for (int i = 0; i < s2.length; i++)
                {
                pstmt.setString(2, s2);
                }
           pstmt.executeUpdate();


      Please help me
      Thanks
        • 1. Re: Database insertion problem
          796447
          1. What are you expecting by putting this in a loop?
          pstmt.setString(2, s2[ i ]); You'll have to execute an insert for each item, it won't work by magic like this.
          2. Do some simple debugging. Make your code tell you if it's even getting to that code at all.
          3. Where's your catch statement? Are you completely ignoring a possible exception?
          • 2. Re: Database insertion problem
            807600
            Thanks for ur reply

            Here im giving the complete code

            String s1;
            String driver = "org.gjt.mm.mysql.Driver";
            Connection con=null;
            PreparedStatement pstmt=null;
            String sql="INSERT INTO my_det(name,person) VALUES(?,?)";
            s1=request.getParameter("a");
            String[] s2=request.getParameterValues("n");
            try
            {
            Class.forName(driver).newInstance();
            String url="jdbc:mysql://localhost:3306/mydb";
            con=DriverManager.getConnection(url,"vin","vin");
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1, s1);
            for (int i = 0; i < s2.length; i++)
            {
            pstmt.setString(2, s2);
            }
            pstmt.executeUpdate();
            }
            catch(Exception e){
            out.println("Exception.");
            e.printStackTrace();
            }
            finally{
            pstmt.close();
            con.close();
            }
            %>

            Message was edited by:
            vinikz
            • 3. Re: Database insertion problem
              796447
              One question (#3) of mine answered, only two more to go...
              • 4. Re: Database insertion problem
                807600
                I tried this also pstmt.setString(2, s2[ i ]); but its not working
                • 5. Re: Database insertion problem
                  DrClap
                  If "not working" means it only writes one record, then yes, that's what your program does. If it means something else, then what?