|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
首先第一点:jsp,servlet,javabean这些最基本的,嘿嘿,就算你是高手的话,在大行的企业级应用的话还是需要框架的,一个好的框架确实能构解决许多问题。js|程序|购物车|数据|数据库上面是我用JSP和数据库做的购物车的源程序:
注重:
1、从头计费部分还没有做好,人人本人下手吧!
2、下一版本将用session做。
//shop_cart.jsp
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pagesession="true"%>
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="bka"scope="page"class="shop.bka"/>
<%
Stringproduct_type;
Stringaction;
intproduct_id;
intcurpage;
//商品范例
if(request.getParameter("product_type")==null){
product_type="all";
}else{
product_type=request.getParameter("product_type");
}
//页数和商品范例参数,能够在“持续购物”时前往到前次购物的页面
if(request.getParameter("curpage")==null){
curpage=1;
}else{
curpage=java.lang.Integer.parseInt(request.getParameter("curpage"));
}
//举措
if(request.getParameter("action")==null){
action="view";
}else{
action=request.getParameter("action");
}
//商品编号
if(request.getParameter("product_id")==null){
product_id=0;
}else{
product_id=java.lang.Integer.parseInt(request.getParameter("product_id"));
}
intbbb;
bbb=1;
Integernum=newInteger(bbb);
//商铺编号
session.putValue("shop_id",num);
//主顾username
session.putValue("guest_name","asp2001");
Stringguest_name=(String)session.getValue("guest_name");
Integershop_id=(Integer)session.getValue("shop_id");
java.lang.Stringsql;
java.sql.ResultSetrs;
if(action.compareTo("add")==0){
sql="selectcart_quantityfromshop_cartwherecart_shop_id="+shop_id+"andcart_guest_id="+guest_name+"andcart_product_id="+product_id;
rs=bka.executeQuery(sql);
if(rs.next()){
intcart_quantity;
cart_quantity=java.lang.Integer.parseInt(rs.getString("cart_quantity"))+1;
sql="updateshop_cartsetcart_quantity="+cart_quantity+"wherecart_shop_id="+shop_id+"andcart_guest_id="+guest_name+"andcart_product_id="+product_id;
rs=bka.executeQuery(sql);}
else
{
sql="insertintoshop_cart(cart_shop_id,cart_guest_id,cart_product_id,cart_quantity)values("+shop_id+","+guest_name+","+product_id+",1)";
rs=bka.executeQuery(sql);
}
}
if(action.compareTo("clear")==0){
sql="deletefromshop_cartwherecart_shop_id="+shop_id+"andcart_guest_id="+guest_name+"";
rs=bka.executeQuery(sql);
}
if(action.compareTo("delete")==0){
sql="deletefromshop_cartwherecart_shop_id="+shop_id+"andcart_guest_id="+guest_name+"andcart_product_id="+product_id;
rs=bka.executeQuery(sql);
}
%>
<divalign="center"><center>
<tableborder="0"cellpadding="0"cellspacing="0"width="610"
height="2">
<tr>
<td><formmethod="POST"
action="shop_cart.jsp?action=update&product_type=<%=product_type%>&curp
age=<%=curpage%>">
<tableborder="0"cellpadding="0"cellspacing="0"width="610"
height="2">
<tr>
<tdwidth="122"height="7"><divalign="center"><center><table
border="1"cellpadding="2"cellspacing="0"width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><div
align="center"><center><p>购物车</td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"
align="center"><divalign="center"><center><table
border="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p><a
href="shop_list.jsp?shop_id=<%=shop_id%>&product_type=<%=product_type%>
&curpage=<%=curpage%>">持续购物</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"
align="center"><divalign="center"><center><table
border="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p><a
href="javascript:document.forms[0].submit()">从头计费</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"
align="center"><divalign="center"><center><table
border="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p><ahref="shop_cart.jsp?action=clear&product_type=<%=product_type%>&curpage=<%=curpage%>">清空购物车</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center"><divalign="center"><center><table
border="1"cellpadding="2"cellspacing="0"width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p><ahref="shop_order.asp">确认购置</a></td>
</tr>
</table>
</center></div></td>
</tr>
<tralign="center">
<tdwidth="610"height="1"colspan="5"><divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"bgcolor="#FDFEE2"bordercolorlight="#FFB468"bordercolordark="#FFFFFF"height="40">
<tr>
<tdwidth="20%"height="8"align="left">商品称号</td>
<tdwidth="10%"height="8"align="left">市场价</td>
<tdwidth="10%"height="8"align="left">优惠价</td>
<tdwidth="10%"height="8"align="left">数目</td>
<tdwidth="14%"height="8"align="left">小计</td>
<tdwidth="12%"height="8"align="left">定金比例</td>
<tdwidth="17%"height="8"align="left">定金小计</td>
<tdwidth="17%"height="8"align="left">删除</td>
</tr>
<%
sql="selectshop_product.product_id,shop_product.product_name,shop_product.product_price,shop_product.product_discount,shop_cart.cart_quantity,shop_product.product_firstfromshop_cart,shop_productwhereshop_cart.cart_shop_id="+shop_id+"andshop_cart.cart_guest_id="+guest_name+"andshop_cart.cart_product_id=shop_product.product_id";
rs=bka.executeQuery(sql);
inttotal;
inttotal_first;
total=0;
total_first=0;
Stringproduct_name;
intproduct_price;
intproduct_discount;
intproduct_first;
intcart_quantity;
if(rs.next()){
while(rs.next()){
product_id=java.lang.Integer.parseInt(rs.getString(1));
product_name=rs.getString(2);
product_price=java.lang.Integer.parseInt(rs.getString(3));
product_discount=java.lang.Integer.parseInt(rs.getString(4));
cart_quantity=java.lang.Integer.parseInt(rs.getString(5));
product_first=java.lang.Integer.parseInt(rs.getString(6));
%>
<tr>
<tdwidth="10%"height="1"align="left"><%=product_name%></td>
<tdwidth="10%"height="1"align="left"><%=product_price%></td>
<tdwidth="10%"height="1"align="left"><%=product_discount%></td>
<tdwidth="10%"height="1"align="left"><inputtype="text"name="<%="t"+product_id%>"size="3"value="<%=cart_quantity%>"></td>
<tdwidth="14%"height="1"align="left"><%=product_discount*cart_quantity%></td>
<tdwidth="12%"height="1"align="left"><%=product_first+"%"%></td>
<tdwidth="17%"height="1"align="left"><%=product_first*product_discount*cart_quantity/100.0%></td>
<tdwidth="17%"height="1"><divalign="center"><center><p><ahref="shop_cart.jsp?action=delete&product_id=<%=product_id%>">delete</a></td>
</tr>
<%
total=total+product_discount*cart_quantity;
total_first=total_first+product_discount*cart_quantity*product_first/100;
}
%>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>定金总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total_first%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>节余</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total-total_first%></td>
</tr>
</table>
</center></div>
<%
}else{
%>
<palign="center">购物车为空!</p>
<%
}
%>
数据库操纵部分
程序用到两个表:
1shop_cart表
cart_idint购物车编号主动编号
cart_shop_idnvarchar商铺编号
cart_product_idnvarchar商品编号
cart_quantityint商品数目
一时寄存购物车数据
2shop_product表
product_idint商品编号主动编号
shop_idnvarchar商铺编号
product_namenvarchar商品称号
product_bbnvarchar商品先容
product_priceint市场价
product_discountint优惠价
product_imgimg图片
product_statusnvarchar形态
product_firstint定金比例
product_typenvanchar商品范例
寄存商品材料
利用bka.java制成的javabean:bka.class能够供应对数据库的操纵。
别的,需在把持面板的体系DSN中注册bka.dsn,
从而可以使JSP经由过程JDBC-ODBC来挪用sql数据库。
在页面中挪用javabean,基础上可接纳以下体例:
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="RegisterBean"scope="page"class="shop.bka"/>
<%
Stringsql="select*fromxxx";
ResultSetrs=RegisterBean.executeQuery(sql);
if(rs.next()){
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicatenamefound!");
}
%>
注重应在利用后将rs封闭。
以下是bka.java的源程序。注重在利用前需用javac加以编译成为class文件即javabean.
--shop/bka.java--
packageshop;
importjava.sql.*;
publicclassbka{
StringsDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
StringsConnStr="jdbc:odbc:bka";
Connectionconn=null;
ResultSetrs=null;
publicbka(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundExceptione){
System.err.println("bka():"+e.getMessage());
}
}
publicResultSetexecuteQuery(Stringsql){
rs=null;
try{
conn=DriverManager.getConnection(sConnStr,"xxx","yyy");
Statementstmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLExceptionex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
returnrs;
}
}
你精通任何一门语言就最强大。现在来看,java的市场比C#大,C#容易入手,比较简单,java比较难 |
|