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:org.red5.webapps.admin.controllers.service.UserDAO.java

public static boolean addUser(String username, String hashedPassword) {
    boolean result = false;

    Connection conn = null;/*from  www. ja  va  2  s  .  com*/
    PreparedStatement stmt = null;
    try {
        conn = UserDatabase.getConnection();
        //make a statement
        stmt = conn
                .prepareStatement("INSERT INTO APPUSER (username, password, enabled) VALUES (?, ?, 'enabled')");
        stmt.setString(1, username);
        stmt.setString(2, hashedPassword);
        log.debug("Add user: {}", stmt.execute());
        //add role
        stmt = conn.prepareStatement("INSERT INTO APPROLE (username, authority) VALUES (?, 'ROLE_SUPERVISOR')");
        stmt.setString(1, username);
        log.debug("Add role: {}", stmt.execute());
        //
        result = true;
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            UserDatabase.recycle(conn);
        }
    }
    return result;
}

From source file:com.wso2telco.util.DbUtil.java

public static String updateRegistrationStatus(String uuid, String status)
        throws SQLException, AuthenticatorException {

    Connection connection;/* ww  w .  ja  v  a2 s . c om*/
    PreparedStatement ps;
    String sql = "UPDATE regstatus SET status = ? WHERE uuid = ?;";
    connection = getConnectDBConnection();
    ps = connection.prepareStatement(sql);
    ps.setString(1, status);
    ps.setString(2, uuid);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
    return uuid;
}

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

/**
 * deletes user/*from w ww  .jav  a 2  s .co  m*/
 * @param userId user id
 */
public static void disableUser(Long userId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("update users set enabled=false where id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * resets shared secret for user//from www. jav a 2 s . c om
 * @param userId user id
 */
public static void resetSharedSecret(Long userId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("update users set otp_secret=null where id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * Delete all Entries from system_map where system has not instance_id "---" 
 *///from  w  w  w .ja  va 2 s .c  o m
public static void deleteAWSSystemProfileEntries() {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(
                "DELETE FROM system_map sm WHERE sm.system_id IN (SELECT s.id FROM system s WHERE s.instance_id NOT LIKE '---')");
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        e.printStackTrace();
    }
    DBUtils.closeConn(con);
}

From source file:com.jmstoolkit.pipeline.plugin.XMLValueTransformerTest.java

/**
 * Starts an embedded Derby database, creates the lookup table and
 * inserts one row for the test. Also creates the source and expected
 * result XML documents. Finally, initializes the XMLValueTransformer.
 * /*from   ww w  .  j  ava  2s . co  m*/
 * @throws Exception on JDBC problems
 */
@BeforeClass
public static void setUpClass() throws Exception {
    // loads the "driver" which apparently means the database is running
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    PreparedStatement ps;
    //Connection connection = DriverManager.getConnection("jdbc:derby:XDB;create=true");     

    EmbeddedDataSource40 dataSource = new EmbeddedDataSource40();
    dataSource.setCreateDatabase("create");
    dataSource.setDatabaseName(DB_NAME);
    Connection connection = dataSource.getConnection();
    ps = connection.prepareStatement(SQL_CREATE_TABLE);
    ps.execute();
    ps = connection.prepareStatement(SQL_INSERT_ROW);
    ps.execute();
    ps.close();
    // Create the transformer
    XFORM = new XMLValueTransformer(dataSource);
    XFORM.setSql(SQL_SELECT);
    XFORM.setSrcPath("/trade/currency");

    // Create the XML SOURCE document
    SOURCE = DocumentHelper.createDocument();
    Element root = SOURCE.addElement("trade");
    root.addElement("currency").addText("XXX");
    // create the expected result document for comparison
    RESULT = DocumentHelper.createDocument();
    root = RESULT.addElement("trade");
    root.addElement("currency").addText("USD");
}

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

/**
 * updates the status table to keep track of key placement status
 *
 * @param con                DB connection
 * @param hostSystem systems for authorized_keys replacement
 * @param userId user id//from www  . j a  va  2 s.co  m
 */
public static void updateSystemStatus(Connection con, HostSystem hostSystem, Long userId) {

    try {

        PreparedStatement stmt = con.prepareStatement("update status set status_cd=? where id=? and user_id=?");
        stmt.setString(1, hostSystem.getStatusCd());
        stmt.setLong(2, hostSystem.getId());
        stmt.setLong(3, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

From source file:com.l2jserver.model.template.NPCTemplateConverter.java

private static Skills fillSkillList(final ObjectFactory factory, ResultSet npcRs, int npcId)
        throws SQLException {
    final Connection conn = npcRs.getStatement().getConnection();
    final Skills skills = factory.createNPCTemplateSkills();

    final PreparedStatement st = conn.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
    st.setInt(1, npcId);/*from w w  w .  j a  v a2 s.  co  m*/
    st.execute();
    final ResultSet rs = st.getResultSet();
    while (rs.next()) {
        Skills.Skill s = factory.createNPCTemplateSkillsSkill();
        s.setId(new SkillTemplateID(rs.getInt("skillid"), null));
        s.setLevel(rs.getInt("level"));
        skills.getSkill().add(s);
    }
    if (skills.getSkill().size() == 0)
        return null;
    return skills;
}

From source file:com.l2jserver.model.template.NPCTemplateConverter.java

private static Droplist fillDropList(final ObjectFactory factory, ResultSet npcRs, int npcId)
        throws SQLException {
    final Connection conn = npcRs.getStatement().getConnection();
    final Droplist drops = factory.createNPCTemplateDroplist();

    final PreparedStatement st = conn.prepareStatement("SELECT * FROM droplist WHERE mobId = ?");
    st.setInt(1, npcId);//  w  w  w  .  j a  v a 2  s.  c o  m
    st.execute();
    final ResultSet rs = st.getResultSet();
    while (rs.next()) {
        final Droplist.Item item = factory.createNPCTemplateDroplistItem();
        item.setId(new ItemTemplateID(rs.getInt("itemId"), null));
        item.setMin(rs.getInt("min"));
        item.setMax(rs.getInt("max"));
        item.setChance(rs.getInt("chance"));
        item.setCategory(getCategory(rs.getInt("category")));
        drops.getItem().add(item);
    }
    if (drops.getItem().size() == 0)
        return null;
    return drops;
}

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

/**
 * Grabbing the list of repos along with their availability
 * status for internal use. //from  ww w .j a  v a2  s . c  om
 */
private static List<RepoInfo> getRepoUrls() throws SQLException {
    List<RepoInfo> repos = new ArrayList<RepoInfo>();

    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String query = "SELECT repo_url FROM localdb.sys_network.repositories " + "ORDER BY repo_url";
    PreparedStatement ps = conn.prepareStatement(query);
    ps.execute();
    ResultSet rs = ps.getResultSet();
    while (rs.next()) {
        String repo = rs.getString(1);
        boolean accessible = true;
        try {
            JSONObject ob = downloadMetadata(repo);
        } catch (SQLException e) {
            if (e.getMessage().equals(URL_TIMEOUT))
                accessible = false;
        }
        repos.add(new RepoInfo(repo, accessible));
    }
    rs.close();
    ps.close();

    return repos;
}