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.vertica.hivestoragehandler.VerticaRecordWriter.java

public VerticaRecordWriter(Connection conn, String writerTable, long batch) throws SQLException {
    this.connection = conn;
    batchSize = batch;//from w ww.j  a v a  2  s. c  o  m

    vTable = new VerticaRelation(writerTable);

    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ");
    sb.append(vTable.getQualifiedName());

    StringBuilder values = new StringBuilder();
    values.append(" VALUES(");
    sb.append("(");

    String metaStmt = "select ordinal_position, column_name, data_type, is_identity, data_type_name "
            + "from v_catalog.odbc_columns " + "where schema_name = ? and table_name = ? "
            + "order by ordinal_position;";

    PreparedStatement stmt = conn.prepareStatement(metaStmt);
    stmt.setString(1, vTable.getSchema());
    stmt.setString(2, vTable.getTable());

    ResultSet rs = stmt.executeQuery();
    boolean addComma = false;
    while (rs.next()) {
        if (!rs.getBoolean(4)) {
            if (addComma) {
                sb.append(',');
                values.append(',');
            }
            sb.append(rs.getString(2));
            values.append('?');
            addComma = true;
        } else {
            LOG.debug("Skipping identity column " + rs.getString(4));
        }
    }

    sb.append(')');
    values.append(')');
    sb.append(values.toString());

    statement = conn.prepareStatement(sb.toString());
}

From source file:ece356.UserDBAO.java

public static DoctorData queryDoctor(String userName) throws ClassNotFoundException, SQLException {
    Connection con = null;//from  ww w  .java2  s .com
    PreparedStatement pstmt = null;
    DoctorData ret;
    try {
        con = getConnection();

        // Query for general doctor information
        String query = "SELECT * FROM doctorView where username = ?";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, userName);

        ResultSet resultSet;
        resultSet = pstmt.executeQuery();
        resultSet.next();
        ret = new DoctorData();
        ret.userName = resultSet.getString("username");
        ret.firstName = resultSet.getString("first_name");
        ret.lastName = resultSet.getString("last_name");
        ret.middleInitial = resultSet.getString("middle_initial");
        ret.gender = resultSet.getString("gender");
        ret.emailAddress = resultSet.getString("email_address");
        ret.yearsLicensed = resultSet.getInt("yearsLicensed");
        ret.averageRating = resultSet.getInt("averageRating");
        ret.numberOfReviews = resultSet.getInt("numberOfReviews");

        // Query for work addresses of doctor
        query = "SELECT * FROM doctorWorkAddressView where doc_address_username = ?";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, userName);
        resultSet = pstmt.executeQuery();

        ArrayList<WorkAddressData> workAddressList = new ArrayList<WorkAddressData>();
        ret.workAddressList = workAddressList;
        while (resultSet.next()) {
            WorkAddressData workAddress = new WorkAddressData();
            workAddress.city = resultSet.getString("city");
            workAddress.state = resultSet.getString("state");
            workAddress.postalCode = resultSet.getString("postal_code");
            workAddress.streetName = resultSet.getString("street_name");
            workAddress.streetNumber = resultSet.getInt("street_number");
            workAddress.unitNumber = resultSet.getString("street_unit_number");
            workAddressList.add(workAddress);
        }

        // Query for specializations of doctor
        query = "SELECT * FROM doctorSpecializationView where doc_spec_username = ?";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, userName);
        resultSet = pstmt.executeQuery();

        ArrayList<String> specializationList = new ArrayList<String>();
        ret.specializationList = specializationList;
        while (resultSet.next()) {
            String specialization = resultSet.getString("specTypeName");
            specializationList.add(specialization);
        }

        // Query for reviews of doctor
        query = "SELECT * FROM review where doc_username = ? order by date desc";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, userName);
        resultSet = pstmt.executeQuery();

        ArrayList<ReviewData> reviewList = new ArrayList<ReviewData>();
        ret.reviewList = reviewList;
        while (resultSet.next()) {
            ReviewData review = new ReviewData();
            review.comment = resultSet.getString("comment");
            review.reviewId = resultSet.getString("reviewId");
            review.doctorUsername = resultSet.getString("doc_username");
            review.patientUsername = resultSet.getString("patient_username");
            review.date = resultSet.getDate("date");
            review.rating = resultSet.getInt("rating");
            reviewList.add(review);
        }

        return ret;
    } catch (Exception e) {
        System.out.println("EXCEPTION:%% " + e);
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
        if (con != null) {
            con.close();
        }
    }
    return null;
}

From source file:io.muic.ooc.webapp.service.UserService.java

public void deleteUser(Connection conn, String id) {

    String query = "delete from account where id = ?";
    try {// w w w.j  a  v  a2  s .  c o m
        PreparedStatement preparedStatement = conn.prepareStatement(query);
        preparedStatement.setString(1, id);
        // do update
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        System.out.println("Can't delete user");
    }
}

From source file:mercury.DigitalMediaDAO.java

public final DigitalMediaDTO getDigitalMedia(int id, String fileName) {
    Connection con = null;/* w w  w.j ava2  s  .c  o  m*/
    byte[] blob = null;
    DigitalMediaDTO dto = new DigitalMediaDTO();
    try {
        con = getDataSource().getConnection();
        String sql = " SELECT file, file_name, mime_type FROM digital_media " + " WHERE id = ? "
                + " AND file_name = ? ;";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setInt(1, id);
        pst.setString(2, fileName);
        con.setAutoCommit(false);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            blob = rs.getBytes(1);
            if (blob == null) {
                return null;
            }
            dto.setIn(new ByteArrayInputStream(blob));
            dto.setLength((int) blob.length);
            dto.setFileName(rs.getString(2));
            dto.setMimeType(rs.getString(3));
        }
        return dto;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new DAOException(e.getMessage());
    } finally {
        closeConnection(con);
    }
}

From source file:com.github.ffremont.writers.PersonDaoWriter.java

@Override
public void write(List<? extends Person> list) throws Exception {

    jdbcTemplate.batchUpdate(INSERTS, new BatchPreparedStatementSetter() {
        @Override/*from  w w  w.  j a va  2  s  .c o m*/
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Person person = list.get(i);
            ps.setInt(1, person.getId());
            ps.setString(2, person.getNom());
            ps.setString(3, person.getPrenom());
            ps.setString(4, person.getCivilite());
        }

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

    });

}

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

@Override
public boolean cancellaPerson(String numero) {
    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUser("root");
    datasource.setPassword("root");
    datasource.setUrl("jdbc:mysql://localhost:3306/Rubrica");
    Connection connection = null;
    try {/*  ww w  .  j  a  va2 s  . com*/
        connection = datasource.getConnection();
        String sql = "DELETE FROM contatti " + "WHERE numero = ? ;";
        PreparedStatement stat = connection.prepareStatement(sql);
        stat.setString(1, numero);
        if (stat.executeUpdate() > 0) {
            return true;
        }
    } catch (SQLException e) {
        logger.error(e);
    } finally {
        DbUtils.closeQuietly(connection);
    }
    return false;
}

From source file:io.muic.ooc.webapp.service.UserService.java

public void createUser(Connection conn, String id, String password, String name) {

    String query = "insert into account (id, password, name)" + " values (?, ?, ?)";
    try {//  www.j ava  2 s  .com
        PreparedStatement preparedStatement = conn.prepareStatement(query);
        preparedStatement.setString(1, id);
        preparedStatement.setString(2, hash(password));
        preparedStatement.setString(3, name);
        // do update
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        System.out.println("Can't create user" + e);
    }
}

From source file:com.tomoare.mybatis.type.EnumValueTypeHandler.java

@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
        throws SQLException {
    if (jdbcType == null) {
        ps.setString(i, ((DbCodeConstant) parameter).getCodeValue());
    } else {/* ww  w  .  ja  v  a  2s.  c  o  m*/
        ps.setObject(i, ((DbCodeConstant) parameter).getCodeValue(), jdbcType.TYPE_CODE); // TODO
    }
}

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

@Override
public boolean aggiornaPersonInfo(String numero, String data) {
    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUser("root");
    datasource.setPassword("root");
    datasource.setUrl("jdbc:mysql://localhost:3306/Rubrica");
    Connection connection = null;
    try {/* w ww  .  j  a v a 2  s.  c o m*/
        connection = datasource.getConnection();
        String sql = "UPDATE contatti SET data_nascita = ?" + "WHERE numero = ? ;";
        PreparedStatement stat = connection.prepareStatement(sql);
        stat.setString(1, data);
        stat.setString(2, numero);
        if (stat.executeUpdate() > 0) {
            return true;
        }
    } catch (SQLException e) {
        logger.error(e);
    } finally {
        DbUtils.closeQuietly(connection);
    }
    return false;
}

From source file:io.kamax.mxisd.backend.sql.SqlThreePidProvider.java

@Override
public List<_ThreePid> getThreepids(_MatrixID mxid) {
    List<_ThreePid> threepids = new ArrayList<>();

    String stmtSql = cfg.getProfile().getThreepid().getQuery();
    try (Connection conn = pool.get()) {
        PreparedStatement stmt = conn.prepareStatement(stmtSql);
        stmt.setString(1, mxid.getId());

        ResultSet rSet = stmt.executeQuery();
        while (rSet.next()) {
            String medium = rSet.getString("medium");
            String address = rSet.getString("address");
            threepids.add(new ThreePid(medium, address));
        }// w ww  .j  a  va 2 s . co m

        return threepids;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}