我正在用Jsp ajax创建一个简单的库存系统。所有的销售计算都可以很好地完成。
如果我通过Ajax调用成功将数据发送到Jsp Page,则成功通过控制台检查了数据。但是在我检索数据之后
如何传递到数据相关变量并将数据添加到数据库中。
String[] json =request.getParameterValues("items");
以我在下面编写的jsp页面完整代码检索数据
所有销售应添加此表数据。我将发送到sales_add.jsp页面。我这样检查console.log(JSON.stringify(items));我在下面写的完整代码。
function add_product_to_array(item,price,qty,tot)
{
var item = [item,tot];
items.push(item);
console.log(JSON.stringify(items));
}
我通过Console.log检查的表成功显示如下
**[["Chocolate",32,"1",32]]
(index):237 [["Chocolate",32],["Mango",10,10]]**
我以这种方式将** var data =“&items =” + JSON.stringify((items))发送到sales.add.jsp页面
function addProject()
{
var data = "&items=" + JSON.stringify((items));
$.ajax({
type: "POST",url: "sales_add.jsp",dataType: 'JSON',data: data,success: function (data) {
console.log(_data);
alert("Success");
},error: function (xhr,status,error) {
alert(xhr);
console.log(xhr.responseText);
}
});
}
Sales.jsp页面i这样接收数据
String[] json =request.getParameterValues("items");
String item = json[x][0]); if i wrote like this way getting Error
String price = json[x][1]);
String qty = json[x][2]);
String total = json[x][3]);
我在下面写下了完整的代码。
<%
String[] json =request.getParameterValues("items");
Connection con1;
PreparedStatement insert;
int lastinsertid=0;
String[] json =request.getParameterValues("items");
Connection con1;
PreparedStatement insert;
Class.forName("com.mysql.jdbc.Driver");
con1=DriverManager.getconnection("jdbc:mysql://localhost/icepos","root","");
for(int x = 0; x < json.length; x++)
{
String query = "INSERT INTO sale_product(item,total) VALUES (?,?,?)";
String item = json[x][0]);
String price = json[x][1]);
String qty = json[x][2]);
String total = json[x][3]);
}
}
%>
----------