set PreparedStatement Parameters - Java java.sql

Java examples for java.sql:PreparedStatement

Description

set PreparedStatement Parameters

Demo Code


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

public class Main{
    /*  w ww.java  2 s . com*/
    public static void setStatementParameters(PreparedStatement pst,
            Object[] params) throws SQLException {
        for (int i = 0; i < params.length; ++i) {
            Object obj = params[i];
            if (null == obj) {
                pst.setNull(i + 1, Types.CHAR);
            } else if (obj instanceof java.sql.Date) {
                pst.setDate(i + 1, (java.sql.Date) obj);
            } else if (obj instanceof java.sql.Timestamp) {
                pst.setTimestamp(i + 1, (java.sql.Timestamp) obj);
            } else {
                pst.setObject(i + 1, obj);
            }
        }
    }
}

Related Tutorials