Monday, September 17, 2018

Auto generate id



In here we can   generate auto id or number before data insert to database. That id     generate in order. we can attach string or symbol that id. using under code in java class you can display that.




Step 01-   #Create_database_connection


package autoid;
import java.awt.Component;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Ruwan
 */
public class DBconnect {
 
    public static Connection connect()
    {
        Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/login?","root","");
            Component rootPane = null;
          JOptionPane.showMessageDialog(rootPane, "Connect");
         
        } catch (Exception e) {

            JOptionPane.showConfirmDialog(null,e);
        }
       return con;
    }
 
 
}


Step 02- Write #auto_id generate_code




 private void autoId()
    {
        try {
             String sql="SELECT `id` FROM `AutoID` ORDER BY id DESC LIMIT 1";
       
            PreparedStatement  pst=conn.prepareStatement(sql);
           ResultSet rs=pst.executeQuery();
           if(rs.next())
           {
                String rnno=rs.getString("id");
                int co=rnno.length();
               String txt= rnno.substring(0, 2);
               String num=rnno.substring(2, co);
               int n=Integer.parseInt(num);
               n++;
               String snum=Integer.toString(n);
               String ftxt=txt+snum;
               txtautoid.setText(ftxt);
         
           }
           else
           {
               txtautoid.setText("MI1000");
           }
         
                         
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e);
        }
     
    }


You can watch full video tutorial and download project file for generate auto ID

Watch Video Tutorial>>
Watch How create database connection>>
Download Auto ID generate project source code>>


No comments:

Post a Comment