Click here to Skip to main content
15,886,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
 try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection("jdbc:sqlserver://;databaseName=Rental;user=MyAdmin;password=123456789");
            
            Statement s = con.createStatement();
            
            ResultSet rs = s.executeQuery("SELECT Car_No FROM CarRegistration");
            rs.next();
            rs.getString("Car_No");
            
            if(  rs.getString("Car_No") ==null)
            {
                txtregno.setText("C0001");
            }else
            {
                long id = Long.parseLong(rs.getString("Car_no").substring(2,rs.getString("Car_no").length()));
                
                id++;
                
                  txtregno.setText("C0" + String.format("%03d", id));
            }
            
            
            
            
          } catch (ClassNotFoundException ex) {
            Logger.getLogger(CarRegistration.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CarRegistration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

What I have tried:

I want to make Auto increment in "C0001" but it gives me a error No  current row
Posted
Updated 20-Jan-20 0:19am

Quote:
the result set has no current row.

May be you should check the number of records in result.

Advice: Never calculate a next id by your code because one day or another, 2 users will create a new id at same time anf you will end up with 2 records with same id.
Let the server handle it for you.
SQL AUTO INCREMENT a Field[^]
 
Share this answer
 
Comments
Richard Deeming 21-Jan-20 10:15am    
There's no need for two users in this case. With no ORDER BY clause in the query, and no loop on the results, the OP is almost certainly always selecting the first car number. Every subsequent record with get the PK "C0002". :)
Patrice T 21-Jan-20 10:23am    
Agreed
Java
ResultSet rs = s.executeQuery("SELECT Car_No FROM CarRegistration");
rs.next();

You are assuming that the resultset contains some data. You need to check first whether any rows are present.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900