MySql Insert Into JDBC - mysql

I'm trying to execute a query using JDBC in mysql using INSERT INTO SELECT
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/new_db?&user=root&password=");
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
String sql
= "INSERT INTO `tr_tmp`(`id`, `root_group_id`, `IATANO`, `NRPB`, `PlatedCarrierCode`, `PlatedCarrierName`, "
+ "`DateOfIssue`, `DomInt`, `FlownCarrierCode`, `FlownCarrierName`, `FlightNo`, `OperatedCarriercode`, "
+ "`FlightDate`, `Class`, `Origin`, `Destination`, `CashAmt`, `ExCashAmt`, `CreditAmt`, `EXCreditAmt`, "
+ "`EffCommAmt`, `EXEffCommAmt`, `RefCashAmt`, `RefCreditAmt`, `RefEffCommAmt`, "
+ "`ACMCashAmt`, `ACMCreditAmt`, `ACMEffCommAmt`, `ADMCashAmt`, `ADMCreditAmt`, `ADMEffCommAmt`, "
+ "`Farebasis`, `TaxCashAmt`, `EXTaxCashAmt`, "
+ "`TaxCreditAmt`, `EXTaxCreditAmt`, `RefTaxCashAmt`, `RefTaxCreditAmt`, "
+ "`ACMTaxCashAmt`, `ACMTaxCreditAmt`, `ADMTaxCashAmt`, `ADMTaxCreditAmt`, "
+ " `YQTax`, `EXYQtax`, `YRTax`, `EXYRtax`, `Currency`, NFCode) "
+ "(SELECT null, 100 as groupId, a.agtn as `IATANO`, b.nrid as NRPB, m.CarrierCode as PlatedCarrierCode, m.CarrierName as PlatedCarrierName, "
+ "c.DAIS as DateOfIssue, c.STAT as DomInt,j.CARR as FlownCarrierCode , l.CarrierName as FlownCarrierName, TRIM(LEADING '0' FROM j.FTNR) as FlightNo, j.OP_CARR as OP_CARR, "
+ "j.FLIGHTDT as FlightDate, j.RBKD as Class, j.ORAC AS Origin, j.DSTC AS Destination, k.CashAmt, k.ExCashAmt, k.CreditAmt, k.EXCreditAmt, "
+ "k.EffCommAmt as EffCommAmt,k.EXEffCommAmt as EXEffCommAmt, "
+ "k.RefCashAmt as RefCashAmt, k.RefCreditAmt as RefCreditAmt, k.RefEffCommAmt as RefEffCommAmt, "
+ "k.ACMCashAmt as ACMCashAmt, k.ACMCreditAmt as ACMCreditAmt, k.ACMEffCommAmt as ACMEffCommAmt, "
+ "k.ADMCashAmt as ADMCashAmt, k.ADMCreditAmt as ADMCreditAmt, k.ADMEffCommAmt as ADMEffCommAmt, "
+ "j.FBTD as Farebasis,k.TaxCashAmt as TaxCashAmt, k.EXTaxCashAmt as EXTaxCashAmt, "
+ "k.TaxCreditAmt, k.EXTaxCreditAmt, k.RefTaxCashAmt, k.RefTaxCreditAmt, "
+ "k.ACMTaxCashAmt, k.ACMTaxCreditAmt, k.ADMTaxCashAmt, k.ADMTaxCreditAmt, "
+ "k.YQTax, k.EXYQtax, k.YRTax, k.EXYRtax, k.CUTP as Currency, c.NefCode as NFCode "
+ "FROM tp_boh03 a "
+ "INNER JOIN tp_bkt06 b ON a.offheadid= b.offheadid "
+ "INNER JOIN tp_tickets c ON b.transheadid= c.transheadid "
+ "LEFT JOIN tp_bks39 f ON c.TicketHeadId = f.TicketHeadId "
+ "INNER JOIN tp_bki63 j ON c.TicketHeadId = j.TicketHeadId "
+ "INNER JOIN tp_mileage_fare k ON j.ItinerarySegmentID = k.ItinerarySegmentID "
+ "INNER JOIN tp_iata ti ON a.AGTN = ti.iata_no "
+ "LEFT JOIN tp_carriers l ON j.CARR = l.CarrierCode "
+ "LEFT JOIN tp_carriers m ON b.TACN = m.TicketCode)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.execute();
System.out.println("TIME TAKE : " + (System.currentTimeMillis() - start));
} catch (Exception ex) {
ex.printStackTrace();
}
The data expected should insert rows amounting to 2 million, but it's not inserting all data, every run of the script the rows being inserted changes. I tried to run the script directly in mysql and it did inserted same amount of rows after few re-runs.

Related

Why am I getting a "No value specified for parameter 2" error in Spring boot jdbc template

My sql query looks like:
select T.Col1
from (
select Past_Diagnoses_1 as Col1
from patienthistory
union
select Past_Diagnoses_2 as Col1
from patienthistory
union
select Past_Diagnoses_3 as Col1
from patienthistory
union
select Past_Diagnoses_4 as Col1
from patienthistory
) as T;
I keep getting an error on Spring which says No value specified for parameter 2
I am trying to union 4 columns together into 1 super long column.
This is a simplified code. I have 32 columns but for question I have shown 4
Java code :
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select T.Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_3 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_4 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_5 as Col1\n" +
" from patienthistory\n"
" where Patient_Number = ?\n" +
" ) as T";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("T.Col1")
));
}

How to use a user-defined variable in MySQL stored procedure?

I am trying to use a CASE statement in MySQL based on a variable. I tried the code below; however, I have found that "The SELECT statement cannot refer to system variables or user-defined variables". What I am trying to achieve is that I have an input date. I get its day of week so I know which day it is on (1 is Sunday). I then only want rows returned in the query that have the day set to "Y" (e.g., if day of week is 1 then stt_sunday is "Y"). The table has:
stt_id
stt_start_date
stt_end_date
stt_start_time
stt_end_time
stt_sunday
stt_monday
stt_tuesday
stt_wednesday
stt_thursday
stt_friday
stt_saturday
My code is:
String selectQry1 = (" SET #clickedDateDOW = clickedDateDOW; " +
" SELECT st_type, csm_session_timetable.stt_id, stt_start_date, stt_end_date, stt_start_time, stt_end_time " +
" FROM csm_session_type, csm_session_timetable " +
" WHERE csm_session_type.cli_id = ? " +
" AND csm_session_type.prm_id = ? " +
" AND csm_session_type.st_id = csm_session_timetable.st_id " +
" AND (csm_session_timetable.stt_end_date IS NULL || csm_session_timetable.stt_end_date > ?) " +
" AND csm_session_timetable.gym_id = ? " +
" AND CASE " +
" WHEN #clickedDateDOW = 1 AND stt_sunday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 2 AND stt_monday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 3 AND stt_tuesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 4 AND stt_wednesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 5 AND stt_thursday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 6 AND stt_friday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 7 AND stt_saturday LIKE 'Y' THEN 1 " +
" ORDER BY stt_start_date DESC, stt_start_time, stt_end_date DESC;");

unable to evaluate the expression method threw 'java.lang.illegalargumentexception' exception while excuting the flexible search query

hai i am getting products details from db while exceuting the flexiblesearchservice.search(query) it is throwing this error.my flexible search query is working fine with hac. but throwing error from flexible search service. my query and service is shown below.
"({{select {p:"+ProductModel.PK+"} from { "+ OrderModel._TYPECODE +" as o " +
" join "+ OrderStatus._TYPECODE+" as os on {os:pk} = {o:"+OrderModel.STATUS+"}"+
" join "+ OrderEntryModel._TYPECODE +" as oe on {oe:"+ OrderEntryModel.ORDER +"} = {o:"+OrderModel.PK+"}"+
" join "+ ProductModel._TYPECODE +" as p on {oe:"+OrderEntryModel.PRODUCT+"} = {p:"+ProductModel.PK+"}"+
" join "+ CatalogVersionModel._TYPECODE+" as cv on { p:"+ProductModel.CATALOGVERSION +"} = {cv:"+CatalogVersionModel.PK+"}"+
" join "+ CatalogModel._TYPECODE +" as c on {p:"+ProductModel.CATALOG+"} = {c:"+CatalogModel.PK+"}} "+
"where {os:code}='"+OrderStatus.COMPLETED +"' and {"+OrderModel.DATE+"}>'2017-08-16 00:00:00.000' " +
"and {"+OrderModel.DATE+"}<'2020-09-30 00:00:00.000' " +
"and {cv:"+CatalogVersionModel.VERSION+"}= 'Online' " +
"and {c:"+CatalogModel.ID+"} like 'apparelProductCatalog' " +
"group by {p:"+ProductModel.PK+"} order by sum({oe:"+OrderEntryModel.QUANTITY+"}) desc" +
" limit 10 }})";
final FlexibleSearchQuery fQuery = new FlexibleSearchQuery(QUERY);
fQuery.setResultClassList(Arrays.asList(ProductModel.class));
final SearchResult<ProductModel> searchResult =flexibleSearchService.search(fQuery);
final List<ProductModel> productModelList= searchResult.getResult();```
**error:** unable to evaluate the expression method threw 'java.lang.illegalargumentexception' exception
please help me out.
"where {os:code}='"+OrderStatus.COMPLETED +"'
This above code may be the issue, OrderStatus.COMPLETED returns an object, not String.
You can pass it as a query parameter.
"where {os:code} = '" + OrderStatus.COMPLETED.getCode() +"'

MySQL - using a prepared statement to copy a row in the same table and include static values throws an error

I am trying to use a prepared statement to copy one row to a new row (INSERT)in the same table and include some static values. However, I get the following error:
SQLException in copyProgram: java.sql.SQLException: Operand should contain 1 column(s)
My input is:
prId: 4 accountID: 50 newFromDate: 2020-04-27 newToDate: 2020-04-28
My code is:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " (SELECT (?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " FROM at_program "
+ " WHERE pr_id = ?);");
ps = c.prepareStatement(insertQry, Statement.RETURN_GENERATED_KEYS);
// Create a statement and execute the query on it
ps.setString(1, accountID);
ps.setString(2, fromDate);
ps.setString(3, toDate);
ps.setString(4, prID);
ps.executeUpdate();
You have parens around the field list in your SELECT! Remove them.
The issue is that SELECT expects a list of (individual) columns, but you are passing a multi-column value.
SELECT x, y, z sees x, y and z as three values each consisting of one column, yet you are passing SELECT (x, y, z) which is interpreted as one value with three columns, hence the error.
By the way, the parens around the whole SELECT are superfluous as well, this time because INSERT INTO table (columns) SELECT ... is a recognized syntax construct in itself.
Corrected code:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " SELECT ?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes "
+ " FROM at_program "
+ " WHERE pr_id = ?;");

how to convert the query to hibernate query language?

how to write this query in hibernate query language
select * from preferred_space p, building b, floor f, space_type st, space s
where p.user_id=11
and p.space_id=s.id
and s.building_id=b.id
and s.floor_id=f.id
and s.space_type_id=st.id
order by p.id;
If I do it this way it is showing me an error?
String sql = "from PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
"where p.userId = ? " +
"and p.spaceId = s.id" +
"and s.buildingId = b.id" +
"and s.floorId = f.id" +
"and s.spaceTypeId = st.id" +
"order by p.id";
error:
ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
16:18:17.569 [http-bio-8080-exec-24] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
antlr.NoViableAltException: unexpected token: s
shows the same error for every row that starts with "s." and also for "by".
You need to add spaces:
String sql = "from PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
"where p.userId = ? " +
"and p.spaceId = s.id " +
"and s.buildingId = b.id " +
"and s.floorId = f.id " +
"and s.spaceTypeId = st.id " +
"order by p.id";