JSP for shopping box

nonamedemo發表於2007-09-03

============

T-SQL

============

if exists ( select * from sysdatabases where name = 'shop')
drop database shop
go
create database shop

use shop
go

if exists(select * from sysobjects where name ='product')
drop table product
go
create table product
(
prodId varchar(15) not null primary key,
prodName varchar(20) not null ,
prodPrice money not null,
prodNumber int
)
go
select * from product

insert into product values('XSQ001','sumsang顯示器',2000,null)
insert into product values('XSQ002','sharp顯示器',4000,null)
insert into product values('XSQ003','BenQ顯示器',1000,null)
insert into product values('XSQ004','Acer顯示器',3000,null)
insert into product values('XSQ005','AOC顯示器',2200,null)
insert into product values('XSQ006','ViewSonic顯示器',1500,null)

=========================

shopping car

==========================

package shoppingcar;

import java.sql.*;

import java.util.ArrayList;
import java.util.Hashtable;

public class shoppingcar {
private Hashtable productlist;
public shoppingcar() {
productlist = this.getProductlist();
}

public Hashtable getProductlist() {
return productlist;
}

//新增一個商品
public void addproduct(String prodId, product prod) {
productlist.put(prodId, prod);
}

//獲取一個商品
public product getproduct(String prodId) {
return (product) productlist.get(prodId);
// product prod = null;
// Connection con = null;
// con = DBManager.getConnection();
// String sql = "select * from product where prodId='" + prodId + "'";
// try {
// Statement stmt = con.createStatement();
// ResultSet rs = stmt.executeQuery(sql);
// prod.setProdId(rs.getString("prodId"));
// prod.setProdName(rs.getString("prodName"));
// prod.setProdPrice(rs.getFloat("prodPrice"));
// prod.setProdNumber(rs.getInt("prodNumber"));
// } catch (SQLException ex) {
// ex.printStackTrace();
// } finally {
// try {
// con.close();
// } catch (SQLException ex1) {
// ex1.printStackTrace();
// }
// }
//
// return prod;
}

//刪除一個商品
public void removeprod(String prodId) {
productlist.remove(prodId);
}


}

=================

product

==================

package shoppingcar;

public class product {
private String prodId;
private String prodName;
private int prodNumber;
private float prodPrice;
public product() {
}

public void setProdId(String prodId) {
this.prodId = prodId;
}

public void setProdName(String prodName) {
this.prodName = prodName;
}

public void setProdNumber(int prodNumber) {
this.prodNumber = prodNumber;
}

public void setProdPrice(float prodPrice) {
this.prodPrice = prodPrice;
}

public String getProdId() {
return prodId;
}

public String getProdName() {
return prodName;
}

public int getProdNumber() {
return prodNumber;
}

public float getProdPrice() {
return prodPrice;
}
}

==================

DBManager

===================

package shoppingcar;

import java.sql.*;

public class DBManager {
public DBManager() {
}

public static Connection getConnection() {
Connection con = null;
try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
try {
DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;dataBaseName=shop",
"sa", "");
} catch (SQLException ex1) {
ex1.printStackTrace();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return con;
}
}

============

continuing.........................................

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-967786/,如需轉載,請註明出處,否則將追究法律責任。

相關文章