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.server.plugin.admin.dao.UserDAO.java

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

    Connection conn = null;/*from   w w w .ja  v  a2s. c  om*/
    PreparedStatement stmt = null;
    try {
        // JDBC stuff
        DataSource ds = UserDatabase.getDataSource();

        conn = ds.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) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return result;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.user.MMXUsersResourceTest.java

@AfterClass
public static void cleanupDatabase() {
    final String statementStr1 = "DELETE FROM mmxApp WHERE appName LIKE '%" + "usersresourcetestapp" + "%'";
    final String statementStr3 = "DELETE FROM ofUser WHERE username LIKE '%" + "MMXUsersResourceTestUser"
            + "%'";
    final String statementStr4 = "DELETE FROM mmxTag WHERE tagname LIKE '%" + "tag" + "%'";

    Connection conn = null;/*from  w  ww  .j a va 2 s . c om*/
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt3 = null;
    PreparedStatement pstmt4 = null;

    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt1 = conn.prepareStatement(statementStr1);
        pstmt3 = conn.prepareStatement(statementStr3);
        pstmt4 = conn.prepareStatement(statementStr4);
        pstmt1.execute();
        pstmt3.execute();
        pstmt4.execute();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : {}", e);
    } finally {
        CloseUtil.close(LOGGER, pstmt1, conn);
        CloseUtil.close(LOGGER, pstmt3);
        CloseUtil.close(LOGGER, pstmt4);
    }
}

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

/**
 * deletes host system//from w  w  w. j a  v  a2 s.c  o m
 *
 * @param hostSystemId host system id
 */
public static void deleteSystem(Long hostSystemId) {

    Connection con = null;

    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("delete from system where id=?");
        stmt.setLong(1, hostSystemId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

From source file:com.wso2.raspberrypi.Util.java

public static void clearAllRaspberryPis() throws RaspberryPiException {
    List<RaspberryPi> raspberryPis = getRaspberryPis(null);
    for (RaspberryPi raspberryPi : raspberryPis) {
        if (raspberryPi.getReservedFor() != null && !raspberryPi.getReservedFor().isEmpty()) {
            throw new RaspberryPiException("Cannot clear Raspberry Pis because some are reserved");
        }//ww  w .  j a  v  a 2 s.  c  om
    }
    System.out.println("Removing all Raspberry Pis...");
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("DELETE FROM RASP_PI");
        prepStmt.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.jernejerin.traffic.helper.TripOperations.java

/**
 * Truncate table.//w w w. j  a va  2s .c o  m
 *
 * @param table table to truncate
 */
public static void truncateTable(String table) {
    //        LOGGER.log(Level.INFO, "Started inserting trip into DB for trip = " +
    //                trip.toString() + " from thread = " + Thread.currentThread());
    PreparedStatement truncateTable = null;
    Connection conn = null;
    try {
        // first we need to get connection from connection pool
        conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:taxi");

        // setting up prepared statement
        truncateTable = conn.prepareStatement("truncate " + table);
        truncateTable.execute();
    } catch (SQLException e) {
        LOGGER.log(Level.SEVERE,
                "Problem when truncating table = " + table + " from thread = " + Thread.currentThread());
    } finally {
        try {
            if (truncateTable != null)
                truncateTable.close();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Problem with closing prepared statement for truncating table = " + table
                    + " from thread = " + Thread.currentThread());
        }
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Problem with closing connection from thread = " + Thread.currentThread());
        }
        //            LOGGER.log(Level.INFO, "Finished inserting ticket into DB for for ticket = " +
        //                    trip + " from thread = " + Thread.currentThread());
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static void deleteRaspberryPi(String mac) {
    System.out.println("Removing Raspberry Pi: " + mac);
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    try {//from  ww  w. jav a2s . c  o m
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("DELETE FROM RASP_PI WHERE mac='" + mac + "'");
        prepStmt.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static void updateKeyValuePair(String key, String value) {
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    try {//w  w  w .j  av a 2  s  . c om
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("UPDATE KV_PAIR SET v='" + value + "' where k='" + key + "'");
        prepStmt.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static void releasePi(String macAddress) {
    System.out.println("Releasing RPi " + macAddress);
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    try {// w  w w  .j av  a 2 s  .  c  om
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("UPDATE RASP_PI SET owner='' where mac='" + macAddress + "'");
        prepStmt.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.us.test.H2Helper.java

public static void laodData2H2Db(File ips, String url, String uname, String upasswd)
        throws IOException, SQLException {
    List<String> ipsegma = FileUtils.readLines(ips, "GBK");

    JdbcConnectionPool cp = JdbcConnectionPool.create(url, uname, upasswd);
    Connection conn = cp.getConnection();
    String sql = "insert into ips values(?,?,?,?,?,?)";
    int i = 0;//from   w ww. ja va 2s .  c  o  m
    for (String ip : ipsegma) {
        PreparedStatement statement = conn.prepareStatement(sql);
        String[] ipary = ip.split("\\s+");
        if (ipary.length < 2 || ipary[0].indexOf(".") < 0)
            continue;
        statement.setString(1, ip2Long(ipary[0].trim()));
        statement.setString(2, ip2Long(ipary[1].trim()));
        statement.setString(3, ipary[1].trim());
        statement.setString(4, ipary[1].trim());
        statement.setString(5, ipary.length <= 2 ? "" : ipary[2].trim());
        statement.setString(6, ipary.length <= 3 ? "" : ipary[3].trim());
        System.out.println(i++ + ":" + statement.execute());

    }
    conn.close();
    cp.dispose();
}

From source file:com.concursive.connect.web.modules.common.social.rating.dao.Rating.java

/**
 * Deletes references from the specified table when the object is being deleted
 *
 * @param db/*w  w w  .  ja  va 2 s  .  co m*/
 * @param objectId
 * @param table
 * @param uniqueField
 * @throws SQLException
 */
public static void delete(Connection db, int objectId, String table, String uniqueField) throws SQLException {
    PreparedStatement pst = db
            .prepareStatement("DELETE FROM " + table + "_rating " + "WHERE " + uniqueField + " = ? ");
    pst.setInt(1, objectId);
    pst.execute();
    pst.close();
}