Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hope this will be helpful. </p> <p>When the <code>Table</code> class is initialized, all the input values will be initialized. When <code>getSelectQuery</code> method is called, based on inputs select query will be formed. And <code>populatePreparedSt</code> will be called to set PreparedStatement. Finally based on the inputs, you set in constructor of <code>Table</code>, you will get the query and result.</p> <p>Table class:</p> <pre><code>public class TableRow { private String attibute1; private String attibute2; private int id; // getter and setters public final String selectQuery = " SELECT ID, ATTIBUTE1, ATTIBUTE2 FROM TABLEROW "; public CECardLogRow(int id, String attibute1, String attibute2) { this.id = id; this.attibute1 = attibute1; this.attibute2 = attibute2; } public String getSelectQuery () { StringBuilder sql = new StringBuilder(); boolean whereClauseAdded = false; sql = sql.append(selectQuery); if (id &gt; -1) { sql.append(" WHERE ID = ? "); whereClauseCt++; } if (attibute1 != null &amp;&amp; attibute1.trim() != "") { sql.append(whereClauseAdded ? " AND " : " WHERE "); sql.append(" ATTIBUTE1 = ? "); } if (attibute2 != null &amp;&amp; attibute2.trim() != "") { sql.append(whereClauseAdded ? " AND " : " WHERE "); sql.append(" ATTIBUTE2 = ? "); } return sql.toString(); } public void populatePreparedSt (PreparedStatement ps) { int i = 1; if (id &gt; -1) { ps.setInt(i++, id); } if (attibute1 != null &amp;&amp; attibute1.trim() != "") { ps.setString(i++, attibute1); } if (attibute2 != null &amp;&amp; attibute2.trim() != "") { ps.setString(i++, attibute2); } } } </code></pre> <p>Calling method:</p> <pre><code>public List&lt;TableRow&gt; getData (int id, String attibute1, String attibute2) { List&lt;TableRow&gt; rows = new ArrayList&lt;TableRow&gt;(); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { TableRow row = new TableRow(id, attibute1, attibute2); String sql = row.getSelectQuery(); con = global.getConnection(); // get connection ps = con.prepareStatement(sql); row.populatePreparedSt(ps); rs = ps.executeQuery(); while (rs.next()) { TableRow row = new TableRow(); // get data row.setId(rs.getInt("ID"));// and so on rows.add(tableRow); } } catch (SQLException sqe) { throw sqe; } finally() { // closeConnection } return rows; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload