Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package budget; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javafx.scene.control.TextArea; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; /** * * @author aanpilogov */ public class util { public static Date convertStringToDate(String string) { try { return new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string); } catch (ParseException ex) { Logger.getLogger(util.class.getName()).log(Level.SEVERE, null, ex); } return null; } public static String convertDateToString(Date date) { return new SimpleDateFormat("yyyy-MM-dd").format(date); } public static java.sql.Timestamp getTimestamp() { return new java.sql.Timestamp(System.currentTimeMillis()); } public static double convertStringAmountToDouble(String stringAmount) { String out = stringAmount.replace("+", ""); out = out.replace("$", ""); out = out.replace(",", ""); out = out.replace(" dollars and", ""); out = out.replace(" Cents", ""); out = out.replace("(pending)", ""); return Double.parseDouble(out); } public static String rotateDate(String string, String mode) { Date date = null; try { if (null != mode) { switch (mode) { case "MMMM dd": date = new SimpleDateFormat("MMMM-dd-yyyy", Locale.ENGLISH) .parse(string.replace(" ", "-") + (new SimpleDateFormat("yyyy").format(new Date()))); break; case "MMMM\ndd": date = new SimpleDateFormat("MMMM-dd-yyyy", Locale.ENGLISH).parse( string.replace("\n", "-") + "-" + (new SimpleDateFormat("yyyy").format(new Date()))); break; default: date = new SimpleDateFormat("MM/dd/yy", Locale.ENGLISH).parse(string); break; } } return new SimpleDateFormat("yyyy-MM-dd").format(date); } catch (ParseException ex) { Logger.getLogger(util.class.getName()).log(Level.SEVERE, null, ex); } return null; } public static String prepareTextForQuery(String inText) { String outText = inText.replace("'", ""); outText = outText.replace("#", ""); return outText; } @SuppressWarnings("empty-statement") public static String getAccountName(Statement stmt, int code) { ResultSet rsER; try { rsER = stmt.executeQuery("SELECT NAME FROM ACCOUNTS WHERE ID = " + code); if (rsER.next()) { return rsER.getString("Name"); } } catch (SQLException ex) { } ; return ""; } public static void addTransaction(Boolean isBuffered, Statement stmt, String date, int account, String Category, String Analytics, Double amount, Boolean isPending, TextArea ta) { String analytics = Analytics.replaceAll("\\s+", " "); if (isBuffered) addBatch(stmt, "INSERT INTO TransactionsBuffer VALUES('" + date + "'," + account + ", '" + Category + "', '" + analytics + "', " + amount + ", " + isPending + ")"); else addBatch(stmt, "INSERT INTO Transactions VALUES('" + date + "'," + account + ", '" + Category + "', '" + analytics + "', " + amount + ", " + isPending + ")"); trace("Transaction added: " + date + "/" + getAccountName(stmt, account) + "/" + Category + "/" + analytics + "/" + amount + "/" + isPending, ta); } public static void addTotal(Statement stmt, java.sql.Timestamp timestamp, int account, Double amount, TextArea ta) { addBatch(stmt, "INSERT INTO TotalsBuffer VALUES('" + timestamp + "'," + account + ", " + amount + ")"); trace("Total added: " + timestamp + "/" + getAccountName(stmt, account) + "/" + amount, ta); } public static void addBatch(Statement stmt, String query) { try { stmt.addBatch(query); } catch (SQLException ex) { Logger.getLogger(util.class.getName()).log(Level.SEVERE, null, ex); } } public static void executeBatch(Statement stmt, TextArea ta) { try { stmt.executeBatch(); } catch (SQLException ex) { Logger.getLogger(BudgetOverviewController.class.getName()).log(Level.SEVERE, null, ex); trace(ex.toString(), ta); } } public static void clearBatch(Statement stmt) { try { stmt.clearBatch(); } catch (SQLException ex) { Logger.getLogger(util.class.getName()).log(Level.SEVERE, null, ex); } } public static void trace(String text, TextArea ta) { ta.appendText(new Date().toString() + ": " + text + "\n"); } public static void waitForElement(WebDriverWait wait, By locator) { wait.until(ExpectedConditions.visibilityOfElementLocated(locator)); } public static void waitAndClick(WebDriver driver, WebDriverWait wait, By locator) { wait.until(ExpectedConditions.elementToBeClickable(locator)); driver.findElement(locator).click(); } public static void sleep(int time) { try { Thread.sleep(time); } catch (InterruptedException ex) { Logger.getLogger(WebDriverManager.class.getName()).log(Level.SEVERE, null, ex); } } }