商城项目整理(三)JDBC增删改查
商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看
商品表建表语句:
create table TEST.GOODS_TABLE ( gid NUMBER not null, gname ), gdetails CLOB, gpicture ), gprice NUMBER, gleixing NUMBER, gpinpai ) ) tablespace USERS pctfree initrans maxtrans storage ( initial 64K next 1M minextents maxextents unlimited ); comment on column TEST.GOODS_TABLE.gid is '商品ID'; comment on column TEST.GOODS_TABLE.gname is '商品名称'; comment on column TEST.GOODS_TABLE.gdetails is '商品详情'; comment on column TEST.GOODS_TABLE.gpicture is '商品图片'; comment on column TEST.GOODS_TABLE.gprice is '商品价格'; comment on column TEST.GOODS_TABLE.gleixing is '商品类型'; comment on column TEST.GOODS_TABLE.gpinpai is '商品品牌'; alter table TEST.GOODS_TABLE add constraint PK_GOODSID primary key (GID) using index tablespace USERS pctfree initrans maxtrans storage ( initial 64K next 1M minextents maxextents unlimited );
订单表建表语句:
create table TEST.SHOPORDER ( scid ), scuser ), scgid ), scnum NUMBER, scmoney NUMBER, sctime ), sczt ) ) tablespace SYSTEM pctfree pctused initrans maxtrans storage ( initial 64K next 1M minextents maxextents unlimited ); comment on column TEST.SHOPORDER.scid is '订单号'; comment on column TEST.SHOPORDER.scuser is '用户名'; comment on column TEST.SHOPORDER.scgid is '商品ID'; comment on column TEST.SHOPORDER.scnum is '商品数量'; comment on column TEST.SHOPORDER.scmoney is '商品总价'; comment on column TEST.SHOPORDER.sctime is '交易时间'; comment on column TEST.SHOPORDER.sczt is '订单状态';
用户表建表语句:
create table TEST.USER_TABLE ( userid NUMBER not null, uname ), upassword ), unickname ), uemail ), utime ) ) tablespace USERS pctfree initrans maxtrans storage ( initial 64K next 1M minextents maxextents unlimited ); comment on column TEST.USER_TABLE.userid is '用户id'; comment on column TEST.USER_TABLE.uname is '用户名'; comment on column TEST.USER_TABLE.upassword is '用户密码'; comment on column TEST.USER_TABLE.unickname is '用户昵称'; comment on column TEST.USER_TABLE.uemail is '用户邮箱'; comment on column TEST.USER_TABLE.utime is '注册时间';
日志表建表语句:
create table TEST.SHOPLOG ( pname ), pugroup ), puptime ), pip ), plog ) ) tablespace SYSTEM pctfree pctused initrans maxtrans storage ( initial 64K next 1M minextents maxextents unlimited ); comment on column TEST.SHOPLOG.pname is '用户名'; comment on column TEST.SHOPLOG.pugroup is '用户组'; comment on column TEST.SHOPLOG.puptime is '登陆时间'; comment on column TEST.SHOPLOG.pip is 'IP'; comment on column TEST.SHOPLOG.plog is '操作';
数据库链接驱动类:
package com.hanqi.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库驱动连接类 * @author ZBK */ public class DBHelper { /** * 数据库用户名 */ public static final String USERNAME = "test"; /** * 数据库密码 */ public static final String PASSWORD = "test"; /** * 数据库驱动类 */ public static final String DRIVER = "oracle.jdbc.OracleDriver"; /** * 数据库地址URL */ public static final String URL = "jdbc:oracle:thin:@localhost:1521:xe"; /** * 获取数据库连接 * @return */ public static Connection getConnection() { Connection conn = null; try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 释放资源 * @param conn 数据库连接对象 * @param sm Statement对象 * @param rs ResultSet结果集对象 */ public static void destroy(Connection conn, Statement sm, ResultSet rs) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } if (sm != null) { try { sm.close(); } catch (SQLException e) { e.printStackTrace(); } sm = null; } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } } /** * 验证前台传入的参数是否为空 * @param args * @return */ public static boolean checkParam(String... args) { for (String s : args) { if (s == null || s.trim().length() < 1) { return false; } } return true; } }
数据库操作方法类:
package com.hanqi.dal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.hanqi.model.Goods; import com.hanqi.model.Log; import com.hanqi.model.Order; import com.hanqi.model.TradingStatusFile; import com.hanqi.model.User; import com.hanqi.util.DBHelper; public class MethodDal { private Connection conn; private PreparedStatement ps; private ResultSet rs; private static int scnum=1000; //初始化链接 public void init(String sql) { conn = DBHelper.getConnection(); try { ps = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } } //释放资源 public void close(){ DBHelper.destroy(conn, ps, rs); } //判断传入的参数有没有空的方法,只要有空的就返回false public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以 for(String s : args){ if("".equals(s)||s==null){ return false; } } return true; } /** * 返回所有商品 * @return */ public List<Goods> getAllGoods(){ String sql = "select * from GOODS_TABLE g"; init(sql); List<Goods> list = new ArrayList<Goods>(); try { rs = ps.executeQuery(); while (rs.next()) { Goods good=new Goods(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getInt(6),rs.getString(7)); list.add(good); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 增加一条商品信息 * @param g * @return */ public int insertGoods(Goods g) { String sql = "insert into GOODS_TABLE values(tablexulie.nextval,?,?,?,?,?,?)"; init(sql); int a = -1; try { ps.setString(1, g.getGname()); ps.setString(2, g.getGdetails()); ps.setString(3, g.getGpicture()); ps.setInt(4, g.getGprice()); ps.setInt(5, g.getGleixing()); ps.setString(6, g.getGpinpai()); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } /** * 删除商品信息 */ public int deleteGoods(int gid) { String sql = "delete from GOODS_TABLE g where g.gid=? "; init(sql); int a = -1; try { ps.setInt(1, gid); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } //修改商品信息 public int UpdateGood(Goods g) { String sql = "update GOODS_TABLE g set g.gname=?,g.gdetails=?,g.gpicture=?,g.gprice=?,g.gleixing=?,g.gpinpai=? where g.gid=?"; init(sql); int a = -1; try { ps.setString(1, g.getGname()); ps.setString(2, g.getGdetails()); ps.setString(3, g.getGpicture()); ps.setInt(4, g.getGprice()); ps.setInt(5, g.getGleixing()); ps.setString(6, g.getGpinpai()); ps.setInt(7, g.getGid()); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } public int UpdateGoodNP(Goods g) { String sql = "update GOODS_TABLE g set g.gname=?,g.gdetails=?,g.gprice=?,g.gleixing=?,g.gpinpai=? where g.gid=?"; init(sql); int a = -1; try { ps.setString(1, g.getGname()); ps.setString(2, g.getGdetails()); ps.setInt(3, g.getGprice()); ps.setInt(4, g.getGleixing()); ps.setString(5, g.getGpinpai()); ps.setInt(6, g.getGid()); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } //获取数量 public int getGoodsSum(String scuser) { String sql = "select s.scnum from SHOPPINGCAR s where s.scuser=?"; int sum=0; init(sql); try { ps.setString(1, scuser); rs = ps.executeQuery(); while (rs.next()) { sum+=rs.getInt("scnum"); } } catch (SQLException e) { e.printStackTrace(); } return sum; } public Goods getGoods(String gid) { String sql = "select * from GOODS_TABLE g where g.gid="+gid; init(sql); Goods good=new Goods(); try { rs = ps.executeQuery(); while (rs.next()) { good=new Goods(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getInt(6),rs.getString(7)); } } catch (SQLException e) { e.printStackTrace(); } return good; } /** * 增加日志记录 * @param g * @return */ public int insertLog(Log l) { String sql = "insert into ShopLog values(?,?,?,?,?)"; init(sql); int a = -1; try { ps.setString(1, l.getPname()); ps.setString(2, l.getPugroup()); ps.setString(3, l.getPuptime()); ps.setString(4, l.getPip()); ps.setString(5, l.getPlog()); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } /** * 返回所有日志 * @return */ public List<Log> getAllLogs(){ String sql = "select * from ShopLog s order by s.puptime desc"; init(sql); List<Log> list = new ArrayList<Log>(); try { rs = ps.executeQuery(); while (rs.next()) { Log l=new Log(); l.setPname(rs.getString(1)); l.setPugroup(rs.getString(2)); l.setPuptime(rs.getString(3)); l.setPip(rs.getString(4)); l.setPlog(rs.getString(5)); list.add(l); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 返回购物车数据 * @return */ public List<Order> getAllOrder(String username){ String sql = "select t.* from SHOPPINGCAR t where t.scuser=?"; init(sql); List<Order> list = new ArrayList<Order>(); try { ps.setString(1, username); rs = ps.executeQuery(); while (rs.next()) { Order l=new Order(); l.setScid(rs.getString(1)); l.setScuser(rs.getString(4)); l.setScgid(rs.getString(5)); l.setScnum(rs.getInt(3)); l.setSctime((new Date()).toLocaleString()); list.add(l); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 删除购物车已经提交的信息 */ public int deleteSCar(String username) { String sql = "delete from SHOPPINGCAR g where g.scuser=? "; init(sql); int a = -1; try { ps.setString(1, username); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } /** * 增加订单记录 * @param g * @return */ public int insertOrder(Order l) { String sql = "insert into SHOPORDER values(?,?,?,?,?,?,?)"; init(sql); int a = -1; try { ps.setString(1, l.getScid()); ps.setString(2, l.getScuser()); ps.setString(3, l.getScgid()); ps.setInt(4, l.getScnum()); ps.setInt(5, l.getScmoney()); ps.setString(6, l.getSctime()); ps.setString(7, l.getSczt()); a = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return a; } /** * 返回未确认订单 * @return */ public List<Order> getAllOrder(){ String sql = "select t.* from SHOPORDER t where t.sczt='待确认' order by t.sctime desc"; init(sql); List<Order> list = new ArrayList<Order>(); try { rs = ps.executeQuery(); while (rs.next()) { Order l=new Order(); l.setScid(rs.getString(1)); l.setScuser(rs.getString(2)); l.setScgid(rs.getString(3)); l.setScnum(rs.getInt(4)); l.setScmoney(rs.getInt(5)); l.setSctime(rs.getString(6)); l.setSczt(rs.getString(7)); list.add(l); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 返回确认订单 * @return */ public List<Order> getAllOrder1(){ String sql = "select t.* from SHOPORDER t where t.sczt='已确认' order by t.sctime desc"; init(sql); List<Order> list = new ArrayList<Order>(); try { rs = ps.executeQuery(); while (rs.next()) { Order l=new Order(); l.setScid(rs.getString(1)); l.setScuser(rs.getString(2)); l.setScgid(rs.getString(3)); l.setScnum(rs.getInt(4)); l.setScmoney(rs.getInt(5)); l.setSctime(rs.getString(6)); l.setSczt(rs.getString(7)); list.add(l); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 修改订单状态 * @return */ public void UpdateOrder(String sctime){ String sql = "update SHOPORDER s set s.sczt='已确认' where s.sctime=?"; init(sql); try { ps.setString(1, sctime); ps.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } } /** * 返回用户信息 * @return */ public List<User> getAllUsers(){ String sql = "select t.*, t.rowid from USER_TABLE t order by t.utime desc"; init(sql); List<User> list = new ArrayList<User>(); try { rs = ps.executeQuery(); while (rs.next()) { User l=new User(); l.setUserid(rs.getInt(1)); l.setUname(rs.getString(2)); l.setUpassword(rs.getString(3)); l.setUnickname(rs.getString(4)); l.setUemail(rs.getString(5)); l.setUtime(rs.getString(6)); list.add(l); } } catch (SQLException e) { e.printStackTrace(); } return list; } }
商城项目整理(三)JDBC增删改查的更多相关文章
- JDBC增删改查,PreparedStatement和Statement的区别
此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...
- ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)
Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...
- JDBC增删改查和查唯一的完整代码
第一部分代码(实体类) package com.wf.entity; public class Hehe{ private int hehe_id; private String hehe_name; ...
- jdbc增删改查进行封装
jdbc封装 1 dao (代码分层) com.aaa.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 com.aaa.servlet 存放servle ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 自定义仓储之增删改查
上一篇文章(https://www.cnblogs.com/meowv/p/12913676.html)我们用Code-First的方式创建了博客所需的实体类,生成了数据库表,完成了对EF Core的 ...
- Java项目——模拟电话薄联系人增删改查
该项目模拟了电话本记录联系人的业务功能,用来练习对数据库的增删改查等操作. 菜单类:Menu -- 用来封装主菜单和个选项的子菜单 Person类: Person--联系人的实体类 TelNoteRe ...
- JDBC增删改查
/* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...
- jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决
最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...
- JAVA JDBC 增删改查简单例子
1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...
- JavaJDBC【三、增删改查】
获取数据库连接后,可进行增删改查操作 语句生成: Statement s = con.createStatement(sql); //生成语句 PreparedStatement ps = (Prep ...
随机推荐
- 用StringBuilder和StringBuffer实现的Unicode解码方法的比较(Java)
初衷是用正则来写一个Unicode字符串转码的方法,一开始是打算结合StringBuilder写的,但是看到jdk7的Matcher.appendReplacement文档中一段示例代码用了Match ...
- You may rarely look at it. But you'll always feel it
You may rarely look at it. But you'll always feel it
- 微信小程序支付及退款流程详解
微信小程序的支付和退款流程 近期在做微信小程序时,涉及到了小程序的支付和退款流程,所以也大概的将这方面的东西看了一个遍,就在这篇博客里总结一下. 首先说明一下,微信小程序支付的主要逻辑集中在后端,前端 ...
- OC语言的面向对象—类和对象、方法
1. 面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 1. 面向对象和面向过程的区别 1) 以用电脑听歌为例子 a) 面向过程 ➢ 打开电脑 ➢ ...
- #openstack centos6 centos7 kvm镜像制作
#openstack centos6 centos7 kvm 镜像制作 openstack windows 2008镜像 制作 http://www.cnblogs.com/elvi/p/800129 ...
- HDU 1014 Uniform Generator 欧几里得
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 解题思路: 1. 把题目意思读懂后,明白会输入两个数,然后根据题中的公式产生一系列伪随机数,看这 ...
- yii2.0中数据缓存之增删改查
public function actionSss(){ /* * 获取到缓存 * 这里是获取的是根目录下 的common/main.php中的缓存类组件 * */ $cache=\Yii::$app ...
- Spring JDBC(二)SimpleJdbcInsert
上一篇写了关于jdbcTemplate的一些基本使用,这一篇来聊聊SimpleJdbcInsert SimpleJdbcInsert是springjdbc提供的一个简化插入操作的类,下面来看一下常用的 ...
- iBeacon硬件厂商和产品
最近项目比较忙,也没有时间写Blog. 这几天因为项目需要采购为数不小的iBeacon,所以花了点时间对市面上主要的iBeacon简单的调研总结了一下. 分享给需要买相关设备进行试验的小伙伴,数据不权 ...
- ORACLE SQL 整理
1.查询字段中含有小写字母的数据 SELECT MATERIALCODE FROM RFXITEMATTENDCODE WHERE REGEXP_LIKE(MATERIALCODE,'([a-z])' ...