static String quoteString ( String source , char quote ) { // Normally, the quoted string is two characters longer than the source // string (because of start quote and end quote). StringBuffer quoted = new StringBuffer ( source . length ( ) + 2 ) ; quoted . append ( quote ) ; for ( int i = 0 ; i < source . length ( ) ; i ++ ) { char c = source . charAt ( i ) ; // if the character is a quote, escape it with an extra quote if ( c == quote ) quoted . append ( quote ) ; quoted . append ( c ) ; } quoted . append ( quote ) ; return quoted . toString ( ) ; }
Quote a string so that it can be used as an identifier or a string literal in SQL statements . Identifiers are surrounded by double quotes and string literals are surrounded by single quotes . If the string contains quote characters they are escaped .