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:org.tibetjungle.demo.dao.ContactDaoImpl.java

public void create(final Contact contact) {
    getJdbcTemplate().update("insert into contacts values (?, ?, ?)", new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setLong(1, contact.getId());
            ps.setString(2, contact.getName());
            ps.setString(3, contact.getEmail());
        }// w ww . j  a  va 2s. c o m
    });
}

From source file:br.com.fatecpg.repositories.mysql.MySqlLogRepository.java

@Override
public void add(Log log) {
    if (log == null) {
        throw new IllegalArgumentException("log can't be null.");
    }/*from ww  w .  j  a  va  2s. c  o m*/

    String sql = "insert into log (applicationName, message, url, ipaddress, username, createdon, details) values (?, ?, ?, ?, ?, ?, ?)";
    Connection connection = dbProvider.getConnection();

    try {

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, log.getApplicationName());
        preparedStatement.setString(2, log.getMessage());
        preparedStatement.setString(3, log.getUrl());
        preparedStatement.setString(4, log.getIpAddress());
        preparedStatement.setString(5, log.getUsername());

        Date sqlDate = new Date(log.getCreatedDate().getYear(), log.getCreatedDate().getMonth(),
                log.getCreatedDate().getDay());

        preparedStatement.setDate(6, sqlDate);
        preparedStatement.setString(7, log.getDetails());

        preparedStatement.execute();

    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:fr.xebia.springframework.security.core.userdetails.jdbc.ExtendedJdbcUserDetailsManager.java

/**
 * Update {@link fr.xebia.springframework.security.core.userdetails.ExtendedUser} extra columns associated with
 * {@link fr.xebia.springframework.security.core.userdetails.ExtendedUser#getAllowedRemoteAddresses()} and
 * {@link fr.xebia.springframework.security.core.userdetails.ExtendedUser#getComments()}.
 *
 * @param user//from  w w  w. j a v  a  2s  .  c  om
 */
protected void updateUserExtraColumns(UserDetails user) {
    if (user instanceof ExtendedUser) {
        final ExtendedUser extendedUser = (ExtendedUser) user;
        int updatedRows = getJdbcTemplate().update(updateUserExtraColumns, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, extendedUser.getAllowedRemoteAddresses());
                ps.setString(2, extendedUser.getComments());
                ps.setString(3, extendedUser.getUsername());
            }
        });
        if (updatedRows != 1) {
            log.warn("More/less (" + updatedRows
                    + ") than one row have been updated modifying 'allowedRemoteIpAddresses' to '"
                    + extendedUser.getAllowedRemoteAddresses() + "' and 'comments' to '"
                    + extendedUser.getComments() + "' for username '" + extendedUser.getUsername() + "'");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Given user " + user + " is not an ExtendedUser, no additional column to update.");
        }
    }
}

From source file:com.github.javaplugs.mybatis.TreeNodeTypeHandler.java

@Override
public void setNonNullParameter(PreparedStatement ps, int i, TreeNode parameter, JdbcType jdbcType)
        throws SQLException {
    try {//from  w w  w . j  a v  a 2s .  co m
        ps.setString(i, ReaderWriter.write(parameter));
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

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

public List deleteFiles(final List names) throws IOException {
    jdbcTemplate.batchUpdate(table.sqlMarkDeleteByName(), new BatchPreparedStatementSetter() {
        @Override/*from   ww  w. ja  v  a  2s  . c om*/
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setFetchSize(1);
            ps.setBoolean(1, true);
            ps.setString(2, (String) names.get(i));
            ps.addBatch();
        }

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

From source file:minor.login.AddPortfolio.java

@Override
public String execute() {
    try {//from w w  w  . ja  va 2s  .  c  o m
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Minor", "root", "");
        PreparedStatement ps = con.prepareStatement("INSERT INTO userportfolio VALUES(?,?,?,?)");
        ps.setString(1, getUsername());
        ps.setString(2, getStockQuote().toUpperCase());
        ps.setString(3, getQuantity());
        ps.setString(4, getBuy_price());
        ps.executeUpdate();
        String sql = "UPDATE userinfo " + "  SET portfolio = ? " + "WHERE Username = ?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, "1");
        pstmt.setString(2, getUsername());
        pstmt.executeUpdate();
        con.close();

    } catch (ClassNotFoundException e) {
    } catch (SQLException e) {
        System.out.println(e);
    }
    System.out.println(getUsername() + getStockQuote() + getQuantity() + getBuy_price());
    return "success";
}

From source file:net.freechoice.model.orm.Map_Tag.java

@Override
public PreparedStatementCreator createUpdate(final FC_Tag entity) {

    return new PreparedStatementCreator() {
        @Override/*from   w w  w  .  j  av a2 s.  c  o  m*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

            PreparedStatement ps = con
                    .prepareStatement("update  FC_Tag" + " set content = ? " + "where id = ? ");
            ps.setString(1, entity.content);
            ps.setInt(2, entity.id);
            return ps;
        }
    };
}

From source file:org.perconsys.dao.implement.BlogDb.java

@Override
public Blog create(final Blog blog) {
    KeyHolder keyh = new GeneratedKeyHolder();
    final String sql = "insert into blogs (name, url_name, user_id) value (?, ?, ?) ;";

    getJdbcTemplate().update(new PreparedStatementCreator() {

        @Override/*  w  w  w. j  a  v a2 s .  c  o  m*/
        public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
            PreparedStatement pstat = conn.prepareStatement(sql, new String[] { "id" });
            pstat.setString(1, blog.getName());
            pstat.setString(2, blog.getUrl_name());
            pstat.setLong(3, blog.getUser().getId());
            return pstat;
        }
    }, keyh);
    Long id = (Long) keyh.getKey();
    blog.setId(id);
    return blog;
}

From source file:at.alladin.rmbt.db.fields.StringField.java

@Override
public void getField(final PreparedStatement ps, final int idx) throws SQLException {
    if (value == null)
        ps.setNull(idx, Types.VARCHAR);
    else/*from  www  .ja  v  a2  s.  c  o  m*/
        ps.setString(idx, value);
}

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

public void saveApply(final VehicleLicenseModel apply, final byte[] fileStream) {
    String sql = "INSERT INTO itms_vehicle_license_apply_tmp (APPLYNUM,"
            + "BIZTYPE, SERVICEITEMNUM, BIZREASON, OWNERNAME, HOMEADDRESS, ZIP, EMAIL, PHONE,"
            + " MOBILE, IDTYPE, IDNO, SEX, BIRTHDAY, NATIVEPLACE, IGAMA, PLATENUMTYPE, PLATENUM,"
            + "VEHICLEIDNUM, ENGINENUM, VEHICLEIMAGE, PROXYFLAG, PROXYNAME,"
            + "PROXYADDRESS, PROXYZIP, PROXYPHONE, PROXYEMAIL, 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.getSERVICEITEMNUM());
            ps.setString(4, apply.getBizReason());
            ps.setString(5, apply.getOwnerName());
            ps.setString(6, apply.getHomeAddress());
            ps.setString(7, apply.getZip());
            ps.setString(8, apply.getEmail());
            ps.setString(9, apply.getPhone());

            ps.setString(10, apply.getMobile());
            ps.setInt(11, apply.getIdType());
            ps.setString(12, apply.getIdNO());
            ps.setInt(13, apply.getSex());
            ps.setString(14, apply.getBirthday());
            ps.setString(15, apply.getNATIVEPLACE());
            ps.setString(16, apply.getIGAMA());
            ps.setString(17, apply.getPlateNumType());
            ps.setString(18, apply.getPlateNum());

            ps.setString(19, apply.getVehicleIdNum());
            ps.setString(20, apply.getengineNum());
            // ps.setString(21, apply.getVehicleImage());
            lobCreator.setBlobAsBytes(ps, 21, fileStream);
            ps.setInt(22, apply.getProxyFlag());
            // ps.setString(23, apply.getLetterOfAuth());
            ps.setString(23, apply.getPROXYNAME());

            ps.setString(24, apply.getPROXYADDRESS());
            ps.setString(25, apply.getPROXYzip());
            ps.setString(26, apply.getPROXYphone());
            ps.setString(27, apply.getPROXYemail());
            ps.setInt(28, apply.getReceiveType());
            ps.setString(29, apply.getReceiver());
            ps.setString(30, apply.getReceiveAddress());
            ps.setString(31, apply.getReceivePhone());
            ps.setString(32, apply.getCreator());
            //  ps.setDate(35, java.sql.Date.valueOf("2012-08-17"));// apply.getCreatedTime().toString()
            ps.setInt(33, apply.getSource());// 
            // ps.setString(37,"2012-09-09");//apply.getSUBMITTIME()
        }/*  w w w .  ja  v  a  2s  . c o m*/
    });
}