1

I fetched a data from a database table. It is an integer value. Now I want to increase it by 1 and send it to another table. Can this be done to increase its value by 1?

String transaction = resultset.getString(1) + 1;
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
mayukhsroy
  • 141
  • 1
  • 1
  • 9
  • 4
    No why not `Integer transaction = resultSet.getInt(1) + 1;` then call the update query. because when you concatenate a String with an Integer it will not return the sum but the concatenation for example `String s = "123"; s = s + 1;` the result of s is `1231` – Youcef LAIDANI Jun 14 '18 at 10:45
  • why don`t you convert string to int and do the operation. Then you could convert it to string – User123456 Jun 14 '18 at 10:46

1 Answers1

2

I would actually recommend just doing a single insert or update operation. For example, if you wanted to insert this value into a new record in another table you could try:

INSERT INTO other_table (col)
SELECT id + 1
FROM first_table;

This would only require a single trip to the database, rather than making two trips, via a separate select and DML statements.

This answer is a bit thin on Java code, but for boilerplate JDBC code, you should refer to Java Trails or another SO question.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Didn't you miss the `VALUES` keyword? – Lino Jun 14 '18 at 10:52
  • @Lino No, I didn't miss it. `INSERT INTO ... SELECT` is a perfectly valid construct on almost all SQL databases. `VALUES` is mainly used for inserting _constants_. But, here we're not interested in a constant, but rather some column value. – Tim Biegeleisen Jun 14 '18 at 10:52
  • Oh, didn't know that, sorry for the confusion then. Learned something new today :) – Lino Jun 14 '18 at 10:53
  • the value from the column i am fetching is autogenerated and on the other hand i am sending this data along with other data to the other table. – mayukhsroy Jun 14 '18 at 10:54
  • i doing this in jsp – mayukhsroy Jun 14 '18 at 10:55
  • @MayukhSinghaRoy OK...auto generated doesn't change much so long as that value already exists when you are going to select it. So, does that record already exist? – Tim Biegeleisen Jun 14 '18 at 10:55