Example : How to auto increment serial number taking last value of database record into a text box of a web page [Auto Increment Serial Number].
<%-- 
    Document   : AutoIncrementSerialNo
    Created on : 3 Jun, 2019, 7:37:45 AM
    Author     : Codershelpline
--%>
<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" 
   import="java.util.*" errorPage=""%>
<% 
    Connection conn = null;
    PreparedStatement pst=null;
    ResultSet rs;
    Statement st=null;
     
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
      "oracleusername","oraclepassword");
   out.println("Database Connected... ");

    String sl="";
        
        pst=conn.prepareStatement("select slno from userreg");
        rs=pst.executeQuery();
        while(rs.next())
        {
            sl=rs.getString("slno");
        }
        if(sl==null|| sl=="")
        {
            sl="0";
        }
        int sl2= Integer.parseInt(sl);
        String sl3= String.valueOf(sl2+1);        
        //out.println(sl3);
 %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>AutoSerialNo.</title>
    </head>
    <body>
        Sl. No.:
        <input type="text" name="UrTxtSlno" id="UrTxtSlno1" value="<%=sl3%>">                        
    </body>
</html>
<%-- 
    Document   : AutoIncrementSerialNo
    Created on : 3 Jun, 2019, 7:37:45 AM
    Author     : Codershelpline
--%>
<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" 
   import="java.util.*" errorPage=""%>
<% 
    Connection conn = null;
    PreparedStatement pst=null;
    ResultSet rs;
    Statement st=null;
     
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
      "oracleusername","oraclepassword");
   out.println("Database Connected... ");

    String srlno="";
    int srlno1=0;
    int srlno5=0;

    pst=conn.prepareStatement("select slno from userreg");
    rs=pst.executeQuery();
    while(rs.next())
    {
        srlno=rs.getString("slno");//slno is DataBase table
        
         if(srlno==null||srlno=="")
            {
            srlno="0";
            }
            srlno5=Integer.parseInt(srlno);
            if(srlno1<srlno5)
            {
             srlno1=srlno5;
            }
    }   
    String srlno3=String.valueOf(srlno1+1);
    //out.println(srlno3);
 %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>AutoSerialNo.</title>
    </head>
    <body>
        Sl. No.:
        <input type="text" name="UrTxtSlno" id="UrTxtSlno1" value="<%=srlno3%>">                        
    </body>
</html>

Loading

Categories: JSP

1 Comment

Gaurav Kumar · February 15, 2020 at 5:13 PM

if I delete a data from middle of the list then it will not full fill the list with the data… It only take last value and auto increment…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.