I am using Java and MySQL. This code works and returns the expected results:-
queryStr = "SELECT PROGRAM, EQUIPMENT, t.PART_NUMBER, SN, GFE_CAP, t.STATUS, ii.Part_Number, ii.Serial_Number, [Inventory_Type – Level 1], ii.Status, ii.Destination, ii.Location" +
" FROM tblGFE_CAP as t, NGC_BJ_DEV.dbo.ISSUE_ITEMS as ii" +
" WHERE (t.PART_NUMBER = ii.Part_Number)" +
" AND (SN = ii.SERIAL_NUMBER)" +
" AND (GFE_CAP != [Inventory_Type – Level 1])" +
" AND NOT (([Inventory_Type – Level 1] = 'GFP') and (GFE_CAP = 'GFE'))" +
" AND NOT ((ii.Status = 'Retired') AND (t.Status = 'INACTIVE-REPLACED/RETIRED/DISPOSITION'))" +
" AND NOT ((ii.Status = 'Consumed') and (t.Status = 'INACTIVE-REPLACED/RETIRED/DISPOSITION'))" +
" GROUP BY PROGRAM, EQUIPMENT, t.PART_NUMBER, SN, GFE_CAP, t.Status, ii.Part_Number, ii.Serial_Number, [Inventory_Type – Level 1], ii.Destination, ii.Location, ii.Status" +
" ORDER BY GFE_CAP, t.PART_NUMBER, SN";
stmt = con.prepareStatement(queryStr);
rs = stmt.executeQuery();
However, if I read the exact same text from a file (as below) and copy the results into queryStr
, the code chokes on the Inventory_Type - Level 1
column name. I confirmed that the resultant strings are identical when passed to preparedStatement. I understand that special characters are not a good idea for the column name, but that is beyond my control.
I cannot change the column name. My real question is why the hard coded query works but the file read does not. Alternatively if there is a syntax that can be used to capture the column name within the java built query string.
String bStr = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(in.getAbsolutePath()));
while (true)
{
bStr = br.readLine();
if (bStr == null)
break;
sb.append(bStr);
}
br.close();
queryStr = sb.toString();
stmt = con.prepareStatement(queryStr);
rs = stmt.executeQuery();