Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.ow2.odis.sql;
  
  import java.net.URL;
  import java.sql.Blob;
  import java.sql.Clob;
  import java.sql.Date;
 import java.sql.Ref;
 import java.sql.Time;
 import java.sql.Types;
 
 import  org.objectweb.util.monolog.Monolog;
 import  org.objectweb.util.monolog.api.BasicLevel;
 import  org.objectweb.util.monolog.api.Logger;

Author(s):
lqzp4386
 
 public class ResultSetHelper {
     private static String dateFormat;

    
The Constant LOGGER.
 
     static final Logger LOGGER = Monolog.initialize().getLogger(
             ResultSetHelper.class.getName());
 
     public static void setInStatement(PreparedStatement pstmt,
             int parameterIndexString typeObject valuethrows SQLException,
             OdisAttributeException {
         if ((type != null) && (value != null)) {
             if ("BigDecimal".toLowerCase().equals(type.toLowerCase())) {
                 if ("".equals((Stringvalue) || value == null) {
                     pstmt.setBigDecimal(parameterIndexnull);
                 } else {
                     pstmt.setBigDecimal(parameterIndexnew BigDecimal(
                             (Stringvalue));
                 }
             } else if ("Blob".toLowerCase().equals(type.toLowerCase())) {
                 throw new OdisAttributeException(
                         "java.sql.Blob type is not supported yet.");
             } else if ("Boolean".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setBoolean(parameterIndex, Boolean
                         .parseBoolean((Stringvalue));
             } else if ("Byte".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setByte(parameterIndex, Byte.parseByte((Stringvalue));
             } else if ("Bytes".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setBytes(parameterIndex, (byte[]) value);
             } else if ("Clob".toLowerCase().equals(type.toLowerCase())) {
                 throw new OdisAttributeException(
                         "java.sql.Clob type is not supported yet.");
             } else if ("Date".toLowerCase().equals(type.toLowerCase())) {
                 Date date = new java.sql.Date(getJavaDate((Stringvalue)
                         .getTime());
                 pstmt.setDate(parameterIndexdate);
             } else if ("Double".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setDouble(parameterIndex, Double
                         .parseDouble((Stringvalue));
             } else if ("Float".toLowerCase().equals(type.toLowerCase())) {
                 pstmt
                         .setFloat(parameterIndex, Float
                                 .parseFloat((Stringvalue));
             } else if ("Int".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setInt(parameterIndex, Integer.parseInt((Stringvalue));
             } else if ("Integer".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setInt(parameterIndex, ((Integervalue).intValue());
             } else if ("Long".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setLong(parameterIndex, Long.parseLong((Stringvalue));
             } else if ("Object".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setObject(parameterIndexvalue);
             } else if ("Ref".toLowerCase().equals(type.toLowerCase())) {
                 throw new OdisAttributeException(
                         "java.sql.Ref type is not supported yet.");
             } else if ("String".toLowerCase().equals(type.toLowerCase())) {
                 pstmt.setString(parameterIndex, (Stringvalue);
             } else if ("Short".toLowerCase().equals(type.toLowerCase())) {
                 pstmt
                         .setShort(parameterIndex, Short
                                 .parseShort((Stringvalue));
             } else if ("Time".toLowerCase().equals(type.toLowerCase())) {
                 Time time = new Time(getJavaDate((Stringvalue).getTime());
                 pstmt.setTime(parameterIndextime);
             } else if ("Timestamp".toLowerCase().equals(type.toLowerCase())) {
                 Timestamp timestamp = new Timestamp(getJavaDate((Stringvalue)
                         .getTime());
                 pstmt.setTimestamp(parameterIndextimestamp);
             } else if ("URL".toLowerCase().equals(type.toLowerCase())) {
                 try {
                     pstmt.setURL(parameterIndexnew URL((Stringvalue));
                 } catch (MalformedURLException e) {
                     throw new OdisAttributeException(e.getClass() + " "
                            + e.getMessage());
                }
            } else {
                throw new OdisAttributeException("The type " + type
                        + " is not reconized.");
            }
        }
    }
    protected static java.util.Date getJavaDate(String value)
            throws OdisAttributeException {
        java.util.Date result = null;
        if (value != null) {
            DateFormat formater = new SimpleDateFormat(,
                    ...);
            try {
                result = formater.parse((Stringvalue);
            } catch (Exception e) {
                throw new OdisAttributeException(e.getMessage()
                        + " - date format needed: " + 
                        + " (value received = " + value + ")");
            }
        }
        return result;
    }

    
Gets the value.

Parameters:
rs the rs
columnName the column name
type the type
Returns:
an array : the column name in the first column (String) + the object value in the second column (Object) + the java class name in the third column (String)
Throws:
SQLException the SQL exception
OdisException the odis exception
    public static Object[] getValue(ResultSet rsString columnNameString type)
            throws SQLExceptionOdisAttributeException {
        return getValue(rscolumnNamegetSqlTypeValue(type));
    }

    
Gets the value.

Parameters:
rs the rs
row the row
columnIndex the column index
type the type
Returns:
an array : the column name in the first column (String) + the object value in the second column (Object) + the java class name in the third column (String)
Throws:
SQLException the SQL exception
OdisException the odis exception
    public static Object[] getValue(ResultSet rsString columnNameint type)
            throws SQLExceptionOdisAttributeException {
        Object value = null;
        String className = null;
        switch (type) {
        case .:
            value = rs.getArray(columnName);
            className = java.sql.Array.class.getName();
            break;
        case .:
            value = Long.valueOf(rs.getLong(columnName));
            className = Long.class.getName();
            break;
        case .:
            value = rs.getBytes(columnName);
            className = byte.class.getName();
            break;
        case .:
            value = Boolean.valueOf(rs.getBoolean(columnName));
            className = Boolean.class.getName();
            break;
        case .:
            value = rs.getBlob(columnName);
            className = Blob.class.getName();
            break;
        case .:
            value = Boolean.valueOf(rs.getBoolean(columnName));
            className = Boolean.class.getName();
            break;
        case .:
            value = rs.getString(columnName);
            className = String.class.getName();
            break;
        case .:
            value = rs.getClob(columnName);
            className = Clob.class.getName();
            break;
        case .:
            value = rs.getURL(columnName);
            className = URL.class.getName();
            break;
        case .:
            value = rs.getDate(columnName);
            className = Date.class.getName();
            break;
        case .:
            value = rs.getBigDecimal(columnName);
            className = BigDecimal.class.getName();
            break;
        case .:
            throw new OdisAttributeException(
                    "The java.sql.type.DISTINCT type is not supported.");
        case .:
            value = Double.valueOf(rs.getDouble(columnName));
            className = Double.class.getName();
            break;
        case .:
            value = Double.valueOf(rs.getDouble(columnName));
            className = Double.class.getName();
            break;
        case .:
            value = Integer.valueOf(rs.getInt(columnName));
            className = Integer.class.getName();
            break;
        case .:
            value = rs.getObject(columnName);
            className = Object.class.getName();
            break;
        case .:
            value = rs.getBytes(columnName);
            className = byte[].class.getName();
            break;
        case .:
            throw new OdisAttributeException(
                    "The java.sql.type.NULL type is not supported.");
        case .:
            value = rs.getBigDecimal(columnName);
            className = BigDecimal.class.getName();
            break;
        case .:
            throw new OdisAttributeException(
                    "The java.sql.type.OTHER type is not supported.");
        case .:
            value = Float.valueOf(rs.getFloat(columnName));
            className = Float.class.getName();
            break;
        case .:
            value = rs.getRef(columnName);
            className = Ref.class.getName();
            break;
        case .:
            value = Short.valueOf(rs.getShort(columnName));
            className = Short.class.getName();
            break;
        case .:
            throw new OdisAttributeException(
                    "The java.sql.type.STRUCT type is not supported.");
        case .:
            value = rs.getTime(columnName);
            className = Time.class.getName();
            break;
        case .:
            value = rs.getTimestamp(columnName);
            className = Timestamp.class.getName();
            break;
        case .:
            value = Byte.valueOf(rs.getByte(columnName));
            className = Byte.class.getName();
            break;
        case .:
            value = rs.getBytes(columnName);
            className = byte[].class.getName();
            break;
        case .:
            value = rs.getString(columnName);
            className = String.class.getName();
            break;
        default:
            throw new OdisAttributeException("The type \"" + type
                    + "\" is not reconized as a java.sql.type.");
        }
        Object result[] = { columnNamevalueclassName };
        if (.isLoggable(BasicLevel.DEBUG)) {
            .log(BasicLevel.DEBUG, "[" + columnName + ", " + value + ", "
                    + className + "]");
        }
        return result;
    }

    

Parameters:
typeName
Returns:
Throws:
OdisException
    public static int getSqlTypeValue(String typeName)
            throws OdisAttributeException {
        int result;
        try {
            result = getSqlTypeValue_fromJavaType(typeName);
        } catch (OdisAttributeException e) {
            try {
                result = getSqlTypeValue_fromSqlType(typeName);
            } catch (OdisException e1) {
                throw e;
            }
        }
        return result;
    }

    
Gets the sql type value.

Parameters:
typeName the type name
Returns:
the sql type value
Throws:
OdisException the odis exception
    private static int getSqlTypeValue_fromJavaType(String typeName)
            throws OdisAttributeException {
        int result;
        if ("ARRAY".equals(typeName.toUpperCase()))
            result = .;
        else if ("LONG".equals(typeName.toUpperCase()))
            result = .;
        else if ("BYTES".equals(typeName.toUpperCase()))
            result = .;
        else if ("BOOLEAN".equals(typeName.toUpperCase()))
            result = .;
        else if ("BLOB".equals(typeName.toUpperCase()))
            result = .;
        else if ("CLOB".equals(typeName.toUpperCase()))
            result = .;
        else if ("DATE".equals(typeName.toUpperCase()))
            result = .;
        else if ("BIGDECIMAL".equals(typeName.toUpperCase()))
            result = .;
        else if ("DECIMAL".equals(typeName.toUpperCase()))
            result = .;
        else if ("DOUBLE".equals(typeName.toUpperCase()))
            result = .;
        else if ("INTEGER".equals(typeName.toUpperCase()))
            result = .;
        else if ("INT".equals(typeName.toUpperCase()))
            result = .;
        else if ("JAVA_OBJECT".equals(typeName.toUpperCase()))
            result = .;
        else if ("STRING".equals(typeName.toUpperCase()))
            result = .;
        else if ("FLOAT".equals(typeName.toUpperCase()))
            result = .;
        else if ("REF".equals(typeName.toUpperCase()))
            result = .;
        else if ("SHORT".equals(typeName.toUpperCase()))
            result = .;
        else if ("TIME".equals(typeName.toUpperCase()))
            result = .;
        else if ("TIMESTAMP".equals(typeName.toUpperCase()))
            result = .;
        else if ("BYTE".equals(typeName.toUpperCase()))
            result = .;
        else
            throw new OdisAttributeException("The type \""
                    + typeName.toUpperCase()
                    + "\" is not reconized as a java.sql.type.");
        return result;
    }

    
Gets the sql type value.

Parameters:
typeName the type name
Returns:
the sql type value
Throws:
OdisException the odis exception
    private static int getSqlTypeValue_fromSqlType(String typeName)
            throws OdisException {
        int result;
        if ("ARRAY".equals(typeName.toUpperCase()))
            result = .;
        else if ("BIGINT".equals(typeName.toUpperCase()))
            result = .;
        else if ("BINARY".equals(typeName.toUpperCase()))
            result = .;
        else if ("BIT".equals(typeName.toUpperCase()))
            result = .;
        else if ("BLOB".equals(typeName.toUpperCase()))
            result = .;
        else if ("BOOLEAN".equals(typeName.toUpperCase()))
            result = .;
        else if ("CHAR".equals(typeName.toUpperCase()))
            result = .;
        else if ("CLOB".equals(typeName.toUpperCase()))
            result = .;
        else if ("DATALINK".equals(typeName.toUpperCase()))
            result = .;
        else if ("DATE".equals(typeName.toUpperCase()))
            result = .;
        else if ("DECIMAL".equals(typeName.toUpperCase()))
            result = .;
        else if ("DISTINCT".equals(typeName.toUpperCase()))
            result = .;
        else if ("DOUBLE".equals(typeName.toUpperCase()))
            result = .;
        else if ("FLOAT".equals(typeName.toUpperCase()))
            result = .;
        else if ("INTEGER".equals(typeName.toUpperCase()))
            result = .;
        else if ("INT".equals(typeName.toUpperCase()))
            result = .;
        else if ("JAVA_OBJECT".equals(typeName.toUpperCase()))
            result = .;
        else if ("LONGVARBINARY".equals(typeName.toUpperCase()))
            result = .;
        else if ("NULL".equals(typeName.toUpperCase()))
            result = .;
        else if ("NUMERIC".equals(typeName.toUpperCase()))
            result = .;
        else if ("OTHER".equals(typeName.toUpperCase()))
            result = .;
        else if ("REAL".equals(typeName.toUpperCase()))
            result = .;
        else if ("REF".equals(typeName.toUpperCase()))
            result = .;
        else if ("SMALLINT".equals(typeName.toUpperCase()))
            result = .;
        else if ("STRUCT".equals(typeName.toUpperCase()))
            result = .;
        else if ("TIME".equals(typeName.toUpperCase()))
            result = .;
        else if ("TIMESTAMP".equals(typeName.toUpperCase()))
            result = .;
        else if ("TINYINT".equals(typeName.toUpperCase()))
            result = .;
        else if ("VARBINARY".equals(typeName.toUpperCase()))
            result = .;
        else if ("VARCHAR".equals(typeName.toUpperCase()))
            result = .;
        else
            throw new OdisAttributeException("The type \""
                    + typeName.toUpperCase()
                    + "\" is not reconized as a java.sql.type.");
        return result;
    }

    
Gets the date format

Returns:
dateFormat
    public static String getDateFormat() {
        return ;
    }

    
Sets the date format

Parameters:
dateFormat
    public static void setDateFormat(String dateFormat) {
        . = dateFormat;
    }
New to GrepCode? Check out our FAQ X