Click here to Skip to main content
15,566,160 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
void notrans(){
// this is my function :
try{
    
    String sql="select right(No_Trans,2) as no_akhir from jual";
    Statement stat=conn.createStatement();
    ResultSet res=stat.executeQuery(sql);
    if(res.first()==false){
        txttrans.setText("TRX0001");
    }else{ 
        res.last(); 
        int no=res.getInt(1)+1;
        String cno=String.valueOf(no);
        int pjg_cno=cno.length();
        for(int i=0;i<2-pjg_cno;i++){
           cno="00"+cno;
    }
    txttrans.setText("TRX00"+cno);
}
}catch(Exception e ){
}
}
Posted
Updated 24-Jul-15 20:53pm
v2
Comments
Afzaal Ahmad Zeeshan 25-Jul-15 1:54am    
That is a feature of SQL databases, why do want to implement it in Java? I suggest that you try to create your field as Primary and Identity so that SQL database does this itself, instead of creating a function yourself.

1 solution

Like Afzaal said, this can be easily set up in your Database system (whatever it may be). It is a property of a column called 'Auto-increment' or sometimes AI, as in MySQL. You simply click the AI (or auto-increment) checkbox and voilà, your column will now be automatically incrementing it's ID.
 
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