Imp code modification required - java

Following code needs modification can we use constants files if yes then how we can segregate string in separate files so that loop continues then performance is not impacted
for (int j = 0; j < subconfigListRLC.size(); j++) {
StringBuffer sqlQuery = new StringBuffer();
test = (SubConfigurationDetailsObject) subconfigListRLC.get(j);
if (test.getFlag().equalsIgnoreCase("T")) {
sqlQuery = sqlQuery.append("update SUB_CONFIG set TSTED = "
+ test.getSubConfigurationIndexNo() + " , Q_SUB_INDX = 0 "
+ "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
+ "AND MODEL_YEAR = '" + modelYear + "' "
+ "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
+ "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
+ "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
+ "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
+ "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
+ "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
} else if (test.getFlag().equalsIgnoreCase("U")) {
sqlQuery = sqlQuery.append("update SUB_CONFIG set Q_SUB_INDX = "
+ test.getSubConfigurationIndexNo() + " "
+ "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
+ "AND MODEL_YEAR = '" + modelYear + "' "
+ "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
+ "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
+ "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
+ "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
+ "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
+ "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
}
//System.out.println("Query----------->"+sqlQuery.toString());
processor.getUpdateAccessor().executeUpdateSql(sqlQuery.toString());
}

Use a Java PreparedStatement in order to build the query. You can then store the query string for the PreparedStatement in a properties file. Read the two possible query strings into variables before entering the loop (in fact you may want to build both PreparedStatements before entering the loop - depending on whether you always use them both). You can then call clearParamaters, then set your new parameters, execute, repeat.

As you asked for the exact details, something like this. Search for javadocs on PreparedStatement. Javadocs are always worth reading.
String sql = "update SUB_CONFIG set TSTED = ? , Q_SUB_INDX = ? " +
"where BASE_ENG_KEY = ? " +
"AND MODEL_YEAR = ? " +
"AND RLHP_LVW = ? " +
"AND LVW_TEST_WT_WO_CONT = ? " +
"AND INERTIA_WT_CLASS = ? " +
"AND TEST_GROUP_ID = ? " +
"AND ENGINE_CODE = ? " +
"AND AXLE_RATIO = ?");
PreparedStatement statement = connection.createPreparedStatement(sql);
for (SubConfigurationDetailsObject test: subconfigListRLC) {
if (test.getFlag().equalsIgnoreCase("T")) {
statement.setIntParam(1, test.getSubConfigurationIndexNo());
statement.setIntParam(2, 0);
} else if (test.getFlag().equalsIgnoreCase("U")) {
statement.setIntParam(1, 0);
statement.setIntParam(2, test.getSubConfigurationIndexNo());
} else {
continue;
}
statement.set...Param(3, ...);
...
statement.executeUpdate();
}
You may probably only need one PreparedStatement, and the result is indeed faster.
BTW. better use StringBuilder instead of StringBuffer.

Related

CASE expression in JPQL

In my SpringBoot project I have CrudRepository with next method:
public interface LoanRequestRepository extends CrudRepository<LoanRequest, UUID> {
#Query(
value = "select " +
"lr.id as id," +
"lr.state as state," +
"lr.iban as iban," +
"lr.amount as amount," +
"lr.rate as rate," +
"lr.term as term," +
"lr.best_offer as best_offer," +
"lr.contract_number as contract_number," +
"lr.created as created," +
"lr.company_id as company_id," +
"lr.closed as closed," +
"lr.loan_colvir_id as loan_colvir_id," +
"lr.contract_date as contract_date," +
"lr.first_payment_date as first_payment_date, " +
"lr.gesv as gesv, " +
"c.id as comp_id," +
"c.name as comp_name," +
"c.bin as comp_bin," +
"c.colvir_id as comp_colvir_id," +
"c.kod as comp_kod," +
"c.type as comp_type," +
"c.location_address as comp_location_address," +
"c.registration_address as comp_registration_address," +
"c.legal_address as comp_legal_address," +
"c.actual_address as comp_actual_address," +
"c.department_code as comp_department_code," +
"c.department_name as comp_department_name," +
"c.resident as comp_resident," +
"c.created as comp_created," +
"c.registration_date as comp_registration_date," +
"c.economic_sector as comp_economic_sector," +
"c.short_name as comp_short_name," +
"c.colvir_reference_id as comp_colvir_reference_id," +
"c.card_system_id as comp_card_system_id," +
"c.initial_registration as comp_initial_registration " +
"from loan_request lr " +
"left join company c on c.id = lr.company_id " +
"where 1=1 " +
"and case when :amount is not null " +
" then lr.amount = :amount " +
" else 1=1 end " +
"and case when cast(:dateFrom as date) is not null " +
" then lr.created >= cast(:dateFrom as date)" +
" else 1=1 end " +
"and case when cast(:dateTo as date) is not null " +
" then lr.created <= cast(:dateTo as date) " +
" else 1=1 end " +
"and case when :bin is not null " +
" then c.bin like concat('%', :bin, '%') " +
" else 1=1 end " +
"and case when :companyName is not null " +
" then lower(c.name) like lower(concat('%', :companyName, '%')) " +
" else 1=1 end " +
" group by lr.id, c.id " +
"order by " +
" CASE WHEN :isAscending = true THEN :orderBy END ASC, " +
" CASE WHEN :isAscending = false THEN :orderBy END DESC " +
"limit :size offset :page;")
Iterable<LoanRequestDto> findAllLoanRequestDtoByFilters(#Param("dateFrom") LocalDate dateFrom,
#Param("dateTo") LocalDate dateTo,
#Param("bin") String bin,
#Param("amount") BigDecimal amount,
#Param("companyName") String companyName,
#Param("page") Integer page,
#Param("size") Integer size,
#Param("orderBy") String orderBy,
#Param("isAscending") Boolean isAscending);
}
All query works well, but it ignores "order by" with case expression and list comes in inordered way. I am sending values as "lr.iban", "c.name" to orderBy parameter, and if translate it to sql, it works. But in JPQL it does not work.
So, where am I wrong? How can I solve it?
Also, if change last part with CASE expression to the next:
order by CASE WHEN :isAscending = true THEN c.name END ASC
It also works, well in JPQL. List comes in ordered way.

problem while using database column value into Date() sqlite android

i have small problem, i googled it many times but i couldn't get it...
i'm using a query (sqlite) to retrieve some data ..
in this query their is Date() used to increase certain date dynamic number of days (coming from column in the same table) ..
if i put this days static (1,2,3,.....) it works fine but if i put column name it doesn't.
this query fails and i want it to work:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";
this works fine :
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "','2 day') ";
where
targetDate: date entered by user to get events of that date
DATE: date of every event in the table
REPETETIONS: dynamic number (column in the same table)
the problem is using REPETITIONS in the date function...
create statement
String SQL_CREATE_EVENT_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TITLE + " TEXT ," +
DATE + " TEXT , " +
IS_NOTIFY + " INTEGER , " +
NOTIFICATION_TIME + " TEXT ," +
REPEAT + " INTEGER ," +
REPEAT_DURATION + " INTEGER ," +
REPETITIONS + " INTEGER ," +
CERTAIN_DATE + " TEXT ," +
NOTE + " TEXT ," +
IS_SPOKEN + " INTEGER " +
" );";
and this is the selecting statement
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE " + DATE + " = '" + targetDate
+ "' OR (( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '0' ) AND " + DATE + " <= '" + targetDate + "')"
+ " OR( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '3' ) AND " + DATE + " <= '" + targetDate + "' AND " + CERTAIN_DATE + " >= '" + targetDate + "'"
+ " OR (" + REPEAT + " = '1' AND " + REPEAT_DURATION + "= '2' ) AND " + targetDate + " >= '" + DATE + "' AND "+ targetDate+ " <= date('" + DATE + "','" + REPETITIONS + " days')";
The variable REPETITIONS in this statement:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";
should be a number and not a column name.
The date function in SQLite has various syntaxes but you use this one:
SELECT date('2014-10-23','+7 day');
you must supply a number before day.
Edit try this:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "', " + REPETITIONS + " || ' day') ";
In sqlite date() function, the NNN days is a string modifier and not an expression. Therefore you cannot use column names or even || string concatenation there.
You can format the modifier string in your code and place it in the SQL. This requires additional query to the database, which is likely not what you want.
If you can assume each day is 24 hours (which is not always true, consider e.g. DST events), you could try something like
... date(strftime('%s', '" + targetDate + "', " + REPETITIONS + "*24*60*60, 'unixepoch') ...
where strftime('%s', ...) converts to seconds and date(..., 'unixepoch') converts back to datestamp.
But seriously I'd consider redesigning the schema to better support your functional requirements.

Need to optimize the query to insert the record

I wrote a piece of JDBC template code, which inserts the record in the table, but the problem is my execution is stuck on this particular snippet, it seems some kind of hang up. I didn't figure out the cause as query properly running in sqldeveloper
List<SalaryDetailReport> reports = salaryDetailReportDAO.findAll(tableSuffix, regionId, circleId);
// the above line find the required data, if data is found then it proceeds
if (reports != null && reports.size() > 0) {
for (SalaryDetailReport salaryDetail : reports) {
try {
SalaryDetail sd = new SalaryDetail();
sd.setDetailReport(salaryDetail);
salaryDetailDAO.save(sd, tableSuffix);
} catch (Exception e) {
log.error("Error occured", e);
e.printStackTrace();
throw new MyExceptionHandler(" Error :" + e.getMessage());
}
}
System.out.println("data found");
} else {
log.error("Salary Record Not Found.");
throw new MyExceptionHandler("No record Found.");
}
You people saw try-catch , my execution stuck inside try and catch and here is the insertion code in my implementation class. when i commented the above code then my application works fine, but why my application stuck here, I am not able to figure it out, kindly help me
#Override
public void save(SalaryDetail details, String tableSuffix) {
String tabName = "SALARY_DETAIL_" + tableSuffix;
// String q = "INSERT INTO " + tabName + "(ID "
String q = "INSERT INTO SALARY_DETAIL_TBL "
+ " (ID "
+ " ,EMP_NAME "
+ " ,EMP_CODE "
+ " ,NET_SALARY "
+ " ,YYYYMM "
+ " ,PAY_CODE "
+ " ,EMP_ID "
+ " ,PAY_CODE_DESC "
+ " ,REMARK "
+ " ,PAY_MODE ) "
+ " (SELECT (sd.SALARY_REPORT_ID) ID "
+ " ,(sd.emp_name) emp_name "
+ " ,(sd.EMP_CODE) EMP_CODE "
+ " ,(sd.amount) NET_SALARY "
+ " ,(sd.YYYYMM) YYYYMM "
+ " ,(sd.pay_code) pay_code "
+ " ,(sd.emp_id) emp_id "
+ " ,(sd.PAY_CODE_DESC) PAY_CODE_DESC "
+ " ,(sd.REMARK) REMARK "
+ " ,(sd.PAY_MODE)PAY_MODE "
// + " FROM SALARY_DETAIL_REPORT_" + tableSuffix + " sd "
+ " FROM SALARY_DETAIL_REPORT_TBL sd "
+ " WHERE sd.PAY_CODE = 999 "
+ " AND sd.EMP_ID IS NOT NULL "
// + " AND sd.EMP_ID NOT IN (SELECT EMP_ID FROM SALARY_DETAIL_" + tableSuffix + ") "
+ " AND sd.EMP_ID NOT IN (SELECT EMP_ID FROM SALARY_DETAIL_TBL) "
+ " ) ";
MapSqlParameterSource param = new MapSqlParameterSource();
param.addValue("id", details.getId());
param.addValue("EMP_NAME", details.getEmpName());
param.addValue("EMP_CODE", details.getEmpCode());
param.addValue("NET_SALARY", details.getNetSalary());
param.addValue("GROSS_EARNING", details.getGrossEarning());
param.addValue("GROSS_DEDUCTION", details.getGrossDeduction());
param.addValue("YYYYMM", details.getYyyymm());
param.addValue("EMP_ID", details.getEmployee() != null ? details.getEmployee().getEmpId() : null);
KeyHolder keyHolder = new GeneratedKeyHolder();
getNamedParameterJdbcTemplate().update(q, param);
// details.setId(((BigDecimal) keyHolder.getKeys().get("ID")).longValue());
}
The main problem is in your query is Not In condition. It will degrade your performance. Try to fetch the "SELECT EMP_ID FROM SALARY_DETAIL_TB" in a separate query and pass in the Not in block in the main query. This will increase the performance of your query. Every time a save is performed this will fire the select query every time.
You have to decide whether you will insert records from SELECT or from the application.
If you don't need to manipulate with data after their select then you can simply call one INSERT INTO SELECT statement without any for cycle. It will be fast because of the only one INSERT statement call.
So you will implement method like copyAllInSalaryDetail(tableSuffix, regionId, circleId) in your SalaryDetailReportDAO and that method will execute INSERT INTO salary_detail_tbl... (...) (SELECT ... WHERE ...) using the same WHERE condition as you have in findAll() method. All inserts will be done only on the Database layer.
If you want to manipulate with data before their insert you can continue with your approach using SalaryDetail bean and for cycle, but you should remove the SELECT part from the INSERT statement and use values from the provided bean. Then the save() method can look like:
#Override
public void save(SalaryDetail details, String tableSuffix) {
// use tableSuffix if it is really needed
String q = "INSERT INTO SALARY_DETAIL_TBL "
+ " (ID "
+ " ,EMP_NAME "
+ " ,EMP_CODE "
+ " ,NET_SALARY "
+ " ,YYYYMM "
+ " ,PAY_CODE "
+ " ,EMP_ID "
+ " ,PAY_CODE_DESC "
+ " ,REMARK "
+ " ,PAY_MODE ) "
+ " VALUES (:id "
+ " ,:emp_name "
+ " ,:emp_code "
+ " ,:net_salary "
+ " ,:yyyymm "
+ " ,:pay_code "
+ " ,:emp_id "
+ " ,:pay_code_desc "
+ " ,:remark "
+ " ,:pay_mode)";
MapSqlParameterSource param = new MapSqlParameterSource();
// KeyHolder keyHolder = new GeneratedKeyHolder();
// details.setId(((BigDecimal) keyHolder.getKeys().get("ID")).longValue());
param.addValue("id", details.getId());
param.addValue("emp_name", details.getEmpName());
param.addValue("emp_code", details.getEmpCode());
param.addValue("net_salary", details.getNetSalary());
param.addValue("pay_code", details.getPayCode());
param.addValue("pay_code_desc", details.getPayCodeDesc());
param.addValue("pay_mode", details.getPayMode());
param.addValue("remark", details.getPayRemark());
param.addValue("yyyymm", details.getYyyymm());
param.addValue("emp_id", details.getEmployee() != null ? details.getEmployee().getEmpId() : null);
getNamedParameterJdbcTemplate().update(q, param);
}

SQL Query containing Timestamp Date comparison

I have a table - event_log which has a field in Timestamp format. Some of the example entries are - '2016-01-28 12:37:48' , '2016-01-28 15:26:51'
Now I want to write a query that will select some rows between startTime and endTime. When I fire this Query in an SQL client
SELECT Time,Data
FROM event_log
WHERE Data IN ('player disconnected','Player connected to server')
AND Player_Details_ID = '1'
AND Time > '2016-01-28 11:46:49' AND Time < '2016-02-09 14:38:39'
ORDER BY Time;
it gives me the desired output. But when I write a similar query in Java it fails to give me the result.
sqlQuery = "Select e.Time,e.Data"
+ " from event_log e "
+ "where e.Data IN ('player disconnected','Player connected to server') "
+ "and e.Player_Details_ID = '1' and "
+ "e.TIME > " + "' + startSqlDate + '" + " and e.TIME < " + "' + endSqlDate + '"
+ " order by e.Time";
Where am I going wrong?
Regardless of the values of any 'startSqlDate' and 'endSqlDate' variables you may have defined, the query submitted to the database will invariably be:
Select e.Time,e.Data from event_log e where e.Data IN ('player disconnected','Player connected to server') and e.Player_Details_ID = '1' and e.TIME > ' + startSqlDate + ' and e.TIME < ' + endSqlDate + ' order by e.Time
That is because your variables names are in the string. Change it to this:
"Select e.Time,e.Data" + " from event_log e " + "where e.Data IN ('player disconnected','Player connected to server') " + "and e.Player_Details_ID = '1' and " + "e.TIME > '" + startSqlDate + "' and e.TIME < '" + endSqlDate + "' order by e.Time"
Your 5th line should be:
+ "e.TIME > '" + startSqlDate + "' and e.TIME < '" + endSqlDate + "'"
(Provided your variables are Strings in the right format.)
Your version doesn't use your variables at all, but compiles to a static String.
"e.TIME > " + "' + startSqlDate + '" + " and e.TIME < " + "' + endSqlDate + '"
instead of this , try below
"e.TIME > '"+ startSqlDate + "' and e.TIME < '"+ endSqlDate + '"+ " order by e.Time";
In my opinion you need to write \' than '.
This should avoid some problems with strings.
Also as you can see you have variable names in string.
Something like that should work:
sqlQuery = "SELECT e.Time,e.Data"
+ " FROM event_log e "
+ "WHERE e.Data IN (\'player disconnected\',\'Player connected to server\') "
+ "AND e.Player_Details_ID = \'1\' AND "
+ "e.TIME > " + "\'" + startSqlDate + "\'" + " AND e.TIME < " + "\'" + endSqlDate + "\'"
+ " ORDER BY e.Time";

Can't figure out why this big double inner join isn't working, but when split it does

Here is my queries split up that work perfectly fine...
String sqlstatement = "SELECT WBLinkWebsiteID, WBLinkCategoryParentID, WBLinkTitle, WBLinkURL FROM WEBSITECATEGORY_TABLE WHERE WBLinkCategoryID = ?";
String[] args = { CategorySubID };
Part 2
sqlstatement = "SELECT LOCATIONS_TABLE.LocationWebsiteID, "
+ "LOCATIONS_TABLE.locationCity, "
+ "LOCATIONS_TABLE.locationState, "
+ "LOCATIONS_TABLE.locationCountry, LOCATIONS_TABLE.locationType, "
+ "LOCATIONS_TABLE.locationUrl, "
+ "PREF_TABLE.Pref_SavedTitle "
+ "FROM PREF_TABLE INNER JOIN "
+ "LOCATIONS_TABLE ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID "
+ "WHERE "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "' ORDER BY LOCATIONS_TABLE.locationState, LOCATIONS_TABLE.locationCity";
Now here is my attempt to combine the two instead of just having 2 go in row back to back and burn time/resources...
String NewSqlstatement = "SELECT LOCATIONS_TABLE.LocationWebsiteID, "
+ "LOCATIONS_TABLE.locationCity, "
+ "LOCATIONS_TABLE.locationState, "
+ "LOCATIONS_TABLE.locationCountry, "
+ "LOCATIONS_TABLE.locationUrl, "
+ "LOCATIONS_TABLE.LocationID, "
+ "PREF_TABLE.Pref_SavedTitle, "
+ "WEBSITECATEGORY_TABLE.WBLinkTitle, "
+ "WEBSITECATEGORY_TABLE.WBLinkURL "
+ "FROM PREF_TABLE INNER JOIN "
+ "LOCATIONS_TABLE ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID "
+ "INNER JOIN WEBSITECATEGORY_TABLE "
+ "ON WEBSITECATEGORY_TABLE.WBLinkWebsiteID = PREF_TABLE.Pref_WebsiteID "
+ "WHERE "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "'";
Now when I try to do my "SINGLE" way it keeps returning the WHOLE database of Locations in the LOCATIONS_TABLE query. It doesn't do just the exact ones I need.
I know the query works, because I have tested it here: http://sqlfiddle.com/#!6/ede97/2
Now I know my example on sqlfiddle is using MS Server 2014, but I assumed the syntax should be pretty much the same since its just standard SELECT with inner joins, but I could be wrong?
Anyone know what I'm doing wrong? Any help is greatly appreciated
EDIT - Fixed the SQLFIDDLE, I put the wrong statement in the example
Aren't you missing your filter on WBLinkCategoryID in the combined query. Shouldn't you have this:
...
+ "INNER JOIN WEBSITECATEGORY_TABLE "
+ "ON WEBSITECATEGORY_TABLE.WBLinkWebsiteID = PREF_TABLE.Pref_WebsiteID "
+ "WHERE "
+ "WEBSITECATEGORY_TABLE.WBLinkCategoryID IN (<value1>,...,<valueN>) AND "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "'";

Categories

Resources