dao类
package com.zking.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.entity.Goods;
import com.zking.util.DBHelper;
public class GoodsDao {
/**
* 查询所有商品
* @return 返回商品集合
*/
public ArrayList<Goods> getAll(){
ArrayList<Goods>glist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return glist;
}
/**
* 根据编号查询
* @param bid
* @return
*/
public Goods getById(int bid){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Goods g = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods where bid="+bid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
g = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return g;
}
}
实体类
package com.zking.entity;
public class Goods {
private int bid;
private String bname;
private double bprice;
private String binfo;
private String bface;
public Goods(String bname, double bprice, String binfo, String bface) {
super();
this.bname = bname;
this.bprice = bprice;
this.binfo = binfo;
this.bface = bface;
}
public Goods(int bid, String bname, double bprice, String binfo, String bface) {
super();
this.bid = bid;
this.bname = bname;
this.bprice = bprice;
this.binfo = binfo;
this.bface = bface;
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public double getBprice() {
return bprice;
}
public void setBprice(double bprice) {
this.bprice = bprice;
}
public String getBinfo() {
return binfo;
}
public void setBinfo(String binfo) {
this.binfo = binfo;
}
public String getBface() {
return bface;
}
public void setBface(String bface) {
this.bface = bface;
}
@Override
public String toString() {
return "Goods [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + ", binfo=" + binfo + ", bface=" + bface
+ "]";
}
}
实体类
package com.zking.entity;
public class OrderItem {
private Goods goods;
private int gnumber;
private double sumPrice;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getGnumber() {
return gnumber;
}
public void setGnumber(int gnumber) {
this.gnumber = gnumber;
}
public double getSumPrice() {
return sumPrice;
}
public void setSumPrice() {
this.sumPrice =goods.getBprice()*gnumber ;
}
public OrderItem(Goods goods, int gnumber, double sumPrice) {
super();
this.goods = goods;
this.gnumber = gnumber;
this.sumPrice = sumPrice;
}
public OrderItem(int gnumber, double sumPrice) {
super();
this.gnumber = gnumber;
this.sumPrice = sumPrice;
}
public OrderItem() {
super();
}
@Override
public String toString() {
return "OrderItem [goods=" + goods + ", gnumber=" + gnumber + ", sumPrice=" + sumPrice + ", getGoods()="
+ getGoods() + ", getGnumber()=" + getGnumber() + ", getSumPrice()=" + getSumPrice() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
}
连接数据库的
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBHelper {
private static String user = "scott";
private static String upwd = "tiger";
private static String cname = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
//注册驱动类
static {
try {
Class.forName(cname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//连接数据库
/**
* 连接数据库
* @return
*/
public static Connection getCon() {
Connection con = null;
try {
con = DriverManager.getConnection(url, user, upwd);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭连接
* @param con
* @param ps
* @param rs
*/
public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下一个获取下一个编号的方法
* @return 下一个编号
*/
public static int getNextId(String tableName,String col) {
int id = 1;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select max("+col+") from "+tableName);
rs = ps.executeQuery();
if(rs.next()) {
id = rs.getInt(1)+1;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return id;
}
}
主页
package com.zking.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.entity.Goods;
import com.zking.util.DBHelper;
public class GoodsDao {
/**
* 查询所有商品
* @return 返回商品集合
*/
public ArrayList<Goods> getAll(){
ArrayList<Goods>glist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return glist;
}
/**
* 根据编号查询
* @param bid
* @return
*/
public Goods getById(int bid){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Goods g = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods where bid="+bid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
g = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return g;
}
}
订单页面
package com.zking.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.entity.Goods;
import com.zking.util.DBHelper;
public class GoodsDao {
/**
* 查询所有商品
* @return 返回商品集合
*/
public ArrayList<Goods> getAll(){
ArrayList<Goods>glist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return glist;
}
/**
* 根据编号查询
* @param bid
* @return
*/
public Goods getById(int bid){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Goods g = null;
try {
con = DBHelper.getCon();
String sql = "select * from Goods where bid="+bid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
g = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return g;
}
}
还有一半,懒的弄了告辞