Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

From source file:com.uiip.gviviani.esercizioweekend.interfaces.impl.DefaultPhoneDAO.java

@Override
public PhoneModel getPhoneInfo(String name) {
    PhoneModel phone = new PhoneModel();
    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUser("root");
    datasource.setPassword("root");
    datasource.setUrl("jdbc:mysql://localhost:3306/Rubrica");
    Connection connection = null;
    try {//from   www. j  av  a2 s  .  c o  m
        connection = datasource.getConnection();
        String sql = "SELECT id, name, brand, opsys, displaySize " + "FROM telefono WHERE name = ? ;";
        PreparedStatement stat = connection.prepareStatement(sql);
        stat.setString(1, name);
        ResultSet res = stat.executeQuery();
        if (res.first()) {
            phone.setNome(res.getString("name"));
            phone.setBrand(res.getString("brand"));
            phone.setOpsys(res.getString("opsys"));
            phone.setDisplay(res.getString("displaySize"));
        } else {
            phone = null;
        }
    } catch (SQLException e) {
        logger.error(e);
        phone = null;
    } finally {
        DbUtils.closeQuietly(connection);
    }
    return phone;
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void insertPinAttempt(String msisdn, int attempts, String sessionId)
        throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "insert into multiplepasswords(username, attempts, ussdsessionid) values  (?,?,?);";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, msisdn);
    ps.setInt(2, attempts);/*from w ww. ja  v  a 2 s .com*/
    ps.setString(3, sessionId);
    log.info(ps.toString());
    ps.execute();

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

From source file:org.apache.lucene.store.jdbc.handler.ActualDeleteFileEntryHandler.java

public List deleteFiles(final List names) throws IOException {
    jdbcTemplate.batchUpdate(table.sqlDeleteByName(), new BatchPreparedStatementSetter() {
        @Override/*  w  w w.ja v a  2s. co m*/
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setFetchSize(1);
            ps.setString(1, (String) names.get(i));
        }

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

From source file:com.suntek.gztpb.dao.DriverLicenseDao.java

public void saveApply(final DriverLicenseModel apply) {
    String sql = "INSERT INTO " + apply.getTableName() + " (APPLYNUM,BIZTYPE, DOCNUM,BIZREASON,receiptNum,"
            + "OWNERNAME,country,IDTYPE,IDNO,SEX,BIRTHDAY," + "HOMEADDRESS,postAddress,PHONE,MOBILE,EMAIL,"
            + "RECEIVETYPE, RECEIVER, RECEIVEADDRESS, RECEIVEPHONE," + "CREATOR, CREATEDTIME, SOURCE) VALUES("
            + "?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate, ?)";
    getJdbcTemplate().execute(sql, new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
            ps.setString(1, apply.getApplyNum());
            ps.setString(2, apply.getBizType());
            ps.setString(3, apply.getDocNum());
            ps.setString(4, apply.getBizReason());
            ps.setString(5, apply.getReceiptNum());

            ps.setString(6, apply.getOwnerName());
            ps.setString(7, apply.getCountry());
            ps.setInt(8, apply.getIdType());
            ps.setString(9, apply.getIdNO());
            ps.setInt(10, apply.getSex());
            ps.setString(11, apply.getBirthday());

            ps.setString(12, apply.getHomeAddress());
            ps.setString(13, apply.getPostAddress());
            ps.setString(14, apply.getPhone());
            ps.setString(15, apply.getMobile());
            ps.setString(16, apply.getEmail());

            ps.setInt(17, apply.getReceiveType());
            ps.setString(18, apply.getReceiver());
            ps.setString(19, apply.getReceiveAddress());
            ps.setString(20, apply.getReceivePhone());

            ps.setString(21, apply.getCreator());
            ps.setInt(22, apply.getSource());
        }//from  www .  j a  v  a 2s .c o  m
    });
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updatePin(int pin, String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set pin=? where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setInt(1, pin);/*from   ww w.j  ava 2s  .c om*/
    ps.setString(2, sessionId);
    log.info(ps.toString());
    ps.execute();

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

From source file:com.ywang.alone.handler.task.AuthTask.java

/**
 * ? { 'key':'2597aa1d37d432a', 'pNo':'0' }
 * /*from w  w  w. ja  va2  s .co  m*/
 * @param param
 * @return
 */
private static String getAllFollows(String msg) {
    JSONObject jsonObject = AloneUtil.newRetJsonObject();
    JSONObject param = JSON.parseObject(msg);
    String token = param.getString("key");
    String userId = null;

    if (StringUtils.isEmpty(token)) {
        jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
        jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
        jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);
    } else {
        Jedis jedis = JedisUtil.getJedis();
        Long tokenTtl = jedis.ttl("TOKEN:" + token);
        if (tokenTtl == -1) {
            jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
            jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
            jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);

        } else {
            userId = jedis.get("TOKEN:" + token);
            if (StringUtils.isEmpty(userId)) {
                jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
                jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
                jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);

            }
        }
        JedisUtil.returnJedis(jedis);
    }

    if (StringUtils.isEmpty(userId)) {
        return jsonObject.toJSONString();
    }

    LoggerUtil.logMsg("uid is " + userId);

    DruidPooledConnection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DataSourceFactory.getInstance().getConn();
        conn.setAutoCommit(false);

        stmt = conn.prepareStatement(
                "SELECT * FROM USERBASE WHERE USER_ID IN (SELECT FOLLOWED_ID FROM FOLLOW WHERE USER_ID = ?)");
        stmt.setString(1, userId);

        ArrayList<UserInfo> followedList = new ArrayList<UserInfo>();
        UserInfo userInfo = null;
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {

            userInfo = new UserInfo();
            userInfo.setUserId(rs.getString("USER_ID"));
            userInfo.setAvatar(rs.getString("AVATAR"));
            userInfo.setNickName(rs.getString("NICKNAME"));
            userInfo.setAge(rs.getString("AGE"));
            userInfo.setRoleName(rs.getString("ROLENAME"));
            userInfo.setOnline(rs.getString("ONLINE"));
            userInfo.setLastLoginTime(rs.getLong("LAST_LOGIN_TIME"));
            userInfo.setIntro(rs.getString("INTRO"));
            userInfo.setDistance(rs.getString("DISTANCE"));

            followedList.add(userInfo);
        }

        conn.commit();
        conn.setAutoCommit(true);
    } catch (SQLException e) {
        LoggerUtil.logServerErr(e);
        jsonObject.put("ret", Constant.RET.SYS_ERR);
        jsonObject.put("errCode", Constant.ErrorCode.SYS_ERR);
        jsonObject.put("errDesc", Constant.ErrorDesc.SYS_ERR);
    } finally {
        try {
            if (null != stmt) {
                stmt.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            LoggerUtil.logServerErr(e.getMessage());
        }
    }

    return jsonObject.toJSONString();
}

From source file:com.ccoe.build.dal.ProjectJDBCTemplate.java

public int create(final Project project, final String appName) {
    final String SQL = "insert into RBT_PROJECT (pool_name, name, group_id, artifact_id, type, version, "
            + "duration, status, start_time) " + "values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplateObject.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(SQL, new String[] { "id" });
            ps.setString(1, appName);
            ps.setString(2, project.getName());
            ps.setString(3, project.getGroupId());
            ps.setString(4, project.getArtifactId());
            ps.setString(5, project.getType());
            ps.setString(6, project.getVersion());
            ps.setLong(7, project.getDuration());
            ps.setString(8, project.getStatus());
            ps.setTimestamp(9, new java.sql.Timestamp(project.getStartTime().getTime()));

            return ps;
        }/*from   w w  w .j a  va2 s.com*/
    }, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:com.ccoe.build.tracking.jdbc.SessionJDBCTemplate.java

public int[] batchUpdateCategory(final List<Session> sessions) {
    int[] updateCounts = jdbcTemplateObject.batchUpdate(
            "update RBT_SESSION set category = ?,  filter = ? where id = ?",
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setString(1, sessions.get(i).getCategory());
                    ps.setString(2, sessions.get(i).getFilter());
                    ps.setInt(3, sessions.get(i).getId());
                }//from w w  w  .  j  av  a  2  s. co  m

                public int getBatchSize() {
                    return sessions.size();
                }
            });
    return updateCounts;
}

From source file:org.tibetjungle.demo.dao.ContactDaoImpl.java

public void update(final Contact contact) {
    getJdbcTemplate().update("update contacts set contact_name = ?, address = ? where id = ?",
            new PreparedStatementSetter() {
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setString(1, contact.getName());
                    ps.setString(2, contact.getEmail());
                    ps.setLong(3, contact.getId());
                }//from   ww w . java  2s . co  m
            });
}

From source file:org.mayocat.store.rdbms.dbi.argument.MapAsJsonStringArgumentFactory.java

@Override
public Argument build(Class<?> expectedType, final Map value, StatementContext ctx) {
    try {// w  ww  . ja  v  a2 s. c  om
        return new Argument() {
            ObjectMapper mapper = new ObjectMapper();

            String json = mapper.writeValueAsString(value);

            @Override
            public void apply(int position, PreparedStatement statement, StatementContext ctx)
                    throws SQLException {
                statement.setString(position, json);
            }
        };
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Failed to convert map argument to JSON", e);
    }
}