Example usage for java.sql Connection prepareStatement

List of usage examples for java.sql Connection prepareStatement

Introduction

In this page you can find the example usage for java.sql Connection prepareStatement.

Prototype

PreparedStatement prepareStatement(String sql) throws SQLException;

Source Link

Document

Creates a PreparedStatement object for sending parameterized SQL statements to the database.

Usage

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findAllCoffeeBeverages(ResultSet[] coffeeBeverages) throws SQLException {

    Connection connection = null;
    PreparedStatement statement = null;

    connection = DriverManager.getConnection("jdbc:default:connection");
    String sql = "SELECT * FROM COFFEE_BEVERAGES";
    statement = connection.prepareStatement(sql);
    coffeeBeverages[0] = statement.executeQuery();

}

From source file:com.sql.DocketNotification.java

/**
 * Delete item from docket notification based off of ID
 * //from  w  ww  . ja v  a 2  s  .  c  om
 * @param id Integer
 */
public static void deleteEmailEntry(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM DocketNotifications WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.winit.vms.base.db.mybatis.support.SQLHelp.java

/**
 * //ww w .  j av a  2s. c  o  m
 *
 * @param sql             SQL?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @param dialect         database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(final String sql, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountString(sql);
    logger.debug("Total count SQL [{}] ", count_sql);
    logger.debug("Total count Parameters: {} ", parameterObject);

    DataSource dataSource = mappedStatement.getConfiguration().getEnvironment().getDataSource();
    Connection connection = DataSourceUtils.getConnection(dataSource);
    PreparedStatement countStmt = null;
    ResultSet rs = null;
    try {
        countStmt = connection.prepareStatement(count_sql);
        //Page SQLCount SQL???boundSql
        DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject,
                boundSql);
        handler.setParameters(countStmt);

        rs = countStmt.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        logger.debug("Total count: {}", count);
        return count;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } finally {
            try {
                if (countStmt != null) {
                    countStmt.close();
                }
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        }
    }
}

From source file:com.example.mydtapp.JdbcInputAppTest.java

public static void insertEventsInTable(int numEvents, int offset) {
    try {// w  w w  . j  a  v a 2 s. c  o  m
        Connection con = DriverManager.getConnection(URL);
        String insert = "insert into " + TABLE_NAME + " values (?,?,?)";
        PreparedStatement stmt = con.prepareStatement(insert);
        for (int i = 0; i < numEvents; i++, offset++) {
            stmt.setInt(1, offset);
            stmt.setString(2, "Account_Holder-" + offset);
            stmt.setInt(3, (offset * 1000));
            stmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

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;//from   w  w  w.j a  v a  2  s .c  o  m

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

From source file:com.sql.EmailOutAttachment.java

/**
 * Deletes attachment based off of email ID
 * //from  www . j  av  a  2  s  .co m
 * @param id Integer
 */
public static void deleteAttachmentsForEmail(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM EmailOutAttachment WHERE emailOutID = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

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

public static void Delete() {

    Connection conn = Javaconnect.ConnecrDb();
    PreparedStatement pst = null;
    ResultSet rs = null;/*www .j  a  va2  s .  co m*/
    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.sql.EmailAttachment.java

/**
 * Insert attachement into received email attachment database table.
 * /*from w  ww. ja v a2  s  .  c om*/
 * @param EmailID Integer
 * @param fileName String
 */
public static void insertEmailAttachment(int EmailID, String fileName) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "INSERT INTO EmailAttachment (" + "emailID, " + "fileName " + ") VALUES (" + "?, " + "?)";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, EmailID);
        ps.setString(2, fileName);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.sql.EmailOutInvites.java

/**
 * Deletes email out invite from database after sending.
 * //  w  ww  . j a  v a 2 s  .c  o  m
 * @param id Integer
 */
public static void deleteEmailEntry(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM EmailOutInvites WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.l2jfree.gameserver.model.entity.faction.FactionQuest.java

public static void createFactionQuest(L2Player player, int factionQuestId) {
    Connection con = null;
    try {/*w w  w  .  j a  v  a  2s  .  c  o  m*/
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement(
                "INSERT INTO character_faction_quests (char_id,faction_quest_id) VALUES (?,?)");
        statement.setInt(1, player.getObjectId());
        statement.setInt(2, factionQuestId);
        statement.executeUpdate();
        statement.close();
    } catch (Exception e) {
        _log.warn("could not insert char faction quest:", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}