Java Code Examples for java.sql.PreparedStatement.setBigDecimal()

The following are Jave code examples for showing how to use setBigDecimal() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: lams   File: SimpleTriggerPersistenceDelegate.java   Source Code and License Vote up 6 votes
public int updateExtendedTriggerProperties(Connection conn, OperableTrigger trigger, String state, JobDetail jobDetail) throws SQLException, IOException {

        SimpleTrigger simpleTrigger = (SimpleTrigger)trigger;
        
        PreparedStatement ps = null;

        try {
            ps = conn.prepareStatement(Util.rtp(UPDATE_SIMPLE_TRIGGER, tablePrefix, schedNameLiteral));

            ps.setInt(1, simpleTrigger.getRepeatCount());
            ps.setBigDecimal(2, new BigDecimal(String.valueOf(simpleTrigger.getRepeatInterval())));
            ps.setInt(3, simpleTrigger.getTimesTriggered());
            ps.setString(4, simpleTrigger.getKey().getName());
            ps.setString(5, simpleTrigger.getKey().getGroup());

            return ps.executeUpdate();
        } finally {
            Util.closeStatement(ps);
        }
    }
 
Example 2
Project: sample.daytrader3   File: TradeDirect.java   Source Code and License Vote up 6 votes
private HoldingDataBean createHolding(Connection conn, int accountID,
		String symbol, double quantity, BigDecimal purchasePrice)
		throws Exception {
	HoldingDataBean holdingData = null;

	Timestamp purchaseDate = new Timestamp(System.currentTimeMillis());
	PreparedStatement stmt = getStatement(conn, createHoldingSQL);

	Integer holdingID = KeySequenceDirect.getNextID(conn, "holding",
			inSession, getInGlobalTxn());
	stmt.setInt(1, holdingID.intValue());
	stmt.setTimestamp(2, purchaseDate);
	stmt.setBigDecimal(3, purchasePrice);
	stmt.setDouble(4, quantity);
	stmt.setString(5, symbol);
	stmt.setInt(6, accountID);
	int rowCount = stmt.executeUpdate();

	stmt.close();

	return getHoldingData(conn, holdingID.intValue());
}
 
Example 3
Project: eXperDB-DB2PG   File: DataAdapter.java   Source Code and License Vote up 6 votes
private  void PreparedStmtSetValue(PreparedStatement pStmt, int idx, Object obj) throws SQLException{
	if (obj instanceof String) {
		pStmt.setString(idx, (String) obj);
	} else if(obj instanceof Integer){
		pStmt.setInt(idx, (Integer) obj);
	} else if(obj instanceof BigDecimal){
			pStmt.setBigDecimal(idx, (BigDecimal) obj);
	} else if(obj instanceof Double){
		pStmt.setDouble(idx, (Double) obj);
	} else if(obj instanceof Date){
		pStmt.setDate(idx, (Date) obj);
	} else if(obj instanceof byte[]){
		pStmt.setBytes(idx, (byte[]) obj);			
	} else{
		pStmt.setObject(idx, obj);
	}
}
 
Example 4
Project: ChronoBike   File: RecordColTypeManagerDecimal.java   Source Code and License Vote up 6 votes
public boolean transfer(int nColumnNumber1Based, ResultSet resultSetSource, PreparedStatement insertStatementInsert)
{
	try
	{			
		BigDecimal value = resultSetSource.getBigDecimal(m_nColSourceIndex);
		if (!resultSetSource.wasNull())
			insertStatementInsert.setBigDecimal(m_nColSourceIndex, value);
		else
			insertStatementInsert.setNull(m_nColSourceIndex, Types.DECIMAL);			
		return true;
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}
	return false;		
}
 
Example 5
Project: lams   File: StdJDBCDelegate.java   Source Code and License Vote up 6 votes
/**
 * <p>
 * Get the number of triggers in the given states that have
 * misfired - according to the given timestamp.
 * </p>
 * 
 * @param conn the DB Connection
 */
public int countMisfiredTriggersInState(
        Connection conn, String state1, long ts) throws SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(COUNT_MISFIRED_TRIGGERS_IN_STATE));
        ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
        ps.setString(2, state1);
        rs = ps.executeQuery();

        if (rs.next()) {
            return rs.getInt(1);
        }

        throw new SQLException("No misfired trigger count returned.");
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }
}
 
Example 6
Project: lams   File: StdJDBCDelegate.java   Source Code and License Vote up 6 votes
/**
 * <p>
 * Get the names of all of the triggers in the given group and state that
 * have misfired.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @return an array of <code>{@link
 * org.quartz.utils.Key}</code> objects
 */
public List<TriggerKey> selectMisfiredTriggersInGroupInState(Connection conn,
        String groupName, String state, long ts) throws SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        ps = conn
                .prepareStatement(rtp(SELECT_MISFIRED_TRIGGERS_IN_GROUP_IN_STATE));
        ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
        ps.setString(2, groupName);
        ps.setString(3, state);
        rs = ps.executeQuery();

        LinkedList<TriggerKey> list = new LinkedList<TriggerKey>();
        while (rs.next()) {
            String triggerName = rs.getString(COL_TRIGGER_NAME);
            list.add(triggerKey(triggerName, groupName));
        }
        return list;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }
}
 
Example 7
Project: asura   File: StdJDBCDelegate.java   Source Code and License Vote up 6 votes
/**
 * <p>
 * Insert the simple trigger data.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @param trigger
 *          the trigger to insert
 * @return the number of rows inserted
 */
public int insertSimpleTrigger(Connection conn, SimpleTrigger trigger)
    throws SQLException {
    PreparedStatement ps = null;

    try {
        ps = conn.prepareStatement(rtp(INSERT_SIMPLE_TRIGGER));
        ps.setString(1, trigger.getName());
        ps.setString(2, trigger.getGroup());
        ps.setInt(3, trigger.getRepeatCount());
        ps.setBigDecimal(4, new BigDecimal(String.valueOf(trigger
                .getRepeatInterval())));
        ps.setInt(5, trigger.getTimesTriggered());

        return ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }
}
 
Example 8
Project: otter-G   File: TestMysqlUnsignedNumber.java   Source Code and License Vote up 6 votes
public static void insertNumeric() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Properties from = new Properties();
    from.put("user", "root");
    from.put("password", "root");
    from.put("characterEncoding", "utf8");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erosa", from);
    PreparedStatement pst = conn.prepareStatement("insert into unsignednumeric(id,id1,id2,id3) values (?,?,?,?)");
    pst.setLong(1, Integer.MAX_VALUE * 2L);
    pst.setLong(2, Integer.MIN_VALUE);
    pst.setBigDecimal(3, new BigDecimal("18446744073709551614"));
    pst.setBigDecimal(4, new BigDecimal(Long.MIN_VALUE + ""));
    pst.executeUpdate();

    pst.close();
    conn.close();
}
 
Example 9
Project: asura   File: StdJDBCDelegate.java   Source Code and License Vote up 5 votes
/**
 * <p>
 * Insert a fired trigger.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @param trigger
 *          the trigger
 * @param state
 *          the state that the trigger should be stored in
 * @return the number of rows inserted
 */
public int insertFiredTrigger(Connection conn, Trigger trigger,
        String state, JobDetail job) throws SQLException {
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(rtp(INSERT_FIRED_TRIGGER));
        ps.setString(1, trigger.getFireInstanceId());
        ps.setString(2, trigger.getName());
        ps.setString(3, trigger.getGroup());
        setBoolean(ps, 4, trigger.isVolatile());
        ps.setString(5, instanceId);
        ps.setBigDecimal(6, new BigDecimal(String.valueOf(trigger
                .getNextFireTime().getTime())));
        ps.setString(7, state);
        if (job != null) {
            ps.setString(8, trigger.getJobName());
            ps.setString(9, trigger.getJobGroup());
            setBoolean(ps, 10, job.isStateful());
            setBoolean(ps, 11, job.requestsRecovery());
        } else {
            ps.setString(8, null);
            ps.setString(9, null);
            setBoolean(ps, 10, false);
            setBoolean(ps, 11, false);
        }
        ps.setInt(12, trigger.getPriority());
        

        return ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }
}
 
Example 10
Project: asura   File: PointbaseDelegate.java   Source Code and License Vote up 5 votes
public int insertTrigger(Connection conn, Trigger trigger, String state,
        JobDetail jobDetail) throws SQLException, IOException {

    ByteArrayOutputStream baos = serializeJobData(trigger.getJobDataMap());
    int len = baos.toByteArray().length;
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    
    PreparedStatement ps = null;

    int insertResult = 0;

    try {
        ps = conn.prepareStatement(rtp(INSERT_TRIGGER));
        ps.setString(1, trigger.getName());
        ps.setString(2, trigger.getGroup());
        ps.setString(3, trigger.getJobName());
        ps.setString(4, trigger.getJobGroup());
        setBoolean(ps, 5, trigger.isVolatile());
        ps.setString(6, trigger.getDescription());
        ps.setBigDecimal(7, new BigDecimal(String.valueOf(trigger
                .getNextFireTime().getTime())));
        long prevFireTime = -1;
        if (trigger.getPreviousFireTime() != null) {
            prevFireTime = trigger.getPreviousFireTime().getTime();
        }
        ps.setBigDecimal(8, new BigDecimal(String.valueOf(prevFireTime)));
        ps.setString(9, state);
        if (trigger instanceof SimpleTrigger && ((SimpleTrigger)trigger).hasAdditionalProperties() == false ) {
            ps.setString(10, TTYPE_SIMPLE);
        } else if (trigger instanceof CronTrigger && ((CronTrigger)trigger).hasAdditionalProperties() == false ) {
            ps.setString(10, TTYPE_CRON);
        } else {
            ps.setString(10, TTYPE_BLOB);
        }
        ps.setBigDecimal(11, new BigDecimal(String.valueOf(trigger
                .getStartTime().getTime())));
        long endTime = 0;
        if (trigger.getEndTime() != null) {
            endTime = trigger.getEndTime().getTime();
        }
        ps.setBigDecimal(12, new BigDecimal(String.valueOf(endTime)));
        ps.setString(13, trigger.getCalendarName());
        ps.setInt(14, trigger.getMisfireInstruction());
        ps.setBinaryStream(15, bais, len);
        ps.setInt(16, trigger.getPriority());
        
        insertResult = ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }

    if (insertResult > 0) {
        String[] trigListeners = trigger.getTriggerListenerNames();
        for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
            insertTriggerListener(conn, trigger, trigListeners[i]);
        }
    }

    return insertResult;
}
 
Example 11
Project: aliyun-maxcompute-data-collectors   File: JdbcWritableBridge.java   Source Code and License Vote up 5 votes
public static void writeBigDecimal(BigDecimal val, int paramIdx,
    int sqlType, PreparedStatement s) throws SQLException {
  if (null == val) {
    s.setNull(paramIdx, sqlType);
  } else {
    s.setBigDecimal(paramIdx, val);
  }
}
 
Example 12
Project: asura   File: PointbaseDelegate.java   Source Code and License Vote up 5 votes
public int updateTrigger(Connection conn, Trigger trigger, String state,
        JobDetail jobDetail) throws SQLException, IOException {

    ByteArrayOutputStream baos = serializeJobData(trigger.getJobDataMap());
    int len = baos.toByteArray().length;
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            
    PreparedStatement ps = null;

    int insertResult = 0;


    try {
        ps = conn.prepareStatement(rtp(UPDATE_TRIGGER));
            
        ps.setString(1, trigger.getJobName());
        ps.setString(2, trigger.getJobGroup());
        setBoolean(ps, 3, trigger.isVolatile());
        ps.setString(4, trigger.getDescription());
        long nextFireTime = -1;
        if (trigger.getNextFireTime() != null) {
            nextFireTime = trigger.getNextFireTime().getTime();
        }
        ps.setBigDecimal(5, new BigDecimal(String.valueOf(nextFireTime)));
        long prevFireTime = -1;
        if (trigger.getPreviousFireTime() != null) {
            prevFireTime = trigger.getPreviousFireTime().getTime();
        }
        ps.setBigDecimal(6, new BigDecimal(String.valueOf(prevFireTime)));
        ps.setString(7, state);
        if (trigger instanceof SimpleTrigger && ((SimpleTrigger)trigger).hasAdditionalProperties() == false ) {
            //                updateSimpleTrigger(conn, (SimpleTrigger)trigger);
            ps.setString(8, TTYPE_SIMPLE);
        } else if (trigger instanceof CronTrigger && ((CronTrigger)trigger).hasAdditionalProperties() == false ) {
            //                updateCronTrigger(conn, (CronTrigger)trigger);
            ps.setString(8, TTYPE_CRON);
        } else {
            //                updateBlobTrigger(conn, trigger);
            ps.setString(8, TTYPE_BLOB);
        }
        ps.setBigDecimal(9, new BigDecimal(String.valueOf(trigger
                .getStartTime().getTime())));
        long endTime = 0;
        if (trigger.getEndTime() != null) {
            endTime = trigger.getEndTime().getTime();
        }
        ps.setBigDecimal(10, new BigDecimal(String.valueOf(endTime)));
        ps.setString(11, trigger.getCalendarName());
        ps.setInt(12, trigger.getMisfireInstruction());
        
        ps.setInt(13, trigger.getPriority());
        ps.setBinaryStream(14, bais, len);
        ps.setString(15, trigger.getName());
        ps.setString(16, trigger.getGroup());

        insertResult = ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }

    if (insertResult > 0) {
        deleteTriggerListeners(conn, trigger.getName(), trigger.getGroup());

        String[] trigListeners = trigger.getTriggerListenerNames();
        for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
            insertTriggerListener(conn, trigger, trigListeners[i]);
        }
    }

    return insertResult;
}
 
Example 13
Project: lams   File: StdJDBCDelegate.java   Source Code and License Vote up 5 votes
/**
 * <p>
 * Get the names of all of the triggers in the given state that have
 * misfired - according to the given timestamp.  No more than count will
 * be returned.
 * </p>
 * 
 * @param conn The DB Connection
 * @param count The most misfired triggers to return, negative for all
 * @param resultList Output parameter.  A List of 
 *      <code>{@link org.quartz.utils.Key}</code> objects.  Must not be null.
 *          
 * @return Whether there are more misfired triggers left to find beyond
 *         the given count.
 */
public boolean hasMisfiredTriggersInState(Connection conn, String state1, 
    long ts, int count, List<TriggerKey> resultList) throws SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(SELECT_HAS_MISFIRED_TRIGGERS_IN_STATE));
        ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
        ps.setString(2, state1);
        rs = ps.executeQuery();

        boolean hasReachedLimit = false;
        while (rs.next() && (hasReachedLimit == false)) {
            if (resultList.size() == count) {
                hasReachedLimit = true;
            } else {
                String triggerName = rs.getString(COL_TRIGGER_NAME);
                String groupName = rs.getString(COL_TRIGGER_GROUP);
                resultList.add(triggerKey(triggerName, groupName));
            }
        }
        
        return hasReachedLimit;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }
}
 
Example 14
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 5 votes
public static void inserirProduto(Produto produto) throws SQLException, ClassNotFoundException {
    String sql = "INSERT INTO produto (nome, descricao, "
            + "vl_compra, vl_venda, categoria, dt_cadastro)"
            + " VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, produto.getNome());
        preparedStatement.setString(2, produto.getDescricao());
        preparedStatement.setBigDecimal(3, produto.getValorCompra());
        preparedStatement.setBigDecimal(4, produto.getValorVenda());
        preparedStatement.setString(5, produto.getCategoria());

        preparedStatement.execute();
    } finally {
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }
}
 
Example 15
Project: lams   File: StdJDBCDelegate.java   Source Code and License Vote up 5 votes
/**
 * <p>
 * Insert a fired trigger.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @param trigger
 *          the trigger
 * @param state
 *          the state that the trigger should be stored in
 * @return the number of rows inserted
 */
public int insertFiredTrigger(Connection conn, OperableTrigger trigger,
        String state, JobDetail job) throws SQLException {
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(rtp(INSERT_FIRED_TRIGGER));
        ps.setString(1, trigger.getFireInstanceId());
        ps.setString(2, trigger.getKey().getName());
        ps.setString(3, trigger.getKey().getGroup());
        ps.setString(4, instanceId);
        ps.setBigDecimal(5, new BigDecimal(String.valueOf(System.currentTimeMillis())));
        ps.setBigDecimal(6, new BigDecimal(String.valueOf(trigger.getNextFireTime().getTime())));
        ps.setString(7, state);
        if (job != null) {
            ps.setString(8, trigger.getJobKey().getName());
            ps.setString(9, trigger.getJobKey().getGroup());
            setBoolean(ps, 10, job.isConcurrentExectionDisallowed());
            setBoolean(ps, 11, job.requestsRecovery());
        } else {
            ps.setString(8, null);
            ps.setString(9, null);
            setBoolean(ps, 10, false);
            setBoolean(ps, 11, false);
        }
        ps.setInt(12, trigger.getPriority());

        return ps.executeUpdate();
    } finally {
        closeStatement(ps);
    }
}
 
Example 16
Project: lams   File: IdentifierGeneratorHelper.java   Source Code and License Vote up 4 votes
public void bind(PreparedStatement preparedStatement, int position) throws SQLException {
	preparedStatement.setBigDecimal( position, new BigDecimal( value ) );
}
 
Example 17
Project: lams   File: IdentifierGeneratorHelper.java   Source Code and License Vote up 4 votes
public void bind(PreparedStatement preparedStatement, int position) throws SQLException {
	preparedStatement.setBigDecimal( position, value );
}
 
Example 18
Project: lams   File: PointbaseDelegate.java   Source Code and License Vote up 4 votes
@Override           
public int updateTrigger(Connection conn, OperableTrigger trigger, String state,
        JobDetail jobDetail) throws SQLException, IOException {

    ByteArrayOutputStream baos = serializeJobData(trigger.getJobDataMap());
    int len = baos.toByteArray().length;
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            
    PreparedStatement ps = null;

    int insertResult = 0;


    try {
        ps = conn.prepareStatement(rtp(UPDATE_TRIGGER));
            
        ps.setString(1, trigger.getJobKey().getName());
        ps.setString(2, trigger.getJobKey().getGroup());
        ps.setString(3, trigger.getDescription());
        long nextFireTime = -1;
        if (trigger.getNextFireTime() != null) {
            nextFireTime = trigger.getNextFireTime().getTime();
        }
        ps.setBigDecimal(4, new BigDecimal(String.valueOf(nextFireTime)));
        long prevFireTime = -1;
        if (trigger.getPreviousFireTime() != null) {
            prevFireTime = trigger.getPreviousFireTime().getTime();
        }
        ps.setBigDecimal(5, new BigDecimal(String.valueOf(prevFireTime)));
        ps.setString(6, state);
        
        TriggerPersistenceDelegate tDel = findTriggerPersistenceDelegate(trigger);
        
        String type = TTYPE_BLOB;
        if(tDel != null)
            type = tDel.getHandledTriggerTypeDiscriminator();

        ps.setString(7, type);
        
        ps.setBigDecimal(8, new BigDecimal(String.valueOf(trigger
                .getStartTime().getTime())));
        long endTime = 0;
        if (trigger.getEndTime() != null) {
            endTime = trigger.getEndTime().getTime();
        }
        ps.setBigDecimal(9, new BigDecimal(String.valueOf(endTime)));
        ps.setString(10, trigger.getCalendarName());
        ps.setInt(11, trigger.getMisfireInstruction());
        
        ps.setInt(12, trigger.getPriority());
        ps.setBinaryStream(13, bais, len);
        ps.setString(14, trigger.getKey().getName());
        ps.setString(15, trigger.getKey().getGroup());

        insertResult = ps.executeUpdate();
        
        if(tDel == null)
            updateBlobTrigger(conn, trigger);
        else
            tDel.updateExtendedTriggerProperties(conn, trigger, state, jobDetail);
        
    } finally {
        closeStatement(ps);
    }

    return insertResult;
}
 
Example 19
Project: sample.daytrader3   File: TradeDirect.java   Source Code and License Vote up 4 votes
/**
    * @see TradeServices#register(String, String, String, String, String,
    *      String, BigDecimal, boolean)
    */

public AccountDataBean register(String userID, String password,
		String fullname, String address, String email, String creditcard,
		BigDecimal openBalance) throws Exception {

	AccountDataBean accountData = null;
	Connection conn = null;
	try {
		if (Log.doTrace())
			Log.traceEnter("TradeDirect:register - inSession("
					+ this.inSession + ")");

		conn = getConn();
		PreparedStatement stmt = getStatement(conn, createAccountSQL);

		Integer accountID = KeySequenceDirect.getNextID(conn, "account",
				inSession, getInGlobalTxn());
		BigDecimal balance = openBalance;
		Timestamp creationDate = new Timestamp(System.currentTimeMillis());
		Timestamp lastLogin = creationDate;
		int loginCount = 0;
		int logoutCount = 0;

		stmt.setInt(1, accountID.intValue());
		stmt.setTimestamp(2, creationDate);
		stmt.setBigDecimal(3, openBalance);
		stmt.setBigDecimal(4, balance);
		stmt.setTimestamp(5, lastLogin);
		stmt.setInt(6, loginCount);
		stmt.setInt(7, logoutCount);
		stmt.setString(8, userID);
		stmt.executeUpdate();
		stmt.close();

		stmt = getStatement(conn, createAccountProfileSQL);
		stmt.setString(1, userID);
		stmt.setString(2, password);
		stmt.setString(3, fullname);
		stmt.setString(4, address);
		stmt.setString(5, email);
		stmt.setString(6, creditcard);
		stmt.executeUpdate();
		stmt.close();

		commit(conn);

		accountData = new AccountDataBean(accountID, loginCount,
				logoutCount, lastLogin, creationDate, balance, openBalance,
				userID);
		if (Log.doTrace())
			Log.traceExit("TradeDirect:register");
	} catch (Exception e) {
		Log.error("TradeDirect:register -- error registering new user", e);
	} finally {
		releaseConn(conn);
	}
	return accountData;
}
 
Example 20
Project: lams   File: StdJDBCDelegate.java   Source Code and License Vote up 4 votes
/**
 * <p>
 * Insert the base trigger data.
 * </p>
 * 
 * @param conn
 *          the DB Connection
 * @param trigger
 *          the trigger to insert
 * @param state
 *          the state that the trigger should be stored in
 * @return the number of rows inserted
 */
public int insertTrigger(Connection conn, OperableTrigger trigger, String state,
        JobDetail jobDetail) throws SQLException, IOException {

    ByteArrayOutputStream baos = null;
    if(trigger.getJobDataMap().size() > 0) {
        baos = serializeJobData(trigger.getJobDataMap());
    }
    
    PreparedStatement ps = null;

    int insertResult = 0;

    try {
        ps = conn.prepareStatement(rtp(INSERT_TRIGGER));
        ps.setString(1, trigger.getKey().getName());
        ps.setString(2, trigger.getKey().getGroup());
        ps.setString(3, trigger.getJobKey().getName());
        ps.setString(4, trigger.getJobKey().getGroup());
        ps.setString(5, trigger.getDescription());
        if(trigger.getNextFireTime() != null)
            ps.setBigDecimal(6, new BigDecimal(String.valueOf(trigger
                    .getNextFireTime().getTime())));
        else
            ps.setBigDecimal(6, null);
        long prevFireTime = -1;
        if (trigger.getPreviousFireTime() != null) {
            prevFireTime = trigger.getPreviousFireTime().getTime();
        }
        ps.setBigDecimal(7, new BigDecimal(String.valueOf(prevFireTime)));
        ps.setString(8, state);
        
        TriggerPersistenceDelegate tDel = findTriggerPersistenceDelegate(trigger);
        
        String type = TTYPE_BLOB;
        if(tDel != null)
            type = tDel.getHandledTriggerTypeDiscriminator();
        ps.setString(9, type);
        
        ps.setBigDecimal(10, new BigDecimal(String.valueOf(trigger
                .getStartTime().getTime())));
        long endTime = 0;
        if (trigger.getEndTime() != null) {
            endTime = trigger.getEndTime().getTime();
        }
        ps.setBigDecimal(11, new BigDecimal(String.valueOf(endTime)));
        ps.setString(12, trigger.getCalendarName());
        ps.setInt(13, trigger.getMisfireInstruction());
        setBytes(ps, 14, baos);
        ps.setInt(15, trigger.getPriority());
        
        insertResult = ps.executeUpdate();
        
        if(tDel == null)
            insertBlobTrigger(conn, trigger);
        else
            tDel.insertExtendedTriggerProperties(conn, trigger, state, jobDetail);
        
    } finally {
        closeStatement(ps);
    }

    return insertResult;
}