1 package com.tonbeller.wcf.utils; 2 3 import java.sql.Time ; 4 5 import org.apache.log4j.Logger; 6 7 11 public class SqlUtils { 12 13 private static Logger logger = Logger.getLogger(SqlUtils.class); 14 15 private SqlUtils() {} 16 17 18 21 public static final String convStatement(String genstmt) { 22 StringBuffer sb = new StringBuffer (); 23 int n = genstmt.length(); 24 for (int i = 0; i < n; i++) { 25 char c = genstmt.charAt(i); 26 if (Character.isWhitespace(c)) 27 sb.append(' '); 28 else 29 sb.append(c); 30 } 31 return sb.toString(); 32 } 33 34 37 public static String escapeSql(Object value) { 38 if (value == null) 39 return "NULL"; 40 if (value instanceof String ) 41 return "'" + escapeSqlString((String ) value) + "'"; 42 if (value instanceof java.sql.Date || value instanceof Time 43 || value instanceof java.sql.Timestamp ) 44 return "'" + value.toString() + "'"; 45 if (value instanceof java.util.Date ) { 46 java.sql.Date d = new java.sql.Date (((java.util.Date ) value).getTime()); 47 return "'" + d.toString() + "'"; 48 } 49 return String.valueOf(value); 50 } 51 52 private static String escapeSqlString(String s) { 53 int pos = s.indexOf('\''); 54 if (pos >= 0) { 55 StringBuffer sb = new StringBuffer (); 56 while (pos >= 0) { 57 sb.append(s.substring(0, pos)); 58 sb.append("''"); 59 s = s.substring(pos + 1); 60 pos = s.indexOf('\''); 61 } 62 sb.append(s); 63 s = sb.toString(); 64 } 65 return s; 66 } 67 68 } | Popular Tags |