Click here to Skip to main content
14,975,098 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
cannot auto increment id in above code

{
           String sql = "insert into artist_reg_ml (id,uname,pwd,email,country) values (?,?,?,?,?)";
           PreparedStatement pst = conn.prepareStatement(sql);
           pst.setString(1, "5");
           pst.setString(2, uname);
           pst.setString(3, pwd);
           pst.setString(4, email);
           pst.setString(5, country);

           int i = pst.executeUpdate();
           String msg = "";
           if (i != 0) {
               //msg = "Record has been inserted";
               //pw.print("<font size='6' color=blue>" + msg + "</font>");
               HttpSession sess = request.getSession();
               sess.setAttribute("uname", request.getParameter("uname"));
               sess.setAttribute("pwd", request.getParameter("cpwd"));
               RequestDispatcher dispatcher = request.getRequestDispatcher("/mypage1.jsp");
               dispatcher.forward(request, response);
               File dir = new File("C:/Users/Naresh/Documents/NetBeansProjects/Registrationform/web/nameoffolder/" + uname);
               dir.mkdir();

           }
Posted
Updated 9-Aug-18 8:05am
Comments
Richard MacCutchan 21-Oct-15 11:56am
   
Why are you storing a fixed character string into the id field?
Member 9671810 21-Oct-15 12:10pm
   
i want that field to auto increment i tried various ways. eg select max(id) from table; or even tried putting null value. But it is not allowing me do so.

   
Comments
Member 9671810 21-Oct-15 12:16pm
   
thanks sir, but i have referred this :) but it didn't helped me :/
Richard MacCutchan 21-Oct-15 12:22pm
   
What do you mean it didn't help you; it explains what you need to do.
Member 9671810 21-Oct-15 12:36pm
   
yes, it does. i tried this one i cannot store the incremented value.
Richard MacCutchan 21-Oct-15 13:03pm
   
That does not tell us anything. Please edit your question and show exactly what you are doing and explain what errors or other problems you have.
Member 9671810 21-Oct-15 21:55pm
   
if i use this pst.setString(1, null); then it gives me error of "Column 'id' cannot be null" cannot be inserted in the table. i use this cause when insert query is fired the id field will the store the incremented value.
Richard MacCutchan 22-Oct-15 3:09am
   
You should not be trying to set the id field. It is an auto-increment column managed by the database engine.
try this.. it works

{
            PreparedStatement pst1 = conn.prepareStatement("select max(id)+1 from artist_reg_ml");
            ResultSet rs = pst1.executeQuery();
            String user_id ="" ;
            while(rs.next())
            {
                user_id = rs.getString(1);
            }
            String sql = "insert into artist_reg_ml (id,uname,pwd,email,country) values (?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, user_id.toString());
            pst.setString(2, uname);
            pst.setString(3, pwd);
            pst.setString(4, email);
            pst.setString(5, country);
            int i = pst.executeUpdate();
            
            String msg = "";
            if (i != 0) {
           
                HttpSession sess = request.getSession();
                sess.setAttribute("uname", request.getParameter("uname"));
                sess.setAttribute("pwd", request.getParameter("cpwd"));
                File dir = new File("C:/Users/Naresh/Documents/NetBeansProjects/Registrationform/web/nameoffolder/" + uname);
                dir.mkdir();
                RequestDispatcher dispatcher = request.getRequestDispatcher("/mypage1.jsp");
                dispatcher.forward(request, response);
             

            } 
   
Comments
Member 14225850 11-Apr-19 6:42am
   
Thanks .....This worked for ....very very thnaks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900