'Join' two different field types together into a String - possible in Java?
Not entirely certain whether this is a straightforward Java or more of a JDBC specific question:
I have two fields, one is a String and one an integer. I need to concatenate these fields together to form one String, prior to performing an insert on a table. ie:
String s = dbRS.getString("partition_name");//get full string
String partstr = s.substring(0, 14);// get first 14 (alpha) characters
int new_partition_number = max_partition_number + 1;//create new unique partition number, incremented by 1
Now I have
- a String (partstr - i.e. "TN1234567890_")
- an integer (i.e. 50)
(NB. The number has to be an integer because it is subject to calculation elsewhere.)
I wish to join these together into a String ("TN1234567890_50") and use the field in an inserted row. I've tried using
String partition_string =new StringBuffer(partstr).append(new_partition_number ).toString();
... but this doesn't work.
I read on a forum that it's impossible to cast an integer to a String in this way in Java, which sounds pretty incredulous from where I am! However my Osborne Java 2 Complete Reference doesn't mention it at all.
Any assistance will be very gratefully received!
Thanks.
# 2
> What is wrong with simply doing :
>
> String t = "test";
> t = t +50;
It compiles, but I get this:
Exception in thread "main" java.lang.NumberFormatException: For input string: "TB_1309845876_"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:62)
at java.lang.Integer.parseInt(Integer.java:448)
at java.lang.Integer.parseInt(Integer.java:498)
at SKModEXEC.main(SKModEXEC.java:228)
# 6
Not really a crosspost - I originally posted it over there and was then told that it belonged here. But sorry for any inconvenience caused or rules trampled upon.
OK - well, I managed to join the fields and combine them into a String field and not get a compile error. However when I try to do the insert, I get a SQLCODE -104 (using a db2 database).
Looking at this link, it seems to suggest that there is still a problem with the format of this (conjoined) field when trying to perform an insert:
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2tools.adb.doc.ug/h2xsqle.htm
Thus I think that it's probably neither a Java nor a (directly) JDBC issue and is DB2 specific?
# 7
> Not really a crosspost - I originally posted it over
> there and was then told that it belonged here. But
> sorry for any inconvenience caused or rules trampled
> upon.
I didn't see anywhere where you were told it belonged over here.
> OK - well, I managed to join the fields and combine
> them into a String field and not get a compile error.
> However when I try to do the insert, I get a SQLCODE
> -104 (using a db2 database).
Have a look at what the error message indicates - hopefully something a bit meaningful.
> Looking at this link, it seems to suggest that there
> is still a problem with the format of this
> (conjoined) field when trying to perform an insert:
>
> http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2
> /index.jsp?topic=/com.ibm.db2tools.adb.doc.ug/h2xsqle.
> htm
>
> Thus I think that it's probably neither a Java nor a
> (directly) JDBC issue and is DB2 specific?
It's possible. Another thing to try is to make a string that includes the number right off the bat and see if that results in any issues.
# 8
> > Not really a crosspost - I originally posted it
> over
> > there and was then told that it belonged here. But
> > sorry for any inconvenience caused or rules
> trampled
> > upon.
>
> I didn't see anywhere where you were told it belonged
> over here.
>
I didn't say that someone told me on the forum - they didn't. But it's all really besides the point. Again, I'm really, very sincerely sorry.