From a servlet in Java I am calling a function to execute a query, Query is given below, I have tried these formats:
Error is:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'TFM00100' in 'field list'
Queries I have tried:
SELECT * FROM tbl_contract AS tc WHERE tc.`contact_id` = 'TFM00100'
SELECT * FROM tbl_contract AS tc WHERE tc.`contact_id` = "TFM00100"
SELECT * FROM tbl_contract WHERE `contact_id` = 'TFM00100'
SELECT * FROM tbl_contract WHERE `contact_id` = "TFM00100"
SELECT * FROM tbl_contract WHERE contact_id = "TFM00100"
SELECT * FROM tbl_contract AS tc WHERE (tc.`contact_id` = 'TFM-00100');
I also have tried with prepared statement also.
But same error return when call specific function to execute a query from a servlet on live server, On local server it is working fine. Also when execute specific query on live MySQL db it works fine.
Servlet Relevent code only:
public class AttandenceInOutReport extends HttpServlet {
private static final long serialVersionUID = 1L;
public AttandenceInOutReport() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
proccessRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
proccessRequest(request, response);
}
protected void proccessRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* Setting default response
*/
// Rest of code
try {
/**
* converting request into string
*/
String res = WebServiceUtils.parseRequestToString(request);
/**
* Request write in logger
*/
logger.info("Request from client" + res);
AttandenceReportReq reportReq = new Gson().fromJson(res, AttandenceReportReq.class);
if (reportReq != null && !reportReq.equals("") && (!WebServiceUtils.isEmpty(reportReq.getStartDate()))
&& (!WebServiceUtils.isEmpty(reportReq.getEndDate()))
&& (!WebServiceUtils.isEmpty(reportReq.getContractId()))) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
LocalDate startDate = formatter.parseLocalDate(reportReq.getStartDate());
LocalDate endDate = formatter.parseLocalDate(reportReq.getEndDate());
String contractId = reportReq.getContractId();
//List<TblContract> contactList = new ArrayList<TblContract>();
// Function being called here
String contractName = CommonHandler.getContract(contractId);
// Rest of the code
// ...............
}
} catch (Exception e) {
success = false;
logger.error("Exception in processing request " + e);
}
// Rest of the code
// ...............
}
}
Function being called:
public static String getContractName(String conId) {
Connection conn = (Connection) DBConnection.getInstance().getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String contractName = "";
String query = "SELECT contract_name FROM tbl_contract WHERE contact_id = '" + conId + "' ";
try {
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
if(rs.next()) {
contractName = rs.getString("contract_name");
}
} catch (Exception e) {
e.printStackTrace();
return contractName;
} finally {
if (conn != null) {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
return contractName;
}
}
}
return contractName;
}
You can try below code:
String query = "SELECT contract_name FROM tbl_contract WHERE contact_id = ? ";
try {
pstmt = conn.prepareStatement(query);
ps.setString(1, conId);
rs = pstmt.executeQuery();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.