I am new to Java, and my Work is all related to JDBC - about inserting and processing data. Over all its working fine.
To reduce code am using single try{} catch()
block to write multiple JDBC
Statements
and Prepared Statements
.
Example Code:
public void dashboardReports()
{
try {
String total_stock_value="select sum(price*closingstock)as tsv from purchase_table";
Statement ps_tsv=connection.createStatement();
ResultSet set_tsv=ps_tsv.executeQuery(total_stock_value);
if(set_tsv.next())
{
total_stock.setText(set_tsv.getString("tsv"));
}
String tota_sales="select sum(INVOICE_VALUE) as iv from PARTYWISE_ACCOUNTS_LEDGER";
Statement st_total_sales=connection.createStatement();
ResultSet set_total_sales=st_total_sales.executeQuery(tota_sales);
if(set_total_sales.next())
{
total_sales.setText(set_total_sales.getString("iv"));
}
String total_purchases="select sum(CP_INVOICEVALUE)as cpi from COMPANY_PAYMENTS";
Statement st_tps=connection.createStatement();
ResultSet set_tps=st_tps.executeQuery(total_purchases);
if(set_tps.next())
{
total_purchases_label.setText(set_tps.getString("cpi"));
}
String total_collectionss="select sum(PAYMENT_REC) as payrec from PARTYWISE_ACCOUNTS_LEDGER";
Statement ps_toco=connection.createStatement();
ResultSet set_toco=ps_toco.executeQuery(total_collectionss);
if(set_toco.next())
{
total_collections.setText(set_toco.getString("payrec"));
}
String total_payments="select sum(CP_PAYMENTREC) as paid from COMPANY_PAYMENTS";
Statement ps_topa=connection.createStatement();
ResultSet set_topa=ps_topa.executeQuery(total_payments);
if(set_topa.next())
{
total_payments_label.setText(set_topa.getString("paid"));
}
} catch (Exception e) {
// TODO: handle except
}
}
So is this Good Way to Handle or another other way?
As of now my code is working very fine, do we have any future problems with this kind of approach.