Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Delete a user/*from   www.j av a  2 s .  c o m*/
 * 
 * @param userName The username of the user to delete.
 * @return true if the user was deleted successfully, false otherwise
 */
public static boolean deleteUser(String userName) {
    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement deleteUser = conn.prepareStatement(DELETE_USER);

            deleteUser.setString(1, userName); // Set parameters
            deleteUser.executeUpdate();

            deleteUser.close();
            conn.close(); // Close statement and connection

            return true;
        }

        catch (Exception e) {
            MetaDbHelper.logEvent(e);
        }
    }
    return false;
}

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

/**
 * updates host system record/*from  w  w  w.  ja va  2  s .  c o  m*/
 *
 * @param hostSystem host system object
 */
public static void updateSystem(HostSystem hostSystem) {

    Connection con = null;

    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement(
                "update system set display_nm=?, user=?, host=?, port=?, authorized_keys=?, status_cd=?  where id=?");
        stmt.setString(1, hostSystem.getDisplayNm());
        stmt.setString(2, hostSystem.getUser());
        stmt.setString(3, hostSystem.getHost());
        stmt.setInt(4, hostSystem.getPort());
        stmt.setString(5, hostSystem.getAuthorizedKeys());
        stmt.setString(6, hostSystem.getStatusCd());
        stmt.setLong(7, hostSystem.getId());
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

From source file:database.HashTablesTools.java

private static int enterInPDBfilesHashDB(Connection connexion, String hash, String fourLetterCode,
        String tableName, char[] chainType, char[] chainName, String sequence) {
    try {//from  w  ww. jav a2  s . c o  m
        String insertTableSQL = "INSERT INTO " + tableName + " "
                + "(pdbfilehash, fourLettercode, chainId, chainType, sequenceString) VALUES" + "(?,?,?,?,?)";
        PreparedStatement preparedStatement = connexion.prepareStatement(insertTableSQL);
        preparedStatement.setString(1, hash);
        preparedStatement.setString(2, String.valueOf(fourLetterCode));
        preparedStatement.setString(3, String.valueOf(chainName));
        preparedStatement.setString(4, String.valueOf(chainType));
        preparedStatement.setString(5, sequence);

        int ok = preparedStatement.executeUpdate();
        preparedStatement.close();
        System.out.println(ok + " raw created " + String.valueOf(fourLetterCode) + "  "
                + String.valueOf(chainName) + "  " + String.valueOf(chainType)); // + " " + sequence);
        return 1;
    } catch (SQLException e1) {
        System.out.println("Failed to enter entry in " + tableName + " table ");
        return 0;
    }
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.topics.MMXTopicsItemsResourceTest.java

@AfterClass
public static void cleanupDatabase() {
    final String statementStr1 = "DELETE FROM mmxApp WHERE appName LIKE '%" + "mmxtopicstagsresourcetesttopic"
            + "%'";
    final String statementStr2 = "DELETE FROM ofPubsubItem where serviceID = ? AND nodeID = ?";
    Connection conn = null;// w ww. java  2s  . c  o  m
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;

    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt1 = conn.prepareStatement(statementStr1);
        pstmt1.executeUpdate();
        pstmt2 = conn.prepareStatement(statementStr2);
        pstmt2.setString(1, SERVICE_ID);
        pstmt2.setString(2, getNodeId());
        pstmt2.executeUpdate();

    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : caught exception cleaning ofPubsubItem");
    } finally {
        CloseUtil.close(LOGGER, pstmt2, conn);
    }
}

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   www  .  j  ava2s  . c  o 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:module.entities.NameFinder.DB.java

/**
 * Update the activity log/*from  w w  w. j  a v a2  s .c  o  m*/
 *
 * @param endTime
 * @param status_id
 * @param regexerId
 * @param obj
 * @throws java.sql.SQLException
 */
public static void UpdateLogRegexFinder(long endTime, int regexerId, JSONObject obj) throws SQLException {
    String updateCrawlerStatusSql = "UPDATE log.activities SET " + "end_date = ?, status_id = ?, message = ?"
            + "WHERE id = ?";
    PreparedStatement prepUpdStatusSt = connection.prepareStatement(updateCrawlerStatusSql);
    prepUpdStatusSt.setTimestamp(1, new java.sql.Timestamp(endTime));
    prepUpdStatusSt.setInt(2, 2);
    prepUpdStatusSt.setString(3, obj.toJSONString());
    prepUpdStatusSt.setInt(4, regexerId);
    prepUpdStatusSt.executeUpdate();
    prepUpdStatusSt.close();
}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Create a new user// w  w w  . ja v  a2s.  c  om
 * 
 * @param userName The username for the new user.
 * @param password The password for the new user.
 * @param type The type for the new user. ("admin" or "worker")
 * @return true if the user is added successfully, false otherwise
 */
public static boolean createUser(String userName, String password, String type, String authType) {
    if (MetaDbHelper.userExists(userName)) //duplicate user
        return false;

    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement createUser = conn.prepareStatement(CREATE_USER);

            createUser.setString(1, userName);
            createUser.setString(2, encryptPassword(password));
            createUser.setString(3, type);
            createUser.setString(4, authType);
            createUser.setLong(5, 0);

            createUser.executeUpdate();

            createUser.close();
            conn.close();
            return true;

        } catch (Exception e) {
            MetaDbHelper.logEvent(e);
        }
    }
    return false;
}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Update the user type of a user./*w  w  w  .jav  a2s  .c  o  m*/
 * 
 * @param userName The username of the user to update.
 * @param newUserType The new user type for the user to update.
 * @return true if the user's user type is updated successfully, false otherwise
 */
public static boolean updateUserType(String userName, String newUserType) {
    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement updateUser = conn.prepareStatement(UPDATE_USER_TYPE);

            updateUser.setString(1, newUserType);
            updateUser.setString(2, userName);
            updateUser.executeUpdate();

            updateUser.close();
            conn.close(); // Close statement and connection

            return true;

        } catch (Exception e) {
            MetaDbHelper.logEvent(e);

        }
    }
    return false;
}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Update the authentication type of a user.
 * //from www  .  ja  v  a 2  s . c om
 * @param userName The username of the user to update.
 * @param newAuthType The new authentication type for the user to update.
 * @return true if the user's authentication type is updated successfully, false otherwise
 */
public static boolean updateAuthType(String userName, String newAuthType) {
    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement updateUser = conn.prepareStatement(UPDATE_AUTH_TYPE);

            updateUser.setString(1, newAuthType);
            updateUser.setString(2, userName);
            updateUser.executeUpdate();

            updateUser.close();
            conn.close(); // Close statement and connection

            return true;

        } catch (Exception e) {
            MetaDbHelper.logEvent(e);

        }
    }
    return false;
}

From source file:module.entities.NameFinder.DB.java

public static void insertDemocracitConsultationMinister(TreeMap<Integer, String> consultationCountersign,
        TreeMap<Integer, String> consultationCounterrole) throws SQLException {
    try {//from ww w . j a v a 2 s  .  c  o  m
        String sql = "INSERT INTO consultations_ner "
                + "(id,countersigned_name, countersigned_position) VALUES " + "(?,?,?)";
        PreparedStatement prepStatement = connection.prepareStatement(sql);
        for (int consId : consultationCountersign.keySet()) {
            prepStatement.setInt(1, consId);
            prepStatement.setString(2, consultationCountersign.get(consId));
            if (consultationCounterrole.get(consId) != null) {
                prepStatement.setString(3, consultationCounterrole.get(consId));
            } else {
                prepStatement.setString(3, "");
            }
            prepStatement.addBatch();
        }
        prepStatement.executeBatch();
        prepStatement.close();
    } catch (BatchUpdateException ex) {
        //            ex.printStackTrace();
        System.out.println(ex.getNextException());
    }
}