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:at.becast.youploader.database.SQLite.java

public static Boolean setUploadFinished(int upload_id, Status Status) {
    PreparedStatement prest = null;
    String sql = "UPDATE `uploads` SET `status`=?,`url`=?,`uploaded`=`lenght` WHERE `id`=?";
    try {/*from   w  ww. j a v  a  2  s  .  co  m*/
        prest = c.prepareStatement(sql);
        prest.setString(1, Status.toString());
        prest.setString(2, "");
        prest.setInt(3, upload_id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error marking upload as finished", e);
        return false;
    }
}

From source file:com.keybox.manage.db.SessionAuditDB.java

/**
 * insert new terminal history for user/*w w  w .j  av  a2  s.co m*/
 *
 * @param con           DB connection
 * @param sessionOutput output from session terminals
 * @return session id
 */
public static void insertTerminalLog(Connection con, SessionOutput sessionOutput) {

    try {

        if (sessionOutput != null && sessionOutput.getSessionId() != null
                && sessionOutput.getInstanceId() != null && sessionOutput.getOutput() != null
                && !sessionOutput.getOutput().toString().equals("")) {
            //insert
            PreparedStatement stmt = con.prepareStatement(
                    "insert into terminal_log (session_id, instance_id, system_id, output) values(?,?,?,?)");
            stmt.setLong(1, sessionOutput.getSessionId());
            stmt.setLong(2, sessionOutput.getInstanceId());
            stmt.setLong(3, sessionOutput.getId());
            stmt.setString(4, sessionOutput.getOutput().toString());
            stmt.execute();
            DBUtils.closeStmt(stmt);
        }

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

}

From source file:com.keybox.manage.db.PublicKeyDB.java

/**
 * deletes public key/*w  ww.j a v  a  2s  .  c o m*/
 *
 * @param publicKeyId key id
 * @param userId  user id
 */
public static void deletePublicKey(Long publicKeyId, Long userId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con
                .prepareStatement("delete from public_keys where id=? and user_id=? and enabled=true");
        stmt.setLong(1, publicKeyId);
        stmt.setLong(2, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}

From source file:at.becast.youploader.database.SQLite.java

public static Boolean startUpload(int id, long progress) {
    PreparedStatement prest = null;
    String sql = "UPDATE `uploads` SET `status`=?,`uploaded`=? WHERE `id`=?";
    try {//from  www.jav  a2s  .  c om
        prest = c.prepareStatement(sql);
        prest.setString(1, UploadManager.Status.UPLOADING.toString());
        prest.setLong(2, progress);
        prest.setInt(3, id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error starting upload", e);
        return false;
    }
}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Just uninstalls a single .jar file without deletion.
 * Eats any errors./*w w  w. j ava 2s .c  om*/
 */
public static void uninstallJar(String jarFile) {
    PreparedStatement ps = null;
    try {
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        String name = jarFile.replaceAll("\\.jar", "");
        String query = "DROP JAR localdb.sys_network.\"" + name + "\" OPTIONS(1) CASCADE";
        ps = conn.prepareStatement(query);
        ps.execute();
    } catch (Throwable e) {
        //munch
    } finally {
        try {
            if (ps != null)
                ps.close();
        } catch (SQLException e) {
            //pass
        }
    }
}

From source file:com.firewallid.util.FISQL.java

public static String getFirstFieldInsertIfNotExist(Connection conn, String tableName, String field,
        Map<String, String> fields) throws SQLException {
    /* Query *///from w  w w. j av  a2  s. com
    String query = "SELECT " + field + " FROM " + tableName + " WHERE "
            + Joiner.on(" = ? AND ").join(fields.keySet()) + " = ?";

    /* Execute */
    PreparedStatement pst = conn.prepareStatement(query);
    int i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    ResultSet executeQuery = pst.executeQuery();
    if (executeQuery.next()) {
        return executeQuery.getString(field);
    }

    /* Row is not exists. Insert */
    query = "INSERT INTO " + tableName + " (" + Joiner.on(", ").join(fields.keySet()) + ") VALUES ("
            + StringUtils.repeat("?, ", fields.size() - 1) + "?)";
    pst = conn.prepareStatement(query);
    i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    if (pst.execute()) {
        return null;
    }
    return getFirstFieldInsertIfNotExist(conn, tableName, field, fields);
}

From source file:edu.ucsb.nceas.MCTestCase.java

protected static Vector<Hashtable<String, Object>> dbSelect(String sqlStatement, String methodName)
        throws SQLException {
    Vector<Hashtable<String, Object>> resultVector = new Vector<Hashtable<String, Object>>();

    DBConnectionPool connPool = DBConnectionPool.getInstance();
    DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
    int serialNumber = dbconn.getCheckOutSerialNumber();

    PreparedStatement pstmt = null;

    debug("Selecting from db: " + sqlStatement);
    pstmt = dbconn.prepareStatement(sqlStatement);
    pstmt.execute();

    ResultSet resultSet = pstmt.getResultSet();
    ResultSetMetaData rsMetaData = resultSet.getMetaData();
    int numColumns = rsMetaData.getColumnCount();
    debug("Number of data columns: " + numColumns);
    while (resultSet.next()) {
        Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
        for (int i = 1; i <= numColumns; i++) {
            if (resultSet.getObject(i) != null) {
                hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
            }//w w w .j  a  v a  2 s.  com
        }
        debug("adding data row to result vector");
        resultVector.add(hashTable);
    }

    resultSet.close();
    pstmt.close();
    DBConnectionPool.returnDBConnection(dbconn, serialNumber);

    return resultVector;
}

From source file:at.becast.youploader.database.SQLite.java

public static int saveTemplate(Template template) throws SQLException, IOException {
    PreparedStatement prest = null;
    ObjectMapper mapper = new ObjectMapper();
    String sql = "INSERT INTO `templates` (`name`, `data`) " + "VALUES (?,?)";
    prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    prest.setString(1, template.getName());
    prest.setString(2, mapper.writeValueAsString(template));
    prest.execute();
    ResultSet rs = prest.getGeneratedKeys();
    prest.close();//from  w w  w .  ja v a2s . c  om
    if (rs.next()) {
        int id = rs.getInt(1);
        rs.close();
        return id;
    } else {
        return -1;
    }
}

From source file:com.keybox.manage.db.UserDB.java

/**
 * updates existing user/*from w w  w .  j a  va 2 s .  c o  m*/
 * @param user user object
 */
public static void updateUserNoCredentials(User user) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(
                "update users set first_nm=?, last_nm=?, email=?, username=?, user_type=? where id=?");
        stmt.setString(1, user.getFirstNm());
        stmt.setString(2, user.getLastNm());
        stmt.setString(3, user.getEmail());
        stmt.setString(4, user.getUsername());
        stmt.setString(5, user.getUserType());
        stmt.setLong(6, user.getId());
        stmt.execute();
        DBUtils.closeStmt(stmt);
        if (User.ADMINISTRATOR.equals(user.getUserType())) {
            PublicKeyDB.deleteUnassignedKeysByUser(con, user.getId());
        }

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}

From source file:com.tethrnet.manage.db.UserDB.java

/**
 * updates existing user/* w  ww.  ja  v  a  2 s. c om*/
 * @param user user object
 */
public static void updateUserCredentials(User user) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        String salt = EncryptionUtil.generateSalt();
        PreparedStatement stmt = con.prepareStatement(
                "update users set  email=?, username=?, user_type=?, password=?, salt=? where id=?");
        stmt.setString(1, user.getEmail());
        stmt.setString(2, user.getUsername());
        stmt.setString(3, user.getUserType());
        stmt.setString(4, EncryptionUtil.hash(user.getPassword() + salt));
        stmt.setString(5, salt);
        stmt.setLong(6, user.getId());
        stmt.execute();
        DBUtils.closeStmt(stmt);
        if (User.ADMINISTRATOR.equals(user.getUserType())) {
            PublicKeyDB.deleteUnassignedKeysByUser(con, user.getId());
        }

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}