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:sample.contact.dao.impl.MenuDaoImpl.java

public int update(final Menu menu) {
    return getJdbcTemplate().update("update menus set menu_name = ?, menu_path = ? where id = ?",
            new PreparedStatementSetter() {
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setString(1, menu.getName());
                    ps.setString(2, menu.getPath());
                    ps.setLong(3, menu.getId());
                }//from   ww  w. jav a 2s.c o m
            });
}

From source file:annis.administration.BinaryImportHelper.java

@Override
public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
    ps.setString(1, fileDestination.getName());
    ps.setLong(2, this.corpusRef);
    ps.setString(3, this.mimeType);
    ps.setString(4, fileSource.getName());
    ps.executeUpdate();/* w  ww.j a  v  a2 s .  com*/
    try {
        FileUtils.copyFile(fileSource, fileDestination);
    } catch (IOException ex) {
        log.error("Could not copy file " + fileSource.getPath(), ex);
        return false;
    }

    return true;
}

From source file:sample.contact.dao.impl.MenuDaoImpl.java

public Menu create(final Menu menu) {
    getJdbcTemplate().update("insert into menus (menu_name, menu_path)values (?, ?)",
            new PreparedStatementSetter() {
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setString(1, menu.getName());
                    ps.setString(2, menu.getPath());
                }//from   w ww  .  j  a  va  2 s  .  c o  m
            });
    long id = getJdbcTemplate().queryForObject("select last_insert_id()", Long.class);
    return getById(id);
}

From source file:com.javacodegags.waterflooding.model.CaptionImplemented.java

@Override
public int insertCaption(final String name) {
    final String sql = "INSERT INTO caption (name,argument) VALUES (?,1);";
    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        @Override/*  w  w w  .  java  2  s  .c om*/
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, name);
            return ps;
        }
    }, holder);
    return holder.getKey().intValue();
}

From source file:com.UploadAction.java

public String execute() throws Exception {
    try {// w  ww .  j  ava2  s  .c  o m
        //long maxFileSize = (2 * 1024 * 1024);
        //int maxMemSize = (2 * 1024 * 1024);
        //final String path = "/tmp";
        HttpServletRequest request = ServletActionContext.getRequest();
        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        if (isMultiPart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(request);
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem fileItem = iter.next();
                if (fileItem.isFormField()) {
                    //processFormField(fileItem);
                } else {
                    flItem = fileItem;
                }
            }
        }
        HttpSession session = ServletActionContext.getRequest().getSession(false);
        String User = (String) session.getAttribute("username");
        Connection con = Connections.conn();
        PreparedStatement stat = con.prepareStatement("update user set image=? where UserName=?");
        stat.setString(2, User);
        stat.setBinaryStream(1, flItem.getInputStream(), (int) flItem.getSize());
        // stat.setBinaryStream(4, (InputStream) itemPhoto.getInputStream(), (int) itemPhoto.getSize());
        int rows = stat.executeUpdate();
        if (rows > 0) {
            return "success";
        }
    } catch (Exception e) {
    }
    return "success";
}

From source file:com.wso2telco.proxy.util.DBUtils.java

public static boolean isSPAllowedScope(String scopeName, String clientId)
        throws ConfigurationException, AuthenticatorException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;//from  w  ww.  ja v  a  2  s . c  o m

    String[] scopeValues = scopeName.split("\\s+|\\+");
    StringBuilder params = new StringBuilder("?");
    for (int i = 1; i < scopeValues.length; i++) {
        params.append(",?");
    }

    String queryToGetOperatorProperty = "SELECT COUNT(*) AS record_count FROM `sp_configuration` WHERE `client_id`=? AND `config_key`=? AND "
            + "`config_value` in (" + params + ")";
    boolean isSPAllowedScope = false;

    try {
        connection = getConnectDBConnection();
        preparedStatement = connection.prepareStatement(queryToGetOperatorProperty);
        preparedStatement.setString(1, clientId);
        preparedStatement.setString(2, "scope");

        for (int i = 0; i < scopeValues.length; i++) {
            preparedStatement.setString(i + 3, scopeValues[i]);
        }

        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            isSPAllowedScope = resultSet.getInt("record_count") == scopeValues.length;
        }
    } catch (SQLException e) {
        handleException("Error occurred while SP Configurations for ClientId - " + clientId + " and Scope - "
                + scopeName, e);
    } catch (NamingException e) {
        throw new ConfigurationException("DataSource could not be found in mobile-connect.xml");
    } finally {
        closeAllConnections(preparedStatement, connection, resultSet);
    }
    return isSPAllowedScope;
}

From source file:ece356.UserDBAO.java

public static String getSalt(String username) throws ClassNotFoundException, SQLException {
    Connection con = null;//from   w  w  w. j  a  v  a 2s.  co m
    PreparedStatement pstmt = null;
    UserData ret;
    try {
        con = getConnection();
        String query = "select COUNT(*)as numRecords, password_salt from user INNER JOIN userType ON user.userTypeID = userType.userTypeID where user.username = ?";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, username);
        ResultSet resultSet;
        resultSet = pstmt.executeQuery();
        resultSet.next();

        if (resultSet.getInt("numRecords") > 0) {
            return resultSet.getString("password_salt");
        } else
            return null;
    } catch (Exception e) {
        System.out.println("EXCEPTION:%% " + e);
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
        if (con != null) {
            con.close();
        }
    }
    return null;
}

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

public void deleteFile(final String name) throws IOException {
    jdbcTemplate.update(table.sqlMarkDeleteByName(), new PreparedStatementSetter() {
        @Override//from ww w . j  a v  a  2 s. c o m
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setFetchSize(1);
            ps.setBoolean(1, true);
            ps.setString(2, name);
        }
    });
}

From source file:test.RowMapperTests.java

@Test
public void testPreparedStatementSetterWithRowMapper() throws SQLException {
    result = template.query("some SQL", new PreparedStatementSetter() {
        @Override// w  w w  .j a va2 s.c  om
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, "test");
        }
    }, new TestRowMapper());
    verify(preparedStatement).setString(1, "test");
    verify(preparedStatement).close();
}

From source file:com.wabacus.system.datatype.VarcharType.java

public void setPreparedStatementValue(int iindex, String value, PreparedStatement pstmt, AbsDatabaseType dbtype)
        throws SQLException {
    log.debug("setString(" + iindex + "," + value + ")");
    pstmt.setString(iindex, value);
}