public static String sqlEscape ( String str ) { final int len = str . length ( ) ; buffer . setLength ( 0 ) ; StringBuilder sql = buffer ; for ( int i = 0 ; i < len ; i ++ ) { char c = str . charAt ( i ) ; switch ( c ) { case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; case ' ' : sql . append ( ' ' ) . append ( ' ' ) ; break ; // case '%': // sql.append('[').append('%').append(']'); // break; // case '_': // sql.append('[').append('_').append(']'); // break; default : sql . append ( c ) ; break ; } } return sql . toString ( ) ; }
Replaces all problematic characters from a String with their escaped versions to make it SQL conform .