@RequestMapping(value = "/invoice", method = RequestMethod.POST)
public String submitInvoice(HttpServletRequest request,
@RequestParam("clients") int clientId,
@RequestParam("invoice_date") String invoice_date,
@RequestParam("invoice_due_date") String invoice_due_date,
@RequestParam("status") String status,
@RequestParam("payment_method") String payment_method,
@RequestParam("currency") String currency,
@RequestParam("description") String description,
@RequestParam("quantity") String quantity,
@RequestParam("price") String price,
@RequestParam("total") String lineTotal) {
if(!hasRole(request, "ROLE_USER")){
return "403";
}
long invoiceId = 0;
DBManager.createInvoice(clientId, invoice_date, invoice_due_date, status, payment_method, currency);
DBManager.invoiceDescription(invoiceId, description, quantity, price, lineTotal);
return "redirect:/invoices/page/1";
}
下面的数据库管理器
public static long createInvoice(
int clientId, String invoice_date, String invoice_due_date,
String status, String payment_method, String currency){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
long invoiceId = 0;
//String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String sql = "INSERT INTO invoice"
+ "(invoiceId, clientId, invoice_date, invoice_due_date, status, payment_method, currency) VALUES"
+ "(NULL,?,?,?,?,?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setInt(1, clientId);
preparedStatement.setString(2, invoice_date);
preparedStatement.setString(3, invoice_due_date);
preparedStatement.setString(4, status);
preparedStatement.setString(5, payment_method);
preparedStatement.setString(6, currency);
preparedStatement.executeUpdate();
System.out.println("You have successfully created an invoice record");
PreparedStatement getLastInsertId = dbConnection.prepareStatement("SELECT LAST_INSERT_ID()");
ResultSet rs = getLastInsertId.executeQuery();
if(rs.next())
{
invoiceId = rs.getLong("last_insert_id()");
System.out.println("Last invoiceId inserted: " + invoiceId);
}
} catch (SQLException e){
System.out.println(e.getMessage());
}
return invoiceId;
}
public static void invoiceDescription(
long invoiceId, String description, String quantity,
String price, String lineTotal){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
//String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String sql = "INSERT INTO invoice_description"
+ "(descId, invoiceId, description, quantity, price, total) VALUES"
+ "(NULL,?,?,?,?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setLong(1, invoiceId);
preparedStatement.setString(2, description);
preparedStatement.setString(3, quantity);
preparedStatement.setString(4, price);
preparedStatement.setString(5, lineTotal);
preparedStatement.executeUpdate();
System.out.println("You have successfully added descriptions to invoice");
} catch (SQLException e){
System.out.println(e.getMessage());
}
}
我收到上述代码的以下消息:
'The request sent by the client was syntactically incorrect.'
。
我正在尝试插入多个表。如果我删除 DBManager.invoiceDescription
它将很好地将第一部分插入到发票表中,不会出现错误,但是当我添加 invoiceDescription
时部分,它不插入任何东西。请帮忙:)
最佳答案
错误“客户端发送的请求在语法上不正确。”
表示您的请求格式不正确。
我发现您的所有请求参数都是必需的,因此如果至少缺少其中一个参数,您将收到此错误。确保您发送所有请求参数,或者如果您有时想发送其中一些参数,您可以将它们设置为非强制:
@RequestParam(value = "clients", required = false)
在这种情况下,如果您将它们全部设为非强制,您可以选择发送哪些参数。
关于java - SPRING框架: The request sent by the client was syntactically incorrect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078295/