public static void main(String args[]) { String database = "tmcprogram3"; String url = "jdbc:mysql://localhost:3306/" + database + "?zeroDateTimeBehavior=convertToNull"; String username = "******"; String password = "******"; boolean result = MySqlDBHelper.init(url, username, password); ArrayList<InvoiceTemplate> items = InvoiceTemplate.select(""); for (InvoiceTemplate item : items) { System.out.println(item); } System.out.println(InvoiceTemplate.count("")); }
// -----------database helper functions-------------- public static String implodeValues(InvoiceTemplate item, boolean withId) { ArrayList<String> values = item.implodeFieldValuesHelper(withId); String output = ""; for (String value : values) { if (!output.isEmpty()) output += ","; output += "'" + value + "'"; } return output; }
public static String implodeFieldsWithValues(InvoiceTemplate item, boolean withId) { ArrayList<String> values = item.implodeFieldValuesHelper( true); // get entire list of values; whether the id is included will be dealt with // later. if (values.size() != fields.length) { System.err.println( "InvoiceTemplate:implodeFieldsWithValues(): ERROR: values length does not match fields length"); } String output = ""; for (int i = 0; i < fields.length; i++) { if (!withId && fields[i].contentEquals("id")) continue; if (!output.isEmpty()) output += ","; output += fields[i] + "='" + values.get(i) + "'"; } return output; }
public static void update(InvoiceTemplate item) { Connection conn = MySqlDBHelper.getInstance().getConnection(); Statement st = null; boolean withid = false; try { st = conn.createStatement(); st.executeUpdate( "update " + tablename + " set " + implodeFieldsWithValues(item, false) + " where id = '" + item.getId().toString() + "';"); } catch (SQLException ex) { Logger.getLogger(InvoiceTemplate.class.getName()).log(Level.SEVERE, null, ex); ex.printStackTrace(); } }
public void save() { if (id == null || id == 0) InvoiceTemplate.insert(this); else InvoiceTemplate.update(this); }
public void delete() { InvoiceTemplate.delete(this); }
public static void delete(InvoiceTemplate item) { delete(item.getId()); }