All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.dao.dao4comm.util.PreparedStatementBuilder Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.dao.dao4comm.util;

import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.dao.dao4comm.annotation.dialect.FieldMetaData;
import java.io.UnsupportedEncodingException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jw362j
 */
public class PreparedStatementBuilder {

    private static final Logger log = LoggerFactory.getLogger(PreparedStatementBuilder.class);

    public static void initPreparedStatement(Map metas, PreparedStatement ps) {

        if (metas == null || metas.size() <= 0 || ps == null) {
            return;
        }
        final Set allKey = metas.keySet();
        for (Integer theIndex : allKey) {
            FieldMetaData data = metas.get(theIndex);
            log.info("theIndex:" + theIndex + ",data:" + data.getDataValue() + ",DataType" + data.getjDataType());
            if (JDataTypes.JBoolean.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        if ("true".equals(data.getDataValue().toString())) {
                            ps.setInt(theIndex, 1);
                        } else {
                            ps.setInt(theIndex, 0);
                        }
                    } else {
                        ps.setInt(theIndex, 0);
                    }

                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JByte.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setByte(theIndex, Byte.parseByte(data.getDataValue().toString()));
                    } else {
                        ps.setByte(theIndex, (byte) 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JChar1.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setString(theIndex, data.getDataValue().toString());
                    } else {
                        ps.setString(theIndex, "");
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JDate.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        Object valueDate = data.getDataValue();
                        ps.setTimestamp(theIndex, new Timestamp(((Date) valueDate).getTime()));
                    } else {
                        ps.setDate(theIndex, null);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JDouble.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setDouble(theIndex, Double.valueOf(data.getDataValue().toString()));
                    } else {
                        ps.setDouble(theIndex, 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JFloat.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setFloat(theIndex, Float.valueOf(data.getDataValue().toString()));
                    } else {
                        ps.setFloat(theIndex, 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JInt.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setInt(theIndex, Integer.valueOf(data.getDataValue().toString()));
                    } else {
                        ps.setInt(theIndex, 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JLong.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setLong(theIndex, Long.valueOf(data.getDataValue().toString()));
                    } else {
                        ps.setLong(theIndex, 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JShort.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setShort(theIndex, Short.valueOf(data.getDataValue().toString()));
                    } else {
                        ps.setShort(theIndex, (short) 0);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                    continue;
                }
            }
            if (JDataTypes.JString.equals(data.getjDataType())) {
                try {
                    if (data.getDataValue() != null) {
                        ps.setString(theIndex, new String(data.getDataValue().toString().getBytes(), "UTF-8"));
                    } else {
                        ps.setString(theIndex, null);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                } catch (UnsupportedEncodingException ex) {
                    log.error(ex.getMessage());
                }
            } else {

                try {
                    if (data.getDataValue() != null) {
                        ps.setString(theIndex, data.getDataValue() + "");
                    } else {
                        ps.setString(theIndex, null);
                    }
                } catch (SQLException s) {
                    log.error(s.getMessage());
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy