0

I performed database connectivity through java with db2 database. Database table name "CRID" and which contains column name "CRID". It's datatype is varchar. Currently value in crid column is 1. When I run the program it displays value present in CRID table and increment its value by one. Second time I run the program value is displayed as 2 and its value is incremented by one. But I want output to be displayed as 00001. when value is incremented next time it should display value as 00002. When its value reaches to 10, it should display output as 00010.

I tried in many ways to do this but i was not successful. following was my approach to do it but it was not successful. Value in database table, I saved it as 00001, so the first time it showed me output as 00001 but in second attempt it showed me output as 2.

Please tell me how to do it. Thank You

  • 1
    Where do you want to display it? Did you search for **String formatting in Java**? – Codebender Aug 05 '15 at 05:21
  • Where do you want to spend the money, on the database server or in the client? How about formatting it in the Java client? Also I recommend taking a look at the data type. VARCHAR for an ID may be ok, but here it looks like an INT is better (faster and cheaper). – data_henrik Aug 05 '15 at 05:25
  • Hi Codebender abd Henrik. Thanks for advice. I want to display it on console. I will look into it. "String formatting in java". I will let you know. – vikrant kamble Aug 05 '15 at 05:37
  • Hi Codebender, I have looked into String formatting in java given on this link "http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/". It shows required output on console. Actually I want to know if we can do similar formatting while storing values in database. Here what i want to say is I want value in database to be stored as 00001 instead of 1. When I display it in output value should be displayed as 00001. Again in second attempt value in the database should be stored as 00002. And in the output it should be displayed as 00002 instead of 2. – vikrant kamble Aug 05 '15 at 06:34
  • Could you please tell me how to achieve this. – vikrant kamble Aug 05 '15 at 06:34

1 Answers1

2

I believe what you need is:

  String.format("%05d", <value>);

See references:

Here And here

Community
  • 1
  • 1
Benjamin
  • 1,221
  • 11
  • 28