【发布时间】:2015-05-06 13:30:42
【问题描述】:
@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 部分时,它不会插入任何内容。请帮忙:)
【问题讨论】:
-
请添加您遇到的错误的堆栈跟踪!!!
标签: java spring jsp spring-mvc