Results 1 to 10 of 10
- 03-25-2011, 05:02 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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 :-
taskAction.java :-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> </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> </FONT><SPAN style="background-color:rgb(204,255,255); background-color:rgb(204,204,255);"><FONT color="#003300"> </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"> Task Id <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"> Task Name <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"> Date <html:text property="dateOfBirth"/> </FONT> </SPAN> </P> <P> </P> <P> <input type="submit" value="Submit"/></P> </form> </body> </html:form>
taskform.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; } }
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.
- 03-25-2011, 09:15 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Not too sure I understood that.
SQL Server has something called (I think) ID as a column type, which is an auto-generated identifier.
- 03-25-2011, 09:57 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
Sorry sir m ot gettin u ....please explain or any useful link..
- 03-25-2011, 10:23 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It's IDENTITY.
- 03-25-2011, 11:11 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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
struts-confi.xmlJava 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; } } }
menu.jspJava 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>
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> </P> </BLOCKQUOTE> </BLOCKQUOTE> </BLOCKQUOTE> </BLOCKQUOTE> </BLOCKQUOTE> <html:form action="createAction.do"> <input type="submit" value="Create Task"/> </html:form> <P> </P> <form action="Enter.jsp"> <input type="submit" value="Update"/> </form> <P> </P> <form action="display.do"> <input type="submit" value="Display"/> </form> <P> </P> </body> </html>
- 03-25-2011, 11:35 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-25-2011, 11:45 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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.
- 03-25-2011, 12:35 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-25-2011, 06:17 PM #9
Member
- Join Date
- Feb 2011
- Location
- India
- Posts
- 22
- Rep Power
- 0
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?
- 03-28-2011, 09:28 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Download then show page from jsp
By aflores in forum New To JavaReplies: 4Last Post: 09-10-2010, 09:26 AM -
autogenerate the number
By Shivraj in forum NetBeansReplies: 0Last Post: 03-17-2009, 04:57 PM -
Not direct to localhost:8080/test/index.do and show This is my JSP page?
By techissue2008 in forum Web FrameworksReplies: 0Last Post: 06-07-2008, 09:02 PM -
show a value in javascipt in the jsp page
By hussainzim in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-20-2008, 09:38 AM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM


LinkBack URL
About LinkBacks

Bookmarks