View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db; 22 23 24 /*** 25 * Ensure special characters are escaped before issuing SQL statements. 26 * 27 * @version v1.0 28 * 29 * @author __AUTHOR__ 30 * 31 */ 32 public class SqlEncoder extends java.lang.Object { 33 /* 34 * Constructors 35 */ 36 37 /*** 38 * Default Constructor 39 */ 40 public SqlEncoder() { 41 } 42 43 /* 44 * Methods 45 */ 46 47 /*** 48 * Encodes characters as escape sequences to prevent errors when passed to 49 * a SQL statement 50 * 51 * Actual chars requiring encoding are: ', newline, \, 52 * 53 */ 54 public static String encode(String string) { 55 if (string == null) { 56 return ""; 57 } 58 59 int n = string.length(); 60 char character; 61 StringBuffer buffer = new StringBuffer(); 62 63 // loop over all the characters of the String. 64 for (int i = 0; i < n; i++) { 65 character = string.charAt(i); 66 67 // the sqlCode of these characters are added to a StringBuffer one by one 68 switch (character) { 69 case '\'': 70 buffer.append("''"); 71 72 break; 73 case '//': 74 buffer.append("//"); 75 76 break; 77 case '\n': 78 buffer.append("\n"); 79 80 break; 81 default: 82 buffer.append(character); 83 } 84 } 85 86 return buffer.toString(); 87 } 88 89 /* 90 * Properties 91 */ 92 }

This page was automatically generated by Maven