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.

Dani AI

Generated

Quick fix first: your textfield shows the old value because you are using the postfix operator. fileno1++ returns the current value and only then increments. Set the next value explicitly and do it once:

int last = Integer.parseInt(Result.getString(2));  // last stored file_no
int next = last + 1;                               // compute once
filenotxtfield.setText(String.valueOf(next));

Note for : the snippet in Post #11 increments twice, so it will skip numbers. One increment is enough.

Bigger picture (agreeing with ): computing the next number in the UI is risky if multiple users are inserting records. Two clients can see the same "next" value and collide. Safer options:

  • Let the database generate the number and read it back after the insert. Use your DBs identity/sequence feature (PostgreSQL/Oracle: sequences, SQL Server: IDENTITY, MySQL: AUTO_INCREMENT). The column can be UNIQUE even if it is not the primary key. After saving, set the textfield to the value you just inserted so the next form opens with that number.
  • If you cannot use an identity/sequence, create a small counter table and update it atomically when you open a new form (accepting gaps if the user cancels).

Example workflow with a DB-generated value:

  1. Insert without a file number (or with DEFAULT) and return the assigned value.
  2. Show that returned value in the UI, and use it to prefill the next record.

Pseudocode sketch:

PreparedStatement ps = con.prepareStatement(
    "INSERT INTO files(col1, col2) VALUES(?, ?)", 
    Statement.RETURN_GENERATED_KEYS);
ps.setString(1, ...);
ps.setString(2, ...);
ps.executeUpdate();

try (ResultSet keys = ps.getGeneratedKeys()) {
    if (keys.next()) {
        int assigned = keys.getInt(1); // file_no if generated by DB
        filenotxtfield.setText(String.valueOf(assigned + 1)); // prefill next
    }
}

Whichever route you choose, add a UNIQUE index on the file number to prevent duplicates.

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.