Getting the last inserted id using jsp

Getting the last inserted id using jsp

Pls how can i retrieve the last inserted id after submitting a form to mysql using jsp

View Answers

November 17, 2011 at 3:26 PM

1)form.jsp:

<html>
<form method="post" action="insert.jsp">
<table>
<tr><td>Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

2)insert.jsp:

<%@page import="java.sql.*,java.util.*"%>
<%
String name=request.getParameter("name");
String address=request.getParameter("address");
        try{
         Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into data(name,address) values('"+name+"','"+address+"')");
           out.println("Data is successfully inserted!");
           int id=0;
           ResultSet rs=st.executeQuery("select * from data");
           if(rs.last()){
               id=rs.getInt("id");
           }
           out.println("Last inserted Id: "+id);


        }
        catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        }
        %>

Ads









Related Tutorials/Questions & Answers:
Getting the last inserted id using jsp
Getting the last inserted id using jsp  Pls how can i retrieve the last inserted id after submitting a form to mysql using jsp
JDBC: Last Inserted Id Example
JDBC: Last Inserted Id Example In this tutorial, you will learn how to find last inserted record ID using JDBC API. Getting Last Inserted ID : Last... insertion.  you can get the last inserted id by using MAX(), writing
Advertisements
value is inserted into the sql table through jsp-jdbc but not getting stored into the data base,only row is increasing.
value is inserted into the sql table through jsp-jdbc but not getting stored into the data base,only row is increasing.  <html> <..."> <title>JSP Page</title> <style type
ID using get the databse latest value and displayed in jsp
ID using get the databse latest value and displayed in jsp  function... want to get the latest value using id and displayed in that start date end date etc,... How to get latest values in my jsp page and id will be shown in url
ID using get the databse latest value and displayed in jsp
ID using get the databse latest value and displayed in jsp   I have... in the form (it means latest values) ... how to retrive that values using ID... in preview jsp page. the id will shown in end of the url also
To Display the 'Last Modified' message in JSP
To Display the 'Last Modified' message in JSP  I have to display the "Last Modified" auto save message in a portlet page at the right end corner using JSP I have used this below coding but I'm not getting the solution can anyone
Retrieving newly inserted records and displaying in jsp forever
Retrieving newly inserted records and displaying in jsp forever  Sir... a table and display in jsp(Each row contains a field called as "session... of every record which is displayed on the jsp (not in database) and immediately i have
How to store data entered in JSP page by a user in XML file & later retrieval of data using id at other page
How to store data entered in JSP page by a user in XML file & later retrieval of data using id at other page  I have made a form in JSP having emp id... in another jsp page using retrieve submit button.Please tell step by step instructions
dynamically created links id from mysql table is not getting
only getting the last recordset from table malayalamcinema, plz help me, its...dynamically created links id from mysql table is not getting  guys... to another page, but while i'm trying to access its value, i'm getting only
the last data entered into database is getting stored again after refreshing
the last data entered into database is getting stored again after refreshing  hey all i made a shout box using php and mysql but the last data... if the data is deleted from the database the last data entered is getting posted plz help
Getting an exception - JSP-Servlet
Getting an exception  Dear Sir , While sending a mail am getting an following exception javax.mail.MessagingException: 554 mail server permanently rejected message (#5. 3.0)so how to over come from this please help
delete row using id
delete row using id  package pkg2; import org.hibernate.Query; import... = "delete from Insurance insurance where id = 2"; Query query = sess.createQuery...()); } } } this is the code m using . Error is -query must begin with SELECT or FROM
Getting a Property value in jsp
Getting a Property value in jsp   ... a Property Value</H1> <jsp:useBean id="bean" class="form.GetProperties" />... GetProperties() {} } In the above example, we are using bean with <jsp
Printing Session Id Using Variable
Printing Session Id Using Variable  Hi, I am a learner in PHP language. Could any one guide me, how to print session Id using variable in PHP. Thanks
MySQL Generate AlphaNumberic Id In JSP
numeric id using MySQL as database, tomcat 7 as a server for deploying the JSP...MySQL Generate AlphaNumberic Id In JSP In this section we will discus about how to generate id in specific format in MySQL and JSP. This example explains
Is JSF using JSP?
Is JSF using JSP?  Is JSF using JSP
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Jsp using mvc - JSP-Servlet
Jsp using mvc  hi I have written using mvc2 architecture, i have written logic in java ,i want to print a message on the form stating the the values are inserted successfully.So can you tell me how i have to write the code
tree using jsp code
tree using jsp code  i want to draw a tree structure of a family hierarchy using jsp code
how to create using jsp
how to create using jsp  code 1: <%@ page language="java..."/><br/> <input type="text" name="fname" id=""/><br/> <input type="text" name="lname" id=""/><br/> <input type
i need the source code to generate id in jsp
i need the source code to generate id in jsp  hai,i need the source code for generate id...which is i need to generate the new id from the maximum... of generate ids each should have some range...plz give the source code in jsp with ms
How to identify the radio button id and row id in a table in jsp?
How to identify the radio button id and row id in a table in jsp?   function generateid(no) {var table...; table.rows[rowCount-1].cell[no].id="r"+rowCount+"c
A Java Program by using JSP
A Java Program by using JSP  how to draw lines by using JSP plz show me the solution by using program
[JSP] How to pass back ID to previous page.
[JSP] How to pass back ID to previous page.  Hi experts, May I ask... a UKM, it linked to ViewProducts.jsp <a href='lihatProduk.jsp?ID_UKM=" + rs.getString("ID_UKM") + "'title='Lihat Produk' class='icon-5 info-tooltip'<
generate charts using JSP
generate charts using JSP  any one know coding for generate bar chart or pie chart using JSP
ScatterPlot using jsp
ScatterPlot using jsp  hi, can anybody provide me code for ScatterPlot using jsp. thanks
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop down list to select book from database. i'm able to retrieve dropdown list from... is inserted successfully"); %>   thanx for the quick response. i tried just
BMI by using JSP Languange
BMI by using JSP Languange  Using JSP language, create a system that calculates Body Mass Index (BMI) of users who entered their weight and height. Then, system will display all the records. Can you provide me the coding

Ads