G.S
G.S

Reputation: 21

Generate auto increment serial number in java eclipse

I designed one form in java where textfield is there.so I want to display serial numbers in this textfield...means current textfield value should be 1..then I'll fill up these form and submitted...when I'll come back textfield value should be 2...after submitting this form textfield value become 3 and so on...So how to make auto increment serial number and display in JTextfield..

Here I used java eclipse and sqlite database. I tried following code...

textField = new JTextField();
try {
    String query = "Select max(`Enquiry No`) as max from enquiry ";
    PreparedStatement pst = conn.prepareStatement(query);
    ResultSet rs = pst.executeQuery();

    while (rs.next()) {
        int num = rs.getInt(1);
        int inc = num + 1;
        textField.setText(valueOf(inc));
    }
} catch (Exception f) {
    f.printStackTrace();
}

please give solution for it

Upvotes: 1

Views: 8018

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60045

The idea simple after the insertion you should to call your method, i suggest something like this :

Insert function, you should to call it when you submit, after submit you had to call the second method

insertion_function(){
   //insert into your database
   //call your method which get max and show you the result
   set_max_function();
}

set_max_function() {
    textField = new JTextField();
    try {
        String query = "Select max(`Enquiry No`) as max from enquiry ";
        PreparedStatement pst = conn.prepareStatement(query);
        ResultSet rs = pst.executeQuery();

        while (rs.next()) {
            int num = rs.getInt(1);
            int inc = num + 1;
            textField.setText(valueOf(inc));
        }
    } catch (Exception f) {
        f.printStackTrace();
    }
}

Hope you understand the idea.

Upvotes: 1

Related Questions