Skip to content

Instantly share code, notes, and snippets.

@anozimada
Created November 28, 2017 07:11
Show Gist options
  • Save anozimada/d717523951b7f85b524ded2113e47695 to your computer and use it in GitHub Desktop.
Save anozimada/d717523951b7f85b524ded2113e47695 to your computer and use it in GitHub Desktop.
// http://www.adempiere.com/ADempiere_Best_Practices#How_to_use_PreparedStatement.2FResultSet_.3F
// How to use PreparedStatement/ResultSet ?
final String sql = "''your SQL SELECT code''";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, trxName);
DB.setParameters(pstmt, new Object[]{...''parameters''...});
rs = pstmt.executeQuery();
while(rs.next())
{
''// process fetched row''
}
}
// If your method is not throwing Exception or SQLException you need this block to catch SQLException
// and convert them to unchecked DBException
catch (SQLException e)
{
throw new DBException(e, sql);
}
// '''ALWAYS''' close your ResultSet in a finally statement
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment