fill PreparedStatement - Java JDBC

Java examples for JDBC:PreparedStatement

Description

fill PreparedStatement

Demo Code


//package com.java2s;

import java.sql.PreparedStatement;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    public static void fillStatement(PreparedStatement stmt,
            Object... params) throws SQLException {
        if (params != null && params.length > 0) {
            ResultSetMetaData metadata = stmt.getMetaData();
            for (int i = 0; i < params.length; i++) {
                if (params[i] != null) {
                    stmt.setObject(i + 1, params[i]);
                } else {
                    try {
                        stmt.setNull(i + 1, metadata.getColumnType(i + 1));
                    } catch (NullPointerException ex) { // Oracle bug workaround
                        //            stmt.setNull(i + 1, java.sql.Types.VARCHAR);
                        stmt.setObject(i + 1, null);
                    }/*from  w w  w  .ja va  2s  .  co  m*/
                }
            }
        }
    }
}

Related Tutorials