Example usage for java.sql PreparedStatement setInt

List of usage examples for java.sql PreparedStatement setInt

Introduction

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

Prototype

void setInt(int parameterIndex, int x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java int value.

Usage

From source file:com.product.ProductResource.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)// w ww . j  a v  a  2  s . c  o m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (conn == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        String result = "";
        JsonArrayBuilder productArr = Json.createArrayBuilder();
        while (rs.next()) {
            JsonObjectBuilder prodMap = Json.createObjectBuilder();
            prodMap.add("productID", rs.getInt("product_id"));
            prodMap.add("name", rs.getString("name"));
            prodMap.add("description", rs.getString("description"));
            prodMap.add("quantity", rs.getInt("quantity"));
            productArr.add(prodMap);
        }
        result = productArr.build().toString();

        return result;
    }

}

From source file:com.l2jfree.gameserver.model.entity.Couple.java

public void divorce() {
    Connection con = null;//  www.j a  v  a2 s  .  c  o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM couples WHERE id=?");
        statement.setInt(1, _id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: Couple.divorce(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.products.ProductResource.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)// ww  w  .  j a v a  2 s. co m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (co == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement pstmt = co.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        String result = "";
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map productMap = new LinkedHashMap();
            productMap.put("productID", rs.getInt("product_id"));
            productMap.put("name", rs.getString("name"));
            productMap.put("description", rs.getString("description"));
            productMap.put("quantity", rs.getInt("quantity"));
            productArr.add(productMap);
        }
        result = productArr.toString();

        return result;
    }

}

From source file:dao.MaterialDaoImplem.java

@Override
public int insertMaterial(Material material) {
    try (Connection connection = dataSource.getConnection()) {
        String query = ("INSERT INTO Material (name, weight, manufacturer,cost,quantity) values(?,?,?,?,?)");
        PreparedStatement stat = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        stat.setString(1, material.getName());
        stat.setInt(2, material.getWeight());
        stat.setString(3, material.getManufacturer());
        stat.setInt(4, material.getCost());
        stat.setInt(5, material.getQuantity());
        stat.execute();//from   w  w  w  . jav  a 2s  .co  m
        ResultSet res = stat.getGeneratedKeys();
        if (res.next()) {
            return res.getInt(1);
        } else {
            throw new RuntimeException("? ?    ");
        }
    } catch (Exception e) {
        throw new RuntimeException("Error:insertMaterial", e);
    }
}

From source file:com.assignment4.productdetails.java

@DELETE
@Path("{id}")
@Consumes(MediaType.TEXT_PLAIN)/*from w  w w .j  a v  a  2 s  .  c  om*/
@Produces(MediaType.TEXT_PLAIN)
public String deleteProduct(@PathParam("id") int id) throws SQLException {

    if (conn == null) {
        return "Not able to connected";
    } else {
        String query = "DELETE FROM products WHERE product_id = ?";
        PreparedStatement prestmt = conn.prepareStatement(query);
        prestmt.setInt(1, id);
        prestmt.executeUpdate();
        return "The specified row is deleted";

    }

}

From source file:com.l2jfree.gameserver.model.entity.hellbound.TowerOfNaiaRoom.java

public void initSpawns(final int roomId) {
    _mobSpawnData.clear();/*from w w w  .j  a  va  2  s  .c o m*/
    _ingeniousContraptionSpawnData = null;
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement("SELECT * FROM hb_naia_spawnlist WHERE room_id=?");
        statement.setInt(1, roomId);
        ResultSet rset = statement.executeQuery();
        while (rset.next()) {
            int npcId = rset.getInt("npc_id");
            L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(npcId);
            if (npcTemplate != null) {
                int x = rset.getInt("locx");
                int y = rset.getInt("locy");
                int z = rset.getInt("locz");
                int heading = rset.getInt("heading");
                int respawnDelay = rset.getInt("respawn_delay");
                SpawnData spawnData = new SpawnData(npcId, x, y, z, heading, respawnDelay);
                if (npcId == TowerOfNaiaManager.ROOM_CONTROLLER_ID)
                    _ingeniousContraptionSpawnData = spawnData;
                else
                    _mobSpawnData.add(spawnData);
            } else {
                _log.warn("TowerOfNaia: Data missing in NPC table for ID: " + npcId + ".");
            }
        }
        rset.close();
        statement.close();
    } catch (Exception e) {
        _log.warn("TowerOfNaia: Spawn could not be initialized: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:mupomat.controller.ObradaOperater.java

@Override
public void obrisiPostojeci(Operater entitet) throws SQLException {
    try {/*from   w  w w.  j  a  v  a  2 s . c  om*/
        Connection veza = MySqlBazaPodataka.getConnection();
        PreparedStatement izraz = veza.prepareStatement("delete from  operater  where sifra=? ");
        izraz.setInt(1, entitet.getSifra());
        izraz.executeUpdate();
        izraz.close();
        veza.close();
    } catch (ClassNotFoundException | IOException e) {
        //  System.out.println(e.getMessage());
        e.printStackTrace();
        return;
    }
}

From source file:edu.cmu.lti.oaqa.openqa.hello.eval.passage.PassageMAPEvalPersistenceProvider.java

@Override
public Multimap<Key, PassageMAPCounts> retrievePartialCounts(final ExperimentKey experiment) {
    String select = getSelectPassageAggregates();
    final Multimap<Key, PassageMAPCounts> counts = LinkedHashMultimap.create();
    RowCallbackHandler handler = new RowCallbackHandler() {
        public void processRow(ResultSet rs) throws SQLException {
            Key key = new Key(rs.getString("experimentId"), new Trace(rs.getString("traceId")),
                    rs.getInt("stage"));
            PassageMAPCounts cnt = new PassageMAPCounts(rs.getFloat("docavep"), rs.getFloat("psgavep"),
                    rs.getFloat("aspavep"), rs.getInt("count"));
            counts.put(key, cnt);/*from   w  w w.ja v  a2 s  .c om*/
        }
    };
    DataStoreImpl.getInstance().jdbcTemplate().query(select, new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, experiment.getExperiment());
            ps.setInt(2, experiment.getStage());
        }
    }, handler);
    return counts;
}

From source file:com.Assignment4.Assign.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)//from w ww  . j  a v  a 2s  .c o  m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (connected == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement pstmt = connected.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        String res = "";
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map proMap = new LinkedHashMap();
            proMap.put("productID", rs.getInt("product_id"));
            proMap.put("name", rs.getString("name"));
            proMap.put("description", rs.getString("description"));
            proMap.put("quantity", rs.getInt("quantity"));
            productArr.add(proMap);
        }
        res = productArr.toString();

        return res;
    }

}

From source file:com.Assignment4.Prod.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)/* w w w  . j a  v a  2s  .  c  o m*/
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (connect == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement prepstmt = connect.prepareStatement(query);
        prepstmt.setInt(1, id);
        ResultSet rs = prepstmt.executeQuery();
        String result = "";
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map pMap = new LinkedHashMap();
            pMap.put("productID", rs.getInt("product_id"));
            pMap.put("name", rs.getString("name"));
            pMap.put("description", rs.getString("description"));
            pMap.put("quantity", rs.getInt("quantity"));
            productArr.add(pMap);
        }
        result = productArr.toString();

        return result;
    }

}