List of usage examples for java.sql PreparedStatement setInt
void setInt(int parameterIndex, int x) throws SQLException;
int
value. From source file:dao.RuleDao.java
/** * Deletes a rule from a target DB and from the local APEX db * /*from ww w . j av a2 s . co m*/ * @param id * @return * @throws Exception */ public boolean deleteRule(int id) throws Exception { int brgId = 0; String name = ""; PreparedStatement statement = this.getApexConnection() .prepareStatement("SELECT BRG_PROJECTID, NAME FROM BUSINESSRULE WHERE ID=?"); statement.setInt(1, id); ResultSet result = statement.executeQuery(); while (result.next()) { brgId = result.getInt("BRG_PROJECTID"); name = result.getString("NAME"); } statement.close(); Statement statementTarget = null; try { Project project = this.getProject(brgId); statementTarget = this.getTargetConnection(project.getTargetConfig()).createStatement(); statementTarget.executeUpdate("DROP TRIGGER " + name.toUpperCase()); statementTarget.close(); } catch (Exception e) { Logger log = new Logger(); log.out(Level.ERROR, "deleteRule", "Rule or project doesn't excist"); } Statement statementRemoveApex = this.getApexConnection().createStatement(); statementRemoveApex.addBatch("DELETE FROM RULE_COLUMN WHERE BUSINESSRULEID=" + id); statementRemoveApex.addBatch("DELETE FROM RULE_TABLE WHERE BUSINESSRULEID=" + id); statementRemoveApex.addBatch("DELETE FROM RULE_VALUE WHERE BUSINESSRULEID=" + id); statementRemoveApex.addBatch("DELETE FROM BUSINESSRULE WHERE ID=" + id); statementRemoveApex.executeBatch(); statementRemoveApex.close(); return true; }
From source file:com.assignment4.productdetails.java
@POST @Path("/products") @Consumes(MediaType.APPLICATION_JSON)/*from w w w. j av a 2 s .c o m*/ @Produces(MediaType.TEXT_PLAIN) public String postProduct(String string) throws SQLException { JsonParser parse = Json.createParser(new StringReader(string)); Map<String, String> pdtmap = new LinkedHashMap<String, String>(); String key = ""; String value = ""; while (parse.hasNext()) { JsonParser.Event event = parse.next(); switch (event) { case KEY_NAME: key = parse.getString(); break; case VALUE_STRING: value = parse.getString(); pdtmap.put(key, value); break; case VALUE_NUMBER: value = parse.getString(); pdtmap.put(key, value); break; default: break; } } if (conn == null) { return "Not able to connect"; } else { String query = "INSERT INTO products (product_id,name,decription,quantity) VALUES (?,?,?)"; PreparedStatement prestmt = conn.prepareStatement(query); prestmt.setInt(1, Integer.parseInt(pdtmap.get("product_id"))); prestmt.setString(2, pdtmap.get("name")); prestmt.setString(3, pdtmap.get("description")); prestmt.setInt(4, Integer.parseInt(pdtmap.get("quantity"))); prestmt.executeUpdate(); return " the row has been inserted into the database"; } }
From source file:com.flexive.core.storage.MySQL.MySQLTreeStorage.java
/** * {@inheritDoc}/* w w w . ja v a 2 s. c o m*/ */ @Override public List<String> getLabels(Connection con, FxTreeMode mode, long labelPropertyId, FxLanguage language, boolean stripNodeInfos, long... nodeIds) throws FxApplicationException { List<String> ret = new ArrayList<String>(nodeIds.length); if (nodeIds.length == 0) return ret; PreparedStatement ps = null; ResultSet rs; try { ps = con.prepareStatement("SELECT tree_FTEXT1024_Chain(?,?,?,?)"); ps.setInt(2, (int) language.getId()); ps.setLong(3, labelPropertyId); ps.setBoolean(4, mode == FxTreeMode.Live); for (long id : nodeIds) { ps.setLong(1, id); try { rs = ps.executeQuery(); if (rs != null && rs.next()) { final String path = rs.getString(1); if (!StringUtils.isEmpty(path)) ret.add(stripNodeInfos ? stripNodeInfos(path) : path); else addUnknownNodeId(ret, id); } else addUnknownNodeId(ret, id); } catch (SQLException e) { if ("22001".equals(e.getSQLState())) { //invalid node id in MySQL addUnknownNodeId(ret, id); } else throw e; } } return ret; } catch (SQLException e) { throw new FxLoadException(LOG, e, "ex.db.sqlError", e.getMessage()); } finally { try { if (ps != null) ps.close(); } catch (Exception e) { //ignore } } }
From source file:com.imagelake.control.InterfaceDAOImp.java
@Override public boolean updateInteface(Interfaces inf) { boolean ok = false; try {/*from ww w . j a v a 2 s . co m*/ String sql = "UPDATE interfaces SET state=? WHERE url=?"; PreparedStatement ps = DBFactory.getConnection().prepareStatement(sql); ps.setInt(1, inf.getState()); ps.setString(2, inf.getUrl()); int i = ps.executeUpdate(); if (i > 0) { ok = true; } } catch (Exception e) { e.printStackTrace(); } return ok; }
From source file:dhbw.clippinggorilla.objects.user.UserUtils.java
/** * Get a User via given id/*www. jav a 2 s. co m*/ * * @param id The id of the requested User * @return The requested User (or a exception is thrown) */ public static User getUser(int id) throws UserNotFoundException { if (USERS_PER_ID.containsKey(id)) { User u = USERS_PER_ID.get(id); addUser(u); return u; } String sql = "SELECT * FROM " + Tables.USER + " WHERE " + Columns.ID + " = ?"; User u = new User(); try { PreparedStatement stat = Database.getConnection().prepareStatement(sql); stat.setInt(1, id); ResultSet result = stat.executeQuery(); if (result.next()) { u.setId(result.getInt(Columns.ID)); u.setUsername(result.getString(Columns.USERNAME)); u.setPassword(result.getString(Columns.PASSWORD)); u.setSalt(result.getString(Columns.SALT)); u.setEmail(result.getString(Columns.EMAIL)); u.setFirstName(result.getString(Columns.FIRST_NAME)); u.setLastName(result.getString(Columns.LAST_NAME)); u.setRegistrationDate(result.getTimestamp(Columns.REGISTRATION_DATE).toLocalDateTime()); u.setAccessLevel(result.getInt(Columns.STATUS)); u.setActivationKey(result.getString(Columns.ACTIVATION_KEY)); if (result.getInt(Columns.SEND_CLIPPING_MAIL) == 1) { u.setSendClippingMail(true); } else { u.setSendClippingMail(false); } u.setClippingTime(loadAllClippingSendTimes(u)); if (u.getClippingTime().size() < 1) { addClippingSendTime(u, LocalTime.of(8, 0)); } } else { throw new UserNotFoundException(); } } catch (SQLException ex) { throw new UserNotFoundException(); } UserUtils.addUser(u); u.setLastClipping(ClippingUtils.getLastClipping(u)); return u; }
From source file:com.redip.dao.impl.QualityNonconformitiesDocDAO.java
@Override public String addNonconformityDoc(QualityNonconformitiesDoc qualityNonconformityDoc) { String statusmessage = ""; final String sql = "INSERT INTO qualitynonconformitiesdoc ( idqualitynonconformities, " + "linkToDoc) values (?,?)"; Integer idqnc = qualityNonconformityDoc.getIdQualityNonconformities() == null ? 0 : qualityNonconformityDoc.getIdQualityNonconformities(); String linkToDoc = qualityNonconformityDoc.getLinkToDoc() == null ? "" : qualityNonconformityDoc.getLinkToDoc(); Logger.log(QualityNonconformitiesDocDAO.class.getName(), Level.INFO, "Connecting to jdbc/qualityfollowup from addNonconformityDoc"); Connection connection = this.databaseSpring.connect(); try {//ww w. j a v a 2 s . c o m PreparedStatement preStat = connection.prepareStatement(sql); try { preStat.setInt(1, idqnc); preStat.setString(2, linkToDoc); int res = preStat.executeUpdate(); try { if (res > 0) { statusmessage = StatusMessage.SUCCESS_NONCONFORMITYCREATED; } else { statusmessage = StatusMessage.ERROR_NONCONFORMITYCREATEDCREATED; } } catch (Exception exception) { Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString()); } } catch (SQLException exception) { Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.INFO, "Disconnecting to jdbc/qualityfollowup from addNonconformity"); connection.close(); } } catch (SQLException e) { Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.WARN, e.toString()); } } return statusmessage; }
From source file:test.other.T_connection.java
@Test public void test() throws DataAccessException, SQLException { // ResultSetMetaData metaData = rs.getMetaData(); ///*from w w w . j a va 2s . co m*/ // int count = metaData.getColumnCount(); // for (int i = 1; i <= count; i++) // { // System.out.println(i + " " + metaData.getColumnName(i)); // } // "insert into FC_User(" + // "id_photo, name_login, name_display, password, tagline, appendix)" + // " values( " + user.id_photo + ", ?, ?, ?, ?, ?)" // ); // PreparedStatement ps = conn.prepareStatement("insert into FC_User(" + "id_photo, name_login, name_display, password, tagline, appendix)" + " values(?, ?, ?, ?, ?, ?)"); ps.setInt(1, 2); ps.setString(2, "login"); ps.setString(3, null); ps.setString(4, "psw"); ps.setString(5, null); ps.setString(6, null); ps.executeUpdate(); // @SuppressWarnings("unchecked") // List<FC_User> list = mapper.extractData(rs); // for (FC_User v_User : list) { // System.out.println(v_User.name_login); // } // ' or '1'='1 // PreparedStatement ps1 = conn.prepareStatement( // "select * from v_user where name_login = ?"); // ps1.setString(1, "' or '1' = '1"); //System.out.println(ps1.toString()); // rs = ps1.executeQuery(); // list = mapper.extractData(rs); //System.out.println("====after:"); // for (FC_User v_User : list) { // System.out.println(v_User.name_login); // } }
From source file:oobbit.orm.Comments.java
public List<Comment> getAllForLinkWithUser(int linkId, int limit) throws SQLException { PreparedStatement statement = getConnection().prepareStatement( "SELECT * FROM `comments` AS q1 LEFT JOIN users AS q2 ON q1.creator = q2.user_id WHERE q1.link_id = ? LIMIT ?;"); statement.setInt(1, linkId); statement.setInt(2, limit);/*from w w w . j av a2s . com*/ return parseResults(statement.executeQuery()); }
From source file:biospectra.taxdb.TaxonDB.java
public Taxonomy getTaxonomyByGI(int gi) throws Exception { Taxonomy tax = null;// ww w. j a v a 2 s . c om PreparedStatement pstmt = null; try { pstmt = this.connection.prepareStatement(QUERY_BY_GI); pstmt.setInt(1, gi); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { tax = new Taxonomy(rs.getInt("taxid"), rs.getString("name"), rs.getInt("parent"), rs.getString("rank")); break; } } catch (SQLException ex) { throw ex; } finally { if (pstmt != null) { pstmt.close(); } } return tax; }
From source file:fll.web.playoff.Playoff.java
/** * Get the list of playoff brackets at the specified tournament as a List of * Strings.//from w ww .ja v a2 s . co m * * @param connection the database connection * @return the List of brackets sorted by bracket name * @throws SQLException on a database error */ public static List<String> getPlayoffBrackets(final Connection connection, final int tournament) throws SQLException { final List<String> list = new LinkedList<String>(); PreparedStatement prep = null; ResultSet rs = null; try { prep = connection.prepareStatement( "SELECT DISTINCT bracket_name FROM playoff_bracket_teams WHERE tournament_id = ? ORDER BY bracket_name"); prep.setInt(1, tournament); rs = prep.executeQuery(); while (rs.next()) { final String division = rs.getString(1); list.add(division); } } finally { SQLFunctions.close(rs); SQLFunctions.close(prep); } return list; }