Example usage for java.sql PreparedStatement setLong

List of usage examples for java.sql PreparedStatement setLong

Introduction

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

Prototype

void setLong(int parameterIndex, long x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java long value.

Usage

From source file:Main.java

public static Object readJavaObject(Connection conn, long id) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();//from  w ww .  j  a  v  a2 s  .com
    Object object = rs.getObject(1);
    String className = object.getClass().getName();
    rs.close();
    pstmt.close();
    return object;
}

From source file:SerializeJavaObjects_MySQL.java

public static Object readJavaObject(Connection conn, long id) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();//from w w w  .  ja v a  2 s .  c om
    Object object = rs.getObject(1);
    String className = object.getClass().getName();

    rs.close();
    pstmt.close();
    System.out.println("readJavaObject: done de-serializing: " + className);
    return object;
}

From source file:wiki.link.LinkResource.java

public static void insertAll(List<Link> links, DbConnector dbc) {
    BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
        @Override//from   w ww.  jav a2 s.  com
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setLong(1, links.get(i).from);
            preparedStatement.setLong(2, links.get(i).to);
        }

        @Override
        public int getBatchSize() {
            return links.size();
        }
    };

    try {
        dbc.jdbcTemplate.batchUpdate("INSERT INTO links(fromPage, toPage) values(?, ?)", bpss);
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
        BatchUpdateException bue = (BatchUpdateException) e.getCause();
        System.out.println(bue.getNextException());
        System.exit(1);
    }
}

From source file:ImageStringToBlob.java

private static int writeBlobToDb(Connection conn, Long id, Blob dataBlob) throws Exception {
    String sql = "update client_image set contents = ? where image_id = ?";
    PreparedStatement pst = conn.prepareStatement(sql);
    pst.setBlob(1, dataBlob);/*w w  w  .j a v a 2s .co  m*/
    pst.setLong(2, id);

    return pst.executeUpdate();
}

From source file:io.github.chrisbotcom.reminder.commands.Update.java

public static boolean execute(Reminder plugin, CommandSender sender, ReminderRecord reminder) throws Exception {
    if ((reminder.getId() == null) && (reminder.getPlayer() == null) && (reminder.getTag() == null)) {
        throw new ReminderException("Must supply either id or player and/or tag.");
    }//from w  w w  . j  av  a 2  s. c o m

    if ((reminder.getId() != null) && ((reminder.getPlayer() != null) && (reminder.getTag() != null))) {
        throw new ReminderException("Must supply either id or player and/or tag.");
    }

    List<String> setClauses = new ArrayList<>();

    // "rate", "echo", 
    // TODO Consider making <player> and <tag> updatable.
    if (reminder.getId() != null) {
        if (reminder.getPlayer() != null) {
            setClauses.add(String.format("player = '%s'", reminder.getPlayer()));
        }
        if (reminder.getTag() != null) {
            setClauses.add(String.format("tag = '%s'", reminder.getTag()));
        }
    }

    if (reminder.getStart() != null) {
        setClauses.add(String.format("start = %s", reminder.getStart()));
    }

    if (reminder.getMessage() != null) {
        setClauses.add(String.format("message = '%s'", reminder.getMessage()));
    }

    if (reminder.getDelay() != null) {
        setClauses.add(String.format("delay = %s", reminder.getDelay()));
    }

    if (reminder.getRate() != null) {
        setClauses.add(String.format("rate = %s", reminder.getRate()));
    }

    if (reminder.getEcho() != null) {
        setClauses.add(String.format("echo = %s", reminder.getEcho()));
    }

    String sql = String.format(
            "UPDATE reminders SET %s WHERE (? IN(id, 0)) AND (? IN(player, 'EMPTY')) AND (? IN(tag, 'EMPTY'))",
            StringUtils.join(setClauses, ", "));
    PreparedStatement preparedStatement = plugin.db.prepareStatement(sql);
    preparedStatement.setLong(1, reminder.getId() == null ? 0 : reminder.getId());
    preparedStatement.setString(2,
            reminder.getId() != null || reminder.getPlayer() == null ? "EMPTY" : reminder.getPlayer());
    preparedStatement.setString(3,
            reminder.getId() != null || reminder.getTag() == null ? "EMPTY" : reminder.getTag());

    int rows = preparedStatement.executeUpdate();

    //sender.sendMessage(ChatColor.BLUE + String.format("%s", preparedStatement));
    sender.sendMessage(ChatColor.GREEN + String.format("%s record(s) updated.", rows));

    return true;
}

From source file:ch.newscron.encryption.ReferralManagerJUnitTest.java

@AfterClass
public static void tearDownClass() throws SQLException {
    // Delete inserted query
    con = ReferralManager.connect();/*from  w  w  w .ja  v a 2 s. co m*/
    PreparedStatement query = null;
    query = con.prepareStatement("DELETE FROM ShortURL WHERE custId= ? AND shortUrl = ?");
    query.setLong(1, Long.parseLong((String) inviteData.get("custID")));
    query.setString(2, shortURL);
    query.execute();
    ReferralManager.disconnect(con, null);
}

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

/**
 * saves user theme//from w  ww.  j a va 2 s . com
 * 
 * @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. jav  a 2 s .  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 (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);

}

From source file:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java

static void execute(PreparedStatement stmt) throws Exception {
    String dir = "100506-Body???/??Body";

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6564LOG.txt")));
    stmt.setLong(2, 1093);
    stmt.executeUpdate();/*from   w  ww .j  a  v  a2s. c  o m*/

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6565LOG.txt")));
    stmt.setLong(2, 1094);
    stmt.executeUpdate();

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6661LOG.txt")));
    stmt.setLong(2, 1095);
    stmt.executeUpdate();
}

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

/**
 * get user theme//from  w w  w  .j a  v a  2s. c o m
 *
 * @param userId object
 * @return user theme object
 */
public static UserSettings getTheme(Long userId) {

    UserSettings theme = null;
    Connection con = null;
    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            theme = new UserSettings();
            theme.setBg(rs.getString("bg"));
            theme.setFg(rs.getString("fg"));
            if (StringUtils.isNotEmpty(rs.getString("d1"))) {
                String[] colors = new String[16];
                colors[0] = rs.getString("d1");
                colors[1] = rs.getString("d2");
                colors[2] = rs.getString("d3");
                colors[3] = rs.getString("d4");
                colors[4] = rs.getString("d5");
                colors[5] = rs.getString("d6");
                colors[6] = rs.getString("d7");
                colors[7] = rs.getString("d8");
                colors[8] = rs.getString("b1");
                colors[9] = rs.getString("b2");
                colors[10] = rs.getString("b3");
                colors[11] = rs.getString("b4");
                colors[12] = rs.getString("b5");
                colors[13] = rs.getString("b6");
                colors[14] = rs.getString("b7");
                colors[15] = rs.getString("b8");
                theme.setColors(colors);
            }
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return theme;

}