How to make Integer number increment by 1 in a JTextField?
-
I have a form in which I have made File no field which hold 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. I have not coded yet anything because I don't know if it is possible?
-
I have a form in which I have made File no field which hold 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. I have not coded yet anything because I don't know if it is possible?
chdboy wrote:
I don't know if it is possible?
It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.
Veni, vidi, abiit domum
-
chdboy wrote:
I don't know if it is possible?
It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.
Veni, vidi, abiit domum
thanks for the Idea I'm working on it. EDIT: I managed to do what I wanted BUT... It only happens till the application is open and I'm saving data one after one...as soon as I close to application and opens in again I have see the last file no and enter the next file no manually. with this code
String filenostr = filenotxtfield.getText();
statement.setString(1,filenostr);
int fileno=Integer.parseInt(filenostr);fileno++;
String convertno=String.valueOf(fileno); filenotxtfield.setText(convertno); statement.executeUpdate(); int rowsAffected = statement.executeUpdate(); if(rowsAffected > 0) { JOptionPane.showMessageDialog(null, "Data Inserted successfully!"); } else { JOptionPane.showMessageDialog(null, "Data is not Inserted!", "Not successfully", JOptionPane.ERROR\_MESSAGE); } statement.close(); con.close();
Addition to that I'm getting error on line
int rowsAffected = statement.executeUpdate();
ERROR:
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__Employer__6F09E6800F975522'. Cannot insert duplicate key in object 'dbo.Employer'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)But as I see in my Management Studio that the data has been saved.but still getting this error ..why?
-
chdboy wrote:
I don't know if it is possible?
It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.
Veni, vidi, abiit domum
I'm able to display the value after data is saved, but it is showing me wrong value with this code.
int fileno1=Integer.parseInt(Result.getString(2));
int increment = ++fileno1;
String convertno1=String.valueOf(increment);
filenotxtfield.setText(convertno1);If 1 and 2 file no already exists after entering 3rd number manually and then save it ,it is showing me 2 not 4.?
-
I'm able to display the value after data is saved, but it is showing me wrong value with this code.
int fileno1=Integer.parseInt(Result.getString(2));
int increment = ++fileno1;
String convertno1=String.valueOf(increment);
filenotxtfield.setText(convertno1);If 1 and 2 file no already exists after entering 3rd number manually and then save it ,it is showing me 2 not 4.?
-
That code looks fine as it stands, but you are not saving the updated count anywhere.
Veni, vidi, abiit domum
-
Do I have to save it in the database and then retrieve it?
int increment = ++fileno1;
I have to save increment in the database?
It depends on how long you need the value, but you have to save it somewhere. If you only need the value for a single execution of the program, then just keep it in memory. If you want to continue where you left off with another separate execution, then you need to kepp it somewhere permanent, database, property etc.
Veni, vidi, abiit domum
-
It depends on how long you need the value, but you have to save it somewhere. If you only need the value for a single execution of the program, then just keep it in memory. If you want to continue where you left off with another separate execution, then you need to kepp it somewhere permanent, database, property etc.
Veni, vidi, abiit domum
ok right now I have not made is permanent,but I think I will save it in the database so when the application loads it will still remember the last file no ...thanks for the Idea. Right now I have done it with this code.
int fileno1=Integer.parseInt(Result.getString(2));
int increment = ++fileno1;
int increment2 = ++increment;
filenotxtfield.setText(Integer.toString(increment2));I have used this code after inserting the data in the database.So now as you suggested I'm working on how to show the next file no upon application starts.
-
ok right now I have not made is permanent,but I think I will save it in the database so when the application loads it will still remember the last file no ...thanks for the Idea. Right now I have done it with this code.
int fileno1=Integer.parseInt(Result.getString(2));
int increment = ++fileno1;
int increment2 = ++increment;
filenotxtfield.setText(Integer.toString(increment2));I have used this code after inserting the data in the database.So now as you suggested I'm working on how to show the next file no upon application starts.
I'm not sure if you're aware of this. But what this code does is essentially:
int fileno1=Integer.parseInt(Result.getString(2));
fileno1 = fileno1 + 1;
int increment = fileno1;
increment = increment + 1;
int increment2 = increment;
filenotxtfield.setText(Integer.toString(increment2));if you just want to increment the value of fileno1 (and don't need those variables increment and increment2) by 2:
int fileno1=Integer.parseInt(Result.getString(2));
fileno1 = fileno1 + 2;
filenotxtfield.setText(Integer.toString(fileno1)); -
I'm not sure if you're aware of this. But what this code does is essentially:
int fileno1=Integer.parseInt(Result.getString(2));
fileno1 = fileno1 + 1;
int increment = fileno1;
increment = increment + 1;
int increment2 = increment;
filenotxtfield.setText(Integer.toString(increment2));if you just want to increment the value of fileno1 (and don't need those variables increment and increment2) by 2:
int fileno1=Integer.parseInt(Result.getString(2));
fileno1 = fileno1 + 2;
filenotxtfield.setText(Integer.toString(fileno1));