Example usage for java.sql PreparedStatement execute

List of usage examples for java.sql PreparedStatement execute

Introduction

In this page you can find the example usage for java.sql PreparedStatement execute.

Prototype

boolean execute() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

Usage

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownRelationship wko) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String.format(
            "INSERT into %s (id, name, description, updated_by, inverse, preferred) VALUES (?, ?, ?, ?, ?, ?)",
            wko.tableName()));// w w w. j  a v a 2 s .c  o m
    try {
        s.setString(1, wko.id());
        s.setString(2, wko.wkoName());
        s.setString(3, wko.description());
        s.setString(4, WellKnownAgency.CORE.id());
        s.setString(5, wko.inverse().id());
        s.setByte(6, (byte) (wko.preferred() ? 1 : 0));
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert %s", wko), e);
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownInterval wki) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String.format(
            "INSERT into %s (id, name, start_unit, duration_unit, description, updated_by) VALUES (?, ?, ?, ?, ?, ?)",
            wki.tableName()));/*  ww w .  j  a v  a 2s .  c o m*/
    try {
        s.setString(1, wki.id());
        s.setString(2, wki.wkoName());
        s.setString(3, wki.startUnit().id());
        s.setString(4, wki.durationUnit().id());
        s.setString(5, wki.description());
        s.setString(6, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert %s", wki), e);
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

private void createNullInference() throws SQLException {
    PreparedStatement s = connection.prepareStatement(
            "INSERT into ruleform.network_inference (id, premise1, premise2, inference, updated_by) VALUES (?, ?, ?, ?, ?)");
    try {//www  . j  av a 2  s. c om
        s.setString(1, UuidGenerator.toBase64(new UUID(0, 0)));
        s.setString(2, WellKnownRelationship.RELATIONSHIP.id());
        s.setString(3, WellKnownRelationship.RELATIONSHIP.id());
        s.setString(4, WellKnownRelationship.RELATIONSHIP.id());
        s.setString(5, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException("Unable to insert null inference", e);
    }
}

From source file:com.concursive.connect.cms.portal.dao.DashboardPortlet.java

public void insert(Connection db) throws SQLException {
    // Insert the page
    PreparedStatement pst = db.prepareStatement("INSERT INTO project_dashboard_portlet "
            + "(page_id, portlet_id " + (entered != null ? ", entered " : "")
            + (modified != null ? ", modified " : "") + ") VALUES (?, ?" + (entered != null ? ", ? " : "")
            + (modified != null ? ", ? " : "") + ")");
    int i = 0;/* w  w  w.  java 2s  . co  m*/
    pst.setInt(++i, pageId);
    pst.setInt(++i, portletId);
    if (entered != null) {
        pst.setTimestamp(++i, entered);
    }
    if (modified != null) {
        pst.setTimestamp(++i, modified);
    }
    pst.execute();
    pst.close();
    id = DatabaseUtils.getCurrVal(db, "project_dashboard_portlet_page_portlet_id_seq", -1);
}

From source file:ch.elexis.data.Query.java

/**
 * Execute the {@link PreparedStatement} on the database.
 * /*from   www. j a  va  2 s .  com*/
 * @param ps
 * @param values
 * @return
 */
public ArrayList<String> execute(final PreparedStatement ps, final String[] values) {
    try {
        for (int i = 0; i < values.length; i++) {
            ps.setString(i + 1, values[i]);
        }
        if (ps.execute() == true) {
            ArrayList<String> ret = new ArrayList<String>();
            ResultSet res = ps.getResultSet();
            while (res.next()) {
                ret.add(res.getString(1));
            }
            return ret;
        }
    } catch (Exception ex) {
        ElexisStatus status = new ElexisStatus(ElexisStatus.ERROR, CoreHub.PLUGIN_ID, ElexisStatus.CODE_NONE,
                "Fehler beim Ausfhren von " + sql.toString(), ex, ElexisStatus.LOG_ERRORS);
        throw new PersistenceException(status);
    }
    return null;
}

From source file:de.unidue.inf.is.ezdl.dlservices.repository.store.repositories.DBRepository.java

@Override
protected void putAsIs(String oid, StoredDocument document) {
    Connection con = null;// w w w.  j  a v  a 2  s.co m
    PreparedStatement st = null;
    PreparedStatement st2 = null;

    byte[] encoded = encode(document);
    ByteArrayInputStream is = new ByteArrayInputStream(encoded);

    String databaseIdForOid = databaseIdForOid(oid);

    try {
        con = provider.connection();

        st = con.prepareStatement(EXISTS);
        st.setString(1, databaseIdForOid);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            String s = rs.getString(1);
            if (Boolean.valueOf(s)) {
                st2 = con.prepareStatement(UPDATE);
                st2.setBlob(1, is);
                st2.setString(2, databaseIdForOid);
                st2.execute();
            } else {
                getLogger().error("Error while checking if row exists");
            }
        } else {
            st2 = con.prepareStatement(PUT);
            st2.setString(1, databaseIdForOid);
            st2.setBlob(2, is);
            st2.execute();
        }
        con.commit();
    } catch (SQLException e) {
        rollback(con);
        getLogger().error("Error putting " + databaseIdForOid, e);
    } finally {
        ClosingUtils.close(st, st2);
        ClosingUtils.close(con);
    }
}

From source file:net.mindengine.oculus.frontend.service.runs.JdbcTestRunDAO.java

@Override
public Long createTestRunParameter(Long testRunId, String name, String value, boolean isInput)
        throws Exception {
    PreparedStatement ps = getConnection().prepareStatement(
            "insert into test_run_parameters (test_run_id, name, value, type) values (?,?,?,?)");

    ps.setLong(1, testRunId);/*from w  w  w  .j a v a 2s. c o  m*/
    ps.setString(2, name);
    ps.setString(3, value);
    if (isInput) {
        ps.setString(4, "input");
    } else
        ps.setString(4, "output");

    logger.info(ps);
    ps.execute();

    ResultSet rs = ps.getGeneratedKeys();
    if (rs.next()) {
        return rs.getLong(1);
    }

    return null;
}

From source file:com.feedzai.commons.sql.abstraction.engine.impl.H2Engine.java

@Override
public synchronized Long persist(String name, EntityEntry entry, boolean useAutoInc)
        throws DatabaseEngineException {

    ResultSet generatedKeys = null;
    try {/* ww  w.  j a v  a2s  .c o  m*/
        getConnection();

        final MappedEntity me = entities.get(name);

        if (me == null) {
            throw new DatabaseEngineException(String.format("Unknown entity '%s'", name));
        }

        PreparedStatement ps = null;
        if (useAutoInc) {
            ps = entities.get(name).getInsert();
        } else {
            ps = entities.get(name).getInsertWithAutoInc();
        }

        entityToPreparedStatement(me.getEntity(), ps, entry, useAutoInc);
        ps.execute();

        long ret = 0;
        if (useAutoInc) {
            generatedKeys = ps.getGeneratedKeys();
            if (generatedKeys.next()) {
                ret = generatedKeys.getLong(1);
            }
            generatedKeys.close();
        }

        return ret == 0 ? null : ret;
    } catch (Exception ex) {
        throw new DatabaseEngineException("Something went wrong persisting the entity", ex);
    } finally {
        try {
            if (generatedKeys != null) {
                generatedKeys.close();
            }
        } catch (Exception e) {
            logger.trace("Error closing result set.", e);
        }
    }
}

From source file:ke.co.tawi.babblesms.server.persistence.items.messageTemplate.MessageTemplateDAO.java

/**
 *
 *///  w ww.  ja  va  2  s  .c  om
@Override
public boolean putMessageTemplate(MessageTemplate messageTemplate) {
    boolean success = true;

    Connection conn = null;
    PreparedStatement pstmt = null;

    try {
        conn = dbCredentials.getConnection();
        pstmt = conn.prepareStatement(
                "INSERT INTO MessageTemplate (Uuid,title,contents,accountuuid) VALUES (?,?,?,?);");
        pstmt.setString(1, messageTemplate.getUuid());
        pstmt.setString(2, messageTemplate.getTitle());
        pstmt.setString(3, messageTemplate.getContents());
        pstmt.setString(4, messageTemplate.getAccountuuid());

        System.out.println(messageTemplate);

        pstmt.execute();

    } catch (SQLException e) {
        logger.error("SQL Exception when trying to put messageTemplate: " + messageTemplate);
        logger.error(ExceptionUtils.getStackTrace(e));
        success = false;
    } finally {
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return success;
}

From source file:mysql5.MySQL5PlayerDAO.java

/**
 * {@inheritDoc} - Nemiroff// w  ww .ja v a  2  s.  c o  m
 */
@Override
public void setPlayersOffline(final boolean online) {
    DB.insertUpdate("UPDATE players SET online=?", new IUStH() {
        @Override
        public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
            stmt.setBoolean(1, online);
            stmt.execute();
        }
    });
}