使用java的jdbc连接MySQL数据库,怎么把一个表中的重复数据合并在一起。具体在补充里说。
我建了个存购买物品信息的表,如下:createtablebasket(p_namevarchar(100),#商品名称p_pricedouble,#商品的单价buy_nu...
我建了个存购买物品信息的表,如下:
create table basket (
p_name varchar(100), #商品名称
p_price double, #商品的单价
buy_num int, #商品的个数
total_amount double #总金额
);
连续插入了如下数据:
('竹笛', 80, 1 , 80 );
('竹笛', 80, 2 , 160 );
怎么操作才能使数据库中的信息变成:
('竹笛', 80, 3 , 240 );
jsp中对应代码如下:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*, java.util.*, cn.jlu.project.mybase.*" %>
<html>
<head>
<title>购物车信息页面</title>
</head>
<body>
<%@ include file="Head.jsp"%>
<%
int p_id = Integer.parseInt(String.valueOf(session.getAttribute("p_id")));
int buy_num = Integer.parseInt(request.getParameter("buy_num"));
String p_name = null;
double p_price = 0;
double total_amount = 0;
Connection conn = new BaseConnect().getConn();
Statement stmt = conn.createStatement();
PreparedStatement pstmt = null;
ResultSet rs = stmt.executeQuery("select * from product where p_id="+p_id);
while(rs.next()) {
p_name = rs.getString("p_name");
p_price = rs.getDouble("p_price");
total_amount = p_price*buy_num;
pstmt = conn.prepareStatement("insert into basket values(?,?,?,?,?)");
pstmt.setString(1, username);
pstmt.setString(2, p_name);
pstmt.setDouble(3, p_price);
pstmt.setInt(4, buy_num);
pstmt.setDouble(5, total_amount);
pstmt.executeUpdate();
}
response.sendRedirect("BasketInfo.jsp");
%>
</body>
</html>
每次选择加入购物车会执行这个页面的代码,然后转到购物车信息显示页面显示相关信息,很遗憾重复的我无法合并。 展开
create table basket (
p_name varchar(100), #商品名称
p_price double, #商品的单价
buy_num int, #商品的个数
total_amount double #总金额
);
连续插入了如下数据:
('竹笛', 80, 1 , 80 );
('竹笛', 80, 2 , 160 );
怎么操作才能使数据库中的信息变成:
('竹笛', 80, 3 , 240 );
jsp中对应代码如下:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*, java.util.*, cn.jlu.project.mybase.*" %>
<html>
<head>
<title>购物车信息页面</title>
</head>
<body>
<%@ include file="Head.jsp"%>
<%
int p_id = Integer.parseInt(String.valueOf(session.getAttribute("p_id")));
int buy_num = Integer.parseInt(request.getParameter("buy_num"));
String p_name = null;
double p_price = 0;
double total_amount = 0;
Connection conn = new BaseConnect().getConn();
Statement stmt = conn.createStatement();
PreparedStatement pstmt = null;
ResultSet rs = stmt.executeQuery("select * from product where p_id="+p_id);
while(rs.next()) {
p_name = rs.getString("p_name");
p_price = rs.getDouble("p_price");
total_amount = p_price*buy_num;
pstmt = conn.prepareStatement("insert into basket values(?,?,?,?,?)");
pstmt.setString(1, username);
pstmt.setString(2, p_name);
pstmt.setDouble(3, p_price);
pstmt.setInt(4, buy_num);
pstmt.setDouble(5, total_amount);
pstmt.executeUpdate();
}
response.sendRedirect("BasketInfo.jsp");
%>
</body>
</html>
每次选择加入购物车会执行这个页面的代码,然后转到购物车信息显示页面显示相关信息,很遗憾重复的我无法合并。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询