Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default How to Autogenerate of ID from database and show on JSP page?

    problem in web application : I have a database names tasks in SQL server 2005 having tasks id as primary key, I am using JSP and struts,SQL server 2005 for database connectivity,Now I can update my database but i want to autogenerate tasks id in numbers on JSP page when I click on Create_Task.jsp page.How can i do so?My project storing user entered task id,task name ,date etc properly in the database,but what and how should i make it (in taskAction.java) that it auto generate tasks_id in jsp page when i click on Create_task.jsp and other data user can enter himself.I am using Jdeveloper.Codes:
    create.jsp :-
    Java Code:
        <%@ page language="Java" %>  
        <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><%@ page contentType="text/html;charset=windows-1252"%>
        <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
        
        <html:form action ="taskAction.do">
          <head>
            <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
            <title>untitled</title>
          </head>
          <body>
            <form >
              <P>&nbsp;</P>
              <BLOCKQUOTE>
                <BLOCKQUOTE>
                  <BLOCKQUOTE>
                    <BLOCKQUOTE>
                      <P>
                        <SPAN style="background-color:rgb(204,204,204); background-color:rgb(204,204,204);"><SPAN style="background-color:rgb(255,255,255);"><FONT color="#006666"><STRONG><U><h1>
                          <SPAN style="background-color:rgb(255,255,255); background-color:rgb(204,204,204);">TASK MANAGER</SPAN>
                        </h1></U></STRONG>&nbsp;</FONT><SPAN style="background-color:rgb(204,255,255); background-color:rgb(204,204,255);"><FONT color="#003300">&nbsp;</FONT></SPAN> </SPAN></SPAN></P>
                    </BLOCKQUOTE>
                  </BLOCKQUOTE>
                </BLOCKQUOTE>
              </BLOCKQUOTE>
              <P>
                <SPAN style="background-color:rgb(204,255,255); background-color:rgb(204,204,255);"><FONT color="#003300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Task Id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <html:text property="taskid"/>
                  </FONT>
                </SPAN>
              </P>
              <P>
                <SPAN style="background-color:rgb(204,255,255); background-color:rgb(204,204,255);"><FONT color="#003300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Task Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <html:text property="taskname"/>
                  </FONT>
                </SPAN>
              </P>
              <P>
                <SPAN style="background-color:rgb(204,255,255); background-color:rgb(204,204,255);"><FONT color="#003300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <html:text property="dateOfBirth"/>
                  </FONT>
                </SPAN>
              </P>
              <P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              </P>
              <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="submit" value="Submit"/></P>
            </form>
          </body>
        </html:form>
    taskAction.java :-
    Java Code:
        //import TaskBO;
        import org.apache.struts.action.Action;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.apache.struts.action.*;
        
        
        
        public class taskAction extends Action 
        {
         public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                      HttpServletResponse response) 
          {
            ActionForward forward;
            taskform tForm = (taskform) form; 
            Taskdata taskdata = populateTaskdata(tForm);
            TaskBO taskBO = new TaskBO();
            if (taskBO.createtask(taskdata)) 
            {
              forward = mapping.findForward("success");
            }
            else 
            {
              forward = mapping.findForward("failure");
            }
            return forward;
          
          }
            private Taskdata populateTaskdata(taskform form) 
          {
            Taskdata td = new Taskdata();
            td.setTaskid(form.getTaskid());
             td.setTaskname(form.getTaskname());
              td.setDateOfBirth(form.getDateOfBirth());
        
            return td;
          }
        }
    taskform.java :-
    Java Code:
        import org.apache.struts.action.ActionForm;
        
        public class taskform extends ActionForm 
        {
        
          private int taskid;
          private String taskname;
          private String dateOfBirth;
          private String update;
          private String delete;
          
          public taskform()
          {
            taskid = 0;
            taskname = "";
            dateOfBirth = "";
          }
        
        
        
          public void setTaskid(int taskid)
          {
            this.taskid = taskid;
          }
          public int getTaskid()
          {
            return taskid;
          }
        
          
          public void setTaskname(String taskname)
          {
            this.taskname = taskname;
          }
          
            public String getTaskname()
          {
            return taskname;
          }
        
          public void setDateOfBirth(String dateOfBirth)
          {
            this.dateOfBirth = dateOfBirth;
          }
        
        
          public String getDateOfBirth()
          {
            return dateOfBirth;
          }
        
        
          public void setUpdate(String update)
          {
            this.update = update;
          }
        
        
          public String getUpdate()
          {
            return update;
          }
        
        
          public void setDelete(String delete)
          {
            this.delete = delete;
          }
        
        
          public String getDelete()
          {
            return delete;
          }
        }
    Last edited by gurpreet.singh; 03-25-2011 at 05:07 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Not too sure I understood that.
    SQL Server has something called (I think) ID as a column type, which is an auto-generated identifier.

  3. #3
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    Sorry sir m ot gettin u ....please explain or any useful link..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    It's IDENTITY.

  5. #5
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    thanx for the reply sir,but i need to show it on jsp page,I m getting max ID value and adding 1 to it and storing in data base using createAction.java.But its again and again redirecting me to Create.jsp without showing MAX Id in that.Can u please rectify the error.
    createAction.java
    Java Code:
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    
    public class createAction extends Action 
    {
    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
    ActionForward forward;
        taskform cform=(taskform) form;
        Connection connection = DatabaseManager.getConnection();
        Statement statement = connection.createStatement() ;
        ResultSet rs1 = statement.executeQuery("select MAX(tasks_id) from dbo.guru");
            if (rs1.next())
        {
    cform.setTaskid(rs1.getInt(1));
          forward=mapping.findForward("success");
           return forward;
        }
        else
            {
        
          forward = mapping.findForward("failure");
           return forward;
        }
        
    
    }
    }
    struts-confi.xml
    Java Code:
    <?xml version = '1.0' encoding = 'windows-1252'?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
    <struts-config>
    <form-beans>
    <form-bean name="LoginForm" type="LoginForm"/>
    <form-bean name="TaskForm" type ="taskform"/> 
    </form-beans>
    <action-mappings>
    <action path="/login" type="LoginAction"  name="LoginForm" scope="request" validate="true" input="login.jsp">
            <forward name="success" path="/success.jsp"/>
            <forward name="failure" path="/failure.jsp"/>
    </action>
    <action path="/taskAction" name="TaskForm" type="taskAction" scope="request">
      <forward name="success" path = "/TaskSubmitted.jsp" redirect ="true" />
      <forward name ="error" path ="/failure.jsp"/>
    </action>
    <action path="/createAction" name="TaskForm" type="createAction" scope="request">
      <forward name="success" path = "/create.jsp" redirect ="true" />
      <forward name ="error" path ="/failure.jsp"/>
    </action>
    <action path="/display" name="TaskForm" type="DisplayAction" scope="request">
      <forward name="success" path = "/display.jsp" />
      <forward name ="error" path ="/failure.jsp"/>
    </action>
    <action path="/enterAction" type="enterAction" name="TaskForm" scope="request">
            <forward name="success" path="/Update.jsp"/>
            <forward name="failure" path="/fail.jsp"/>
    </action>
    <action path="/updateAction" type="updateAction" name="TaskForm" scope="request">
            <forward name="success" path="/display.do" redirect="true"/>
            <forward name="failure" path="/failure.jsp"/>
    </action>
    
    </action-mappings>
      <message-resources parameter="view.ApplicationResources"/>
    </struts-config>
    menu.jsp
    Java Code:
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><%@ page contentType="text/html;charset=windows-1252"%>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>untitled</title>
      </head>
      <body bgcolor="#ccddcc" >
        <BLOCKQUOTE>
          <BLOCKQUOTE>
            <BLOCKQUOTE>
              <BLOCKQUOTE>
                <BLOCKQUOTE>
                  <BLOCKQUOTE>
                    <STRONG><SPAN style="background-color:rgb(153,204,203); background-color:rgb(255,255,255);"><FONT color="#000000"><h1>MAIN MENU</h1></FONT></SPAN></STRONG>
                    <SPAN style="background-color:rgb(255,255,255);"><FONT color="#000000"> </FONT></SPAN>
                    </BLOCKQUOTE>
                  <P>&nbsp;</P>
                </BLOCKQUOTE>
              </BLOCKQUOTE>
            </BLOCKQUOTE>
          </BLOCKQUOTE>
        </BLOCKQUOTE>
        <html:form action="createAction.do">
          <input type="submit" value="Create Task"/>
        </html:form>
        <P>&nbsp;</P>
        <form action="Enter.jsp">
          <input type="submit" value="Update"/>
        </form>
        <P>&nbsp;</P>
        <form action="display.do">
          <input type="submit" value="Display"/>
        </form>
        <P>&nbsp;</P>
      </body>
    </html>

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    No I can't rectify it because it's a rubbish idea.
    Why do you need an ID before you've actually created the entry in the database?
    You will end up with multiple ids that are the same.

    The ID should be generated when the row is inserted into the database, using an IDENTITY field.

  7. #7
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    How can u say its rubbish..
    Data is needed to be entered by the user so when he will press submit it will get stored in the database.
    if it is generated by adding 1 to the Tasks_id how it will have multiple same ids?And Id is primary key here So it cannot be same id for the purpose.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    OK, I'll explain.

    Under your system:
    Your table has entires in it.
    The current highest ID is 10.
    User A goes to the create page, which will show and id of 11 (SELECT max(ID) + 1).
    User B will go to the create page which will show an id of 11 (SELECT max(ID) + 1).
    User A saves...OK.
    User B saves...Primary key violation.

    Hence it's a bad solution.

    You only need an id when inserting the row...for which SQL Server has an IDENTITY option for your id field.

  9. #9
    Rituparna is offline Member
    Join Date
    Feb 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    hello gurpreet and Tolls,
    i was wondering if there is a way to autogenerate a PK in an SQL database.
    actually in my hotel booking page, i wanted the bookingID field get autogenerated at every new booking.
    is it possible to fetch the id before saving the booking details into the database?
    that is wen a booking is to be done, the jsp page will show the bookingID by fetching it from the database and adding 1 to it, it's possible or what?
    i mean is it possible to fetch the last entered id from the database? how?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Depends on the database.
    SQL Server has IDENTITY.
    Oracle has sequences.
    MySQL has auto-generated ids.

    And, as I said above, you only generate the id when you are inserting a new row. There is absolutely no need to have an id before then.

Similar Threads

  1. Download then show page from jsp
    By aflores in forum New To Java
    Replies: 4
    Last Post: 09-10-2010, 09:26 AM
  2. autogenerate the number
    By Shivraj in forum NetBeans
    Replies: 0
    Last Post: 03-17-2009, 04:57 PM
  3. Replies: 0
    Last Post: 06-07-2008, 09:02 PM
  4. show a value in javascipt in the jsp page
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-20-2008, 09:38 AM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •