how to generate auto increment number/id in java with source code

how to generate auto increment number/id in java with source code

This tutorial guide you to generate auto increment number/id on JFrame with Mysql database connection. I take this type ( RN1000 ) number as an example. This number consists of text and number. I go to display this number on JFrame with the auto-increment process. Note that the number not auto-increment in the database.

A video tutorial to generate auto increment number/id in java

Step 01 Create Java project

Step 02 Create Database

Step 03 Create a database connection

Connection class

package image;
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {   
     public static Connection connect()
     Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");                                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDatabaseNameHere?","DatabaseUserNameHere","DatabasePasswordHere");   
catch (Exception e) 
   return con;

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact

Add “MySQL.jdbc.Driver” to the project library.

If you need more information to connect Netbeans with Mysql database please watch this video

source code of generate auto increment number/id in java project

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();
            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);
           String snum=Integer.toString(n);
           String ftxt=txt+snum;


    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, e);


Check more projects>>

Java Tutorials