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:Main.java

/**
 * <p>//ww  w.ja v a  2 s. c om
 * Execute a DDL statement
 * </p>
 */
public static void executeDDL(String ddl) throws SQLException {
    Connection conn = getLocalConnection();

    // now register the function
    print(ddl);
    try {
        PreparedStatement createStatement = conn.prepareStatement(ddl);

        createStatement.execute();
        createStatement.close();
    } catch (SQLException t) {
        SQLException s = new SQLException("Could not execute DDL:\n" + ddl);

        s.setNextException(t);

        throw s;
    }
}

From source file:de.unibayreuth.bayceer.bayeos.xmlrpc.ConnectionPool.java

/** Sets the user id for the current session
 *///w w  w  .  jav  a  2s . c o  m
private static void setUserId(Connection con, int Id) throws SQLException {
    PreparedStatement st = con.prepareStatement("select set_userid(?)");
    st.setInt(1, Id);
    st.execute();
    st.close();
}

From source file:Main.java

/**
 * <p>/*from w  w w. jav  a2 s  .c o  m*/
 * Execute a DDL statement to drop an object if it exists. Swallow exceptions.
 * </p>
 */
public static void dropObject(String objectType, String objectName, boolean objectIfMissing)
        throws SQLException {
    String dropDDL = "drop " + objectType + " " + objectName;
    Connection conn = getLocalConnection();

    // Drop the object if it does exist. Swallow exception if it doesn't.
    print(dropDDL);
    try {
        PreparedStatement dropStatement = conn.prepareStatement(dropDDL);

        dropStatement.execute();
        dropStatement.close();
    } catch (SQLException s) {
        if (objectIfMissing) {
            throw s;
        }
    }
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the insert statement, get the first generated key as a long, and close the statement.
 * //from  w  w  w  .ja v a2 s.c  om
 * @param stmt
 *            must have been created with {@link Statement#RETURN_GENERATED_KEYS} and already
 *            have parameters set
 * @return 0 if the statement did not generate any keys
 */
public static long firstLongKey(PreparedStatement stmt) throws SQLException {
    stmt.execute();
    ResultSet rs = stmt.getGeneratedKeys();
    long l = rs.next() ? rs.getLong(1) : 0L;
    stmt.close();
    return l;
}

From source file:Main.java

public static long[] getEntryExit(Double id, Calendar date, Connection con, PreparedStatement stmt)
        throws Exception {
    stmt.setDate(1, new java.sql.Date(date.getTimeInMillis()));
    // stmt.setDate(2, new java.sql.Date(date.getTimeInMillis()+1000000));
    stmt.execute();
    ResultSet rs = stmt.getResultSet();
    if (rs != null) {

        if (rs.next()) {
            Timestamp d1 = rs.getTimestamp(1);
            Timestamp d2 = rs.getTimestamp(2);
            if (d1 != null && d2 != null) {
                System.out.println(id + ":" + new SimpleDateFormat("dd/MM/yyyy").format(date.getTime()) + ":"
                        + d1.toString());
                long[] res = new long[] { d1.getTime(), d2.getTime() };
                return res;
            }// ww  w.j  a va  2  s . c o m
        }
        rs.close();
    }
    return null;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.db.UserDAOImplTest.java

@AfterClass
public static void cleanupDatabase() throws Exception {
    final String statementStr = "DELETE FROM ofUser where username like '%" + USER_PREFIX + "%'";
    Connection conn = UnitTestDSProvider.getDataSource().getConnection();
    PreparedStatement pstmt = null;
    ResultSet rs = null;/*  w  w  w. j  a v a2s. c om*/

    try {
        pstmt = conn.prepareStatement(statementStr);
        pstmt.execute();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : {}");
    } finally {
        CloseUtil.close(LOGGER, pstmt, conn);
    }
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.DBTestUtil.java

/**
 * Delete all records from the tables specified in tables array. The deletes are executed in
 * the same order as the entries in the tables array.
 *
 * WARNING: This DELETES data and is not undoable. You need to ensure that you are connected to
 * a test database and not a production database.
 *
 * @param tables//  w w  w  . ja v a 2s  . com
 * @param ds
 */
public static void cleanTables(String[] tables, BasicDataSourceConnectionProvider ds) {
    String template = "DELETE FROM %s";
    try {
        Connection connection = ds.getConnection();
        for (String table : tables) {
            String statement = String.format(template, table);
            PreparedStatement preparedStatement = connection.prepareStatement(statement);
            preparedStatement.execute();
        }
    } catch (SQLException e) {

    }
}

From source file:com.akman.excel.controller.CURDInvoice.java

public static void Delete() {

    Connection conn = Javaconnect.ConnecrDb();
    PreparedStatement pst = null;
    ResultSet rs = null;//from w w  w  .j  av  a 2s .c  om
    try {

        String sql1 = "DELETE  FROM Invoice";
        pst = conn.prepareStatement(sql1);
        pst.execute();

    } catch (SQLException ex) {
        Logger.getLogger(CURDInvoice.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(pst);
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.ec2box.manage.db.UserThemeDB.java

/**
 * saves user theme/*from w  w  w.ja v  a 2s  .c  o  m*/
 * 
 * @param userId object
 */
public static void saveTheme(Long userId, UserSettings theme) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("delete from user_theme where user_id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

        if (StringUtils.isNotEmpty(theme.getPlane()) || StringUtils.isNotEmpty(theme.getTheme())) {

            stmt = con.prepareStatement(
                    "insert into user_theme(user_id, bg, fg, d1, d2, d3, d4, d5, d6, d7, d8, b1, b2, b3, b4, b5, b6, b7, b8) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            stmt.setLong(1, userId);
            stmt.setString(2, theme.getBg());
            stmt.setString(3, theme.getFg());
            //if contains all 16 theme colors insert
            if (theme.getColors() != null && theme.getColors().length == 16) {
                for (int i = 0; i < 16; i++) {
                    stmt.setString(i + 4, theme.getColors()[i]);
                }
                //else set to null
            } else {
                for (int i = 0; i < 16; i++) {
                    stmt.setString(i + 4, null);
                }
            }
            stmt.execute();
            DBUtils.closeStmt(stmt);
        }

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

}

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

/**
 * saves user theme/*from  w  w w  .  j a  va 2 s.  c  om*/
 * 
 * @param userId object
 */
public static void saveTheme(Long userId, UserSettings theme) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("delete from user_theme where user_id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

        if (org.apache.commons.lang.StringUtils.isNotEmpty(theme.getPlane())
                || org.apache.commons.lang.StringUtils.isNotEmpty(theme.getTheme())) {

            stmt = con.prepareStatement(
                    "insert into user_theme(user_id, bg, fg, d1, d2, d3, d4, d5, d6, d7, d8, b1, b2, b3, b4, b5, b6, b7, b8) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            stmt.setLong(1, userId);
            stmt.setString(2, theme.getBg());
            stmt.setString(3, theme.getFg());
            //if contains all 16 theme colors insert
            if (theme.getColors() != null && theme.getColors().length == 16) {
                for (int i = 0; i < 16; i++) {
                    stmt.setString(i + 4, theme.getColors()[i]);
                }
                //else set to null
            } else {
                for (int i = 0; i < 16; i++) {
                    stmt.setString(i + 4, null);
                }
            }
            stmt.execute();
            DBUtils.closeStmt(stmt);
        }

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

}