我们的项目名称为:来换网,是一个以发布二手交易消息为主的消息平台
我负责的是数据库层的代码,数据库的环境用的是Mysql,其中JDBC在DAO层实现
这项工作对于我来说是零基础的,以前从未涉及所以时间有限,仅能模仿着其他大佬编写有关代码。
参照DAO设计模式:代码共分为五个类
首先是JDBC数据库连接类:连接MySQL数据库下的JavaTest数据库
package goods.jdbc; import java.sql.Connection; import java.sql.DriverManager; // 主要功能就是连接数据库、关闭数据库 public class DataBaseConnection { // 定义数据库驱动类 private final String DBDRIVER = "com.mysql.jdbc.Driver"; // 定义数据库连接URL private final String DBURL = "jdbc:mysql:///test"; // 定义数据库连接用户名 private final String DBUSER = "root"; // 定义数据库连接密码 private final String DBPASSWORD = "root"; // 定义数据库连接对象 private Connection conn = null; // 构造方法,加载驱动 public DataBaseConnection() { try { Class.forName(DBDRIVER); this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); } catch (Exception e) { System.out.println("加载驱动失败"); } } // 取得数据库连接 public Connection getConnection() { return conn; } // 关闭数据库连接 public void close() { try { conn.close(); } catch (Exception e) { System.out.println("数据库连接关闭失败"); } } }
其次是VO值对象,与数据库表一一对应的Java类。含有与数据库表字段一一对应的属性,相应属性的getter和setter方法。
package goods.instance; public class Goods { private String goodsid; // 物品ID private String goodsname;// 物品名字 private String userid; // 所属用户名 private String style; // 物品类型 private String exchangestyle; // 想交换物品类型 private String picture; // 图片 public String getGoodsid() { return goodsid; } public void setGoodsid(String goodsid) { this.goodsid = goodsid; } public String getGoodsname() { return goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public String getExchangestyle() { return exchangestyle; } public void setExchangestyle(String exchangestyle) { this.exchangestyle = exchangestyle; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } }
接下来是DAO接口:定义了所有的用户的操作,如添加记录、删除记录和查询记录等。
DAO_Interface
DAO实现类.
package goods.crud_implements; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import goods.instance.Goods; import goods.jdbc.DataBaseConnection; public class Goods_crud_implements implements goods.crud_interface.Goods_crud_interface { // 添加商品操作 public void insert(Goods goods) throws Exception { String sql = "INSERT INTO goods(goodsname,userid,style,exchangestyle) VALUES(?,?,?,?)"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1, goods.getGoodsname()); pstmt.setString(2, goods.getUserid()); pstmt.setString(3, goods.getStyle()); pstmt.setString(4, goods.getExchangestyle()); // 进行数据库更新操作 pstmt.executeUpdate(); pstmt.close(); } catch (Exception e) { throw new Exception("操作出现异常"); } finally { // 关闭数据库连接 dbc.close(); } } // 修改操作 public void update(Goods goods) throws Exception { String sql = "UPDATE goods SET goodsname=?,userid=?,style=?,exchangestyle=? WHERE goodsid=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1, goods.getGoodsname()); pstmt.setString(2, goods.getUserid()); pstmt.setString(3, goods.getStyle()); pstmt.setString(4, goods.getExchangestyle()); pstmt.setString(5, goods.getGoodsid()); // 进行数据库更新操作 pstmt.executeUpdate(); pstmt.close(); } catch (Exception e) { throw new Exception("操作出现异常"); } finally { // 关闭数据库连接 dbc.close(); } } // 删除操作(按商品编号删除) public void delete(String goodsid) throws Exception { String sql = "DELETE FROM goods WHERE goodsid=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1, goodsid); // 进行数据库更新操作 pstmt.executeUpdate(); pstmt.close(); } catch (Exception e) { throw new Exception("操作出现异常"); } finally { // 关闭数据库连接 dbc.close(); } } // 按商品名字查询 public Goods querBygoodsName(String goodsname) throws Exception { Goods goods = null; String sql = "SELECT * FROM goods WHERE goodsname=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1, goodsname); // 进行数据库查询操作 ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // 查询出内容,之后将查询出的内容赋值给goods对象 goods = new Goods(); goods.setGoodsid(rs.getString(1)); goods.setGoodsname(rs.getString(2)); goods.setUserid(rs.getString(3)); goods.setStyle(rs.getString(4)); goods.setExchangestyle(rs.getString(5)); } rs.close(); pstmt.close(); } catch (Exception e) { throw new Exception("操作出现异常"); } finally { // 关闭数据库连接 dbc.close(); } return goods; } // 查询全部 public List querAll() throws Exception { List<Goods> all = new ArrayList<Goods>(); String sql = "SELECT * FROM goods "; PreparedStatement pstmt = null; DataBaseConnection dbc = null; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); // 进行数据库查询操作 ResultSet rs = pstmt.executeQuery(); while (rs.next()) { // 查询出内容,之后将查询出的内容赋值给user对象 Goods goods = new Goods(); goods.setGoodsid(rs.getString(1)); goods.setGoodsname(rs.getString(2)); goods.setUserid(rs.getString(3)); goods.setStyle(rs.getString(4)); goods.setExchangestyle(rs.getString(5)); // 将查询出来的数据加入到List对象之中 all.add(goods); } rs.close(); pstmt.close(); } catch (Exception e) { throw new Exception("操作出现异常"); } finally { // 关闭数据库连接 dbc.close(); } return all; } }
最后是DAO的工厂类,通过DAO工厂类来获取具体的DAO实现类。
package goods.factory; import goods.crud_implements.Goods_crud_implements; import goods.crud_interface.Goods_crud_interface; public class Factory { public static Goods_crud_interface getGoodsDAOInstance() { return new Goods_crud_implements(); } }
以上,关键技术就是这些了。