KeyHolder

import com.gctx.framework.dao.IGenericJdbcDAO;
import com.gctx.framework.logs.Logit;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;



public Integer insert(final String sql, final Object[] objs)
  {
    Logit.debugLog(sql);

    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcTemplate().update(new PreparedStatementCreator() {
      public PreparedStatement createPreparedStatement(Connection conn) {
        PreparedStatement ps = null;
        try {
          ps = conn.prepareStatement(sql, 1);

          if ((objs != null) && (objs.length > 0))
            for (int i = 0; i < objs.length; i++)
              ps.setObject(i + 1, objs[i]);
        }
        catch (SQLException e)
        {
          Logit.errorLog(e.getMessage(), e);
        }

        return ps;
      }
    }
    , keyHolder);

    if ((keyHolder == null) || (keyHolder.getKey() == null)) {
      return Integer.valueOf(0);
    }

    return Integer.valueOf(keyHolder.getKey().intValue());
  }

你可能感兴趣的:(KeyHolder)