JSP code help needed ...

Posted on 2011-10-18
Last Modified: 2012-05-12

I have the following JSP code.  This is for JSP with MySQL. But i want the code in JSP with ORACLE. I have changed ALL the code EXCEPT the following line:

long order_id = (( stmt).getLastInsertID();

When we are inserting a record into "Orders" table, we are not passing this value (order_id). This is generated by SEQUENCE in ORACLE and a TRIGGER on "Orders" table is used to insert the SEQUENCE value into "Orders" table. Now i want to get the newly inserted value of (order_id) in "Orders" table. The below statement will work JSP with MySQL but not JSP with ORACLE.

long order_id = (( stmt).getLastInsertID();

Can you please provide the code in JSP with ORACLE ?

JSP Code:



               java.sql.Connection cn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/shop");

               String query = "INSERT INTO orders VALUES ('', ?, ?, ?, ?, ?, ?, ?, ?)";
               java.sql.PreparedStatement stmt = cn.prepareStatement(query);

               stmt.setString(1, request.getParameter("firstname"));
               stmt.setString(2, request.getParameter("surname"));
               stmt.setString(3, request.getParameter("address"));
               stmt.setString(4, request.getParameter("city"));
               stmt.setString(5, request.getParameter("postcode"));
               stmt.setString(6, request.getParameter("card_number"));
               stmt.setString(7, request.getParameter("card_type"));
               stmt.setString(8, request.getParameter("totalvalue"));


               long order_id = (( stmt).getLastInsertID();

               String order_info_query = "INSERT INTO order_info VALUES ('', ?, ?, ?)";

               Enumeration products = basket.getProducts();

               while (products.hasMoreElements())
                    Product product = (Product) products.nextElement();

                    stmt = cn.prepareStatement(order_info_query);

                    stmt.setLong(1, order_id);
                    stmt.setInt (2, Integer.parseInt(product.getId()));
                    stmt.setInt (3, product.getQuantity());





Question by:milani_lucie
    LVL 10

    Accepted Solution

    LVL 10

    Expert Comment

    by:Pramod Kumar
    You may add below method to get the last id. This will work with all kinds of Database:

    and call this method as:
    long order_id = getLastInsertID();

    public long getLastInsertID(Connection cn) throws Exception
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("select max(order_id) from orders");
            return rs.getLong(1);

    Open in new window

    LVL 10

    Assisted Solution

    by:Pramod Kumar
    or if you don't want to add method in JSP, the only the code can be used

    - - - - -
    - - - - -
    Statement st = cn.createStatement();
    ResultSet rs = st.executeQuery("select max(order_id) from orders");
    long order_id = rs.getLong(1);
    - - - - -
    - - - - -

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    After several days of searching and hunting for limited documentation, I wanted to share this guide to hopefully save someone the hassle of trying to figure this out on their own. I have tested this on Xendesktop 7.1 and PS 4.5 running simultaneous…
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now