I have a form in which I have made File no =field which displays(From sql database) the Integer value like 1-2-3-4 and so on.

I want that ,when I save a form which has file no 1 ,and then after when I want to save the next record it should remember the last file no and increment by 1.

Right now it's showing me the last number that is after saving 2 number it is showing me 1 number with this code.

int fileno1=Integer.parseInt(Result.getString(2));//getting the last file no        
filenotxtfield.setText(Integer.toString(fileno1 ++));//adding 1 to it and displaying it in the textfield.

Recommended Answers

All 10 Replies

I'm a bit lost here... what exactly do you want as output in your JTextField?

Open a form and enter some integer value in that field (filenotxtfield)let's say 1 ,and after I save the form to sql database I want 1+1=2 in that field, so that I don't have to keep track of the fileno every time I was about to save the information.The last file number I saved automatically shows up.

well, depending on your db of course, you don't want to automatically add 1.
a good practice to do is, when you run your method to add in your database, return the primary key (usually a numerical value). if you return null, you know the insertion of information in your db failed, otherwise, you set the value returned.

for instance:

public Long insertDataInDataBase(Data toAdd){
 // enter your code here
 return enteredData.getPrimaryKey();
}

Data newData;
Long check = insertDataInDataBase(newData);
if ( null == check)
  System.out.println("Error happened. Data not inserted.");
else
  myJTextField.setText(String.valueOf(check));

But the field fileno in the database is not primary key.

ehm ... so, you have a unique value, an incremental field, and you don't use it as a primary key ....
so ... what's the actual use of that field?

It Is just that I do not have to enter file number manualy.
Whenever I open it for saving the data it pick up the last file number adds 1 to it and display in the text field.

.... and you assume you'll enter primary keys manually?
what is this number used for?

It is not a primary key I will enter , and they are used for file no only nothing else,and I wanted to start by number 1 and so on,but not by the use of primary key.

so: you basically want a field that mimics the functionality of the primary key, but that doesn't serve any function at all? don't see why you would do that, but anyway..

what you can do, is run an sql statement to find the highest value of that id in your db.
add 1 to that value, and use
myField.setText(String.valueOf(alteredValue));

int fileno1=Integer.parseInt(Result.getString(2));      
        int increment = ++fileno1;  
        int increment2 = ++increment;       
        filenotxtfield.setText(Integer.toString(increment2));

This is the code that I have used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.