List of usage examples for java.sql PreparedStatement setInt
void setInt(int parameterIndex, int x) throws SQLException;
int
value. From source file:com.mycompany.rubricatelefonica.DefaultUtenteDao.java
public boolean insertNewUtente(UtenteModel utente) { boolean inserito = false; MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setUrl("jdbc:mysql://localhost:3306/RubricaTelef"); Connection conn = null;//from w w w .j a v a 2 s .co m try { conn = dataSource.getConnection(); PreparedStatement stmtInsertUtente = conn.prepareStatement(INSERT_UTENTE); stmtInsertUtente.setInt(1, utente.getId()); stmtInsertUtente.setString(2, utente.getImei()); stmtInsertUtente.setString(3, utente.getNome()); stmtInsertUtente.setString(4, utente.getCognome()); stmtInsertUtente.setString(5, utente.getEmail()); stmtInsertUtente.setString(6, utente.getNumCell()); stmtInsertUtente.setString(7, utente.getNumTelFisso()); stmtInsertUtente.setString(8, utente.getData()); if (stmtInsertUtente.executeUpdate() > 0) { inserito = true; } } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("errore!! Connessione Fallita"); } finally { DbUtils.closeQuietly(conn); //oppure try with resource } // return inserito; }
From source file:com.product.Product.java
@GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON)/*from ww w. jav a 2 s . c om*/ public String getproduct(@PathParam("id") int id) throws SQLException { if (connect == null) { return "not connected"; } else { String query = "Select * from products where product_id = ?"; PreparedStatement prepstmnt = connect.prepareStatement(query); prepstmnt.setInt(1, id); ResultSet rs = prepstmnt.executeQuery(); String result = ""; JSONArray prodArr = new JSONArray(); while (rs.next()) { Map prodMap = new LinkedHashMap(); prodMap.put("productID", rs.getInt("product_id")); prodMap.put("name", rs.getString("name")); prodMap.put("description", rs.getString("description")); prodMap.put("quantity", rs.getInt("quantity")); prodArr.add(prodMap); } result = prodArr.toString(); return result; } }
From source file:dhbw.clippinggorilla.objects.user.UserUtils.java
/** * Returns all Userprofiles of the user. * * @param u The User whose Interestprofiles should be returned * @return all user profiles in a Set or null if sth. went wrong */// ww w. java 2s. c o m public static Set<InterestProfile> getAllInterestProfiles(User u) { try { String sql = "SELECT " + Columns.ID + " FROM " + Tables.USER_PROFILE + " WHERE " + Columns.USER_ID + " = ?"; PreparedStatement statement = Database.getConnection().prepareStatement(sql); statement.setInt(1, u.getId()); ResultSet result = statement.executeQuery(); Set<InterestProfile> profileSet = new HashSet<>(); while (result.next()) { profileSet.add(InterestProfileUtils.getInterestProfile(result.getInt(Columns.ID))); } return profileSet; } catch (SQLException ex) { Log.warning("Multiprofile selection failed", ex); return null; } }
From source file:com.assignment4.products.Pro_details.java
@POST @Path("/products") @Consumes(MediaType.APPLICATION_JSON)//from w ww . ja va2s . com @Produces(MediaType.TEXT_PLAIN) public String postProduct(String str) throws SQLException { JsonParser p = Json.createParser(new StringReader(str)); Map<String, String> pm = new LinkedHashMap<String, String>(); String key = ""; String val = ""; while (p.hasNext()) { JsonParser.Event event = p.next(); switch (event) { case KEY_NAME: key = p.getString(); break; case VALUE_STRING: val = p.getString(); pm.put(key, val); break; case VALUE_NUMBER: val = p.getString(); pm.put(key, val); break; default: break; } } if (conn == null) { return "Not connected"; } else { String q = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(q); ps.setInt(1, Integer.parseInt(pm.get("product_id"))); ps.setString(2, pm.get("name")); ps.setString(3, pm.get("description")); ps.setInt(4, Integer.parseInt(pm.get("quantity"))); ps.executeUpdate(); return "row has been inserted into the database"; } }
From source file:com.concursive.connect.web.modules.profile.dao.ProjectCategoryLogoFile.java
public boolean insert(Connection db) throws SQLException { boolean result = false; // The required linkModuleId linkModuleId = Constants.PROJECT_CATEGORY_FILES; // Determine if the database is in auto-commit mode boolean doCommit = false; try {// w ww.j a v a 2 s.co m if (doCommit = db.getAutoCommit()) { db.setAutoCommit(false); } // Insert the record result = super.insert(db); // Update the referenced pointer if (result) { int i = 0; PreparedStatement pst = db.prepareStatement( "UPDATE lookup_project_category " + "SET logo_id = ? " + "WHERE code = ? "); pst.setInt(++i, id); pst.setInt(++i, linkItemId); int count = pst.executeUpdate(); result = (count == 1); } if (doCommit) { db.commit(); } } catch (Exception e) { if (doCommit) { db.rollback(); } throw new SQLException(e.getMessage()); } finally { if (doCommit) { db.setAutoCommit(true); } } return result; }
From source file:com.l2jfree.gameserver.communitybbs.bb.Topic.java
/** * *//*w ww . j a va 2 s . co m*/ public void deleteme(Forum f) { TopicBBSManager.getInstance().delTopic(this); f.rmTopicByID(getID()); Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con .prepareStatement("DELETE FROM topic WHERE topic_id=? AND topic_forum_id=?"); statement.setInt(1, getID()); statement.setInt(2, f.getID()); statement.execute(); statement.close(); } catch (Exception e) { _log.error(e.getMessage(), e); } finally { L2DatabaseFactory.close(con); } }
From source file:dhbw.clippinggorilla.objects.user.UserUtils.java
/** * Returns the value whether a clipping mail shall be send * * @param u The User to be changed/* w w w. jav a2s.co m*/ * @return true == send; false == don't send */ public static boolean getEmailNewsletter(User u) { String sql = "SELECT " + Columns.SEND_CLIPPING_MAIL + " FROM " + Tables.USER + " WHERE " + Columns.ID + " = ?"; try { PreparedStatement statement = Database.getConnection().prepareStatement(sql); statement.setInt(1, u.getId()); ResultSet result = statement.executeQuery(); if (result.next()) { if (result.getInt(Columns.SEND_CLIPPING_MAIL) == 1) { u.setSendClippingMail(true); } else { u.setSendClippingMail(false); } } return u.isSendClippingMail(); } catch (SQLException ex) { Log.warning("Sql failed: getEmailNewsletter", ex); return u.isSendClippingMail(); } }
From source file:com.assignment4.productdetails.java
@GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON)//www. j a v a2s . c o m public String getproduct(@PathParam("id") int id) throws SQLException { if (conn == null) { return "it not able to connect"; } else { String query = "Select * from products where product_id = ?"; PreparedStatement prestmt = conn.prepareStatement(query); prestmt.setInt(1, id); ResultSet res = prestmt.executeQuery(); String results = ""; JSONArray podtAr = new JSONArray(); while (res.next()) { Map pdtmap = new LinkedHashMap(); pdtmap.put("productID", res.getInt("product_id")); pdtmap.put("name", res.getString("name")); pdtmap.put("description", res.getString("description")); pdtmap.put("quantity", res.getInt("quantity")); podtAr.add(pdtmap); } results = podtAr.toString(); return results; } }
From source file:cai.flow.packets.V8_FlowDstPrefix.java
void fill_specific(PreparedStatement add_raw_stm) throws SQLException { add_raw_stm.setString(13, Util.str_addr(dst_prefix)); add_raw_stm.setInt(14, (int) dst_mask); add_raw_stm.setInt(15, (int) dst_as); add_raw_stm.setInt(16, (int) output); add_raw_stm.setString(17, Params.getCurrentTime()); }
From source file:com.products.ProductResource.java
@POST @Consumes(MediaType.APPLICATION_JSON)//from ww w .j a va 2 s. co m @Produces(MediaType.TEXT_PLAIN) public String postProduct(String str) throws SQLException { JsonParser parser = Json.createParser(new StringReader(str)); Map<String, String> productMap = new LinkedHashMap<String, String>(); String key = ""; String value = ""; while (parser.hasNext()) { JsonParser.Event event = parser.next(); switch (event) { case KEY_NAME: key = parser.getString(); break; case VALUE_STRING: value = parser.getString(); productMap.put(key, value); break; case VALUE_NUMBER: value = parser.getString(); productMap.put(key, value); break; default: break; } } if (co == null) { return "Not connected"; } else { String query = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)"; PreparedStatement stmt = co.prepareStatement(query); stmt.setInt(1, Integer.parseInt(productMap.get("product_id"))); stmt.setString(2, productMap.get("name")); stmt.setString(3, productMap.get("description")); stmt.setInt(4, Integer.parseInt(productMap.get("quantity"))); stmt.executeUpdate(); return "row has been inserted into the database"; } }