I read a lot about using StringBuffer and String, especially regarding concatenation in Java and whether a stream is safe or not.
So, in the various Java methods that should be used?
For example, in PreparedStatement there should be a StringBuffer request:
String query = ("SELECT * " +
"FROM User " +
"WHERE userName = ?;");
try {
ps = connection.prepareStatement(query);
And again in the String utility, for example:
public static String prefixApostrophesWithBackslash(String stringIn) {
String stringOut = stringIn.replaceAll("'", "\\\\'");
return stringOut;
}
and
public static String removeChar(String stringIn, char c) {
String stringOut = ("");
for (int i = 0; i < stringIn.length(); i++) {
if (stringIn.charAt(i) != c) {
stringOut += stringIn.charAt(i);
}
}
return stringOut;
}
Should I use StringBuffers? Especially where repalceAll is not available for such objects.
thank
Mr. Morgan.
Thanks for all the tips. StringBuffers were replaced by StringBuilders and Strings replaced by StringBuilders, where I thought it was the best.
source
share