本文共 16069 字,大约阅读时间需要 53 分钟。
目录
使用 Java语言实现手机信息管理的功能,Oracle11g作为数据库,主菜单包括菜单项:如图效果(1,2,3,4,5分别 实现功能,0的时候退出系统,其它的输入提示输入错误,请重新输入)
表名 | Mobile | 中文表名称 | 手机信息表 | |||
序号 | 字段名称 |
字段说明 | 类型 | 长度 |
属性 | 备注 |
1 | ID | 序号 | number |
| 序列 | 主键,非空 |
2 | Brand | 品牌 | Varchar2 | 50 |
| 非空 |
3 | Model | 型号 | Varchar2 | 50 |
| 非空 |
4 | Price | 价格 | Number(9,2) |
|
| 非空 |
5 | Count | 数量 | number |
|
| 非空 |
6 | Version | 版本 | Varchar2 | 50 |
| 非空 |
/*Navicat MySQL Data TransferSource Server : localhost_3306Source Server Version : 50527Source Host : localhost:3306Source Database : mobTarget Server Type : MYSQLTarget Server Version : 50527File Encoding : 65001Date: 2019-04-28 18:59:04*/SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for mobile-- ----------------------------DROP TABLE IF EXISTS `mobile`;CREATE TABLE `mobile` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号', `brand` varchar(50) NOT NULL COMMENT '品牌', `model` varchar(50) NOT NULL COMMENT '型号', `price` double(9,2) NOT NULL COMMENT '价格', `count` int(11) NOT NULL COMMENT '数量', `version` varchar(50) NOT NULL COMMENT '版本', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ------------------------------ Records of mobile-- ----------------------------INSERT INTO `mobile` VALUES ('1', 'Apple', 'IPHone4S', '4999.00', '3', '国行');INSERT INTO `mobile` VALUES ('2', 'SONY', 'Z3L55', '4999.00', '10', '智享版');INSERT INTO `mobile` VALUES ('3', '华为', 'AscendP7', '2388.00', '3', '16G版');
package com.cw.mobile.test;import com.cw.mobile.view.MobView;/** * 测试类 */public class TestMob { public static void main(String[] args) { new MobView().show(); }}
package com.cw.util;import java.io.IOException;import java.sql.*;import java.util.Properties;/** * JDBC操作工具类, 提供注册驱动, 连接, 发送器, 动态绑定参数, 关闭资源等方法 * jdbc连接参数的提取, 使用Properties进行优化(软编码) */public class JDBCUtils { private static String driver; private static String url; private static String user; private static String password; static { // 借助静态代码块保证配置文件只读取一次就行 // 创建Properties对象 Properties prop = new Properties(); try { // 加载配置文件, 调用load()方法 // 类加载器加载资源时, 去固定的类路径下查找资源, 因此, 资源文件必须放到src目录才行 prop.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties")); // 从配置文件中获取数据为成员变量赋值 driver = prop.getProperty("db.driver").trim(); url = prop.getProperty("db.url").trim(); user = prop.getProperty("db.user").trim(); password = prop.getProperty("db.password").trim(); // 加载驱动 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 统一关闭资源 * * @param rs * @param stmt * @param conn */ public static void close(ResultSet rs, Statement stmt, Connection conn) { try { if(rs != null){ rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(stmt != null){ stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 动态绑定参数 * * @param pstmt * @param params */ public static void bindParam(PreparedStatement pstmt, Object... params) { try { for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } } catch (SQLException e) { e.printStackTrace(); } } /** * 预处理发送器 * * @param conn * @param sql * @return */ public static PreparedStatement getPstmt(Connection conn, String sql) { PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } /** * 获取发送器的方法 * * @param conn * @return */ public static Statement getStmt(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } /** * 获取数据库连接的方法 * * @return */ public static Connection getConn() { Connection conn = null; try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return conn; }}
db.properties
db_driver=com.mysql.jdbc.Driverdb_user=rootdb_password=12345db_url=jdbc:mysql://localhost:3306/mob?useUnicode=true&characterEncoding=UTF8&useSSL=false
package com.cw.mobile.view;import com.cw.mobile.pojo.Mob;import com.cw.mobile.service.MobService;import com.cw.mobile.service.impl.MobServiceImpl;import java.util.List;import java.util.Scanner;/** * 视图层 */public class MobView { private Scanner sc = new Scanner(System.in); private MobService service = new MobServiceImpl(); public void show() { System.out.println("******************************"); System.out.println(" 欢迎使用手机信息管理系统"); System.out.println("******************************"); while (true) { System.out.println("\n********功能菜单*********"); System.out.println("1.手机录入"); System.out.println("2.根据手机品牌查询手机信息"); System.out.println("3.查询全部手机信息"); System.out.println("4.根据手机编号修改手机价格"); System.out.println("5.根据手机编号删除手机记录"); System.out.println("6.退出"); System.out.print("请输入对应数字进行操作:"); int op = sc.nextInt(); switch (op) { case 1: addMob(); continue; case 2: queryByBrand(); continue; case 3: queryAll(); continue; case 4: updPriceById(); continue; case 5: delById(); continue; case 6: System.out.println("bye bye!"); break; default: System.out.println("输入有误,请重新输入!"); break; } break; } } /** * 根据手机编号删除手机记录 */ private void delById() { System.out.println("请输入要删除的手机的编号:"); int id = sc.nextInt(); System.out.println(service.deleteMob(id) ? "删除成功!" : "对不起,您所在删除的手机不存在!"); queryAll(); } /** * 根据手机编号修改手机价格 */ private void updPriceById() { System.out.println("请输入要修改的手机编号:"); int id = sc.nextInt(); System.out.println("请输入修改后的价格:"); double price = sc.nextDouble(); System.out.println(service.modifyPrice(id, price) ? "修改成功!" : "修改失败!"); queryAll(); } /** * 查询全部手机信息 */ private void queryAll() { Listlist = service.queryAllMob(); System.out.println("\n序号\t\t品牌\t\t型号\t\t价格\t\t数量\t\t版本"); for (Mob mob : list) { System.out.println(mob.getId() + "\t\t" + mob.getBrand() + "\t\t" + mob.getModel() + "\t\t" + mob.getPrice() + "\t\t" + mob.getCount() + "\t\t" + mob.getVersion()); } System.out.println(); } /** * 根据手机品牌查询手机信息 */ private void queryByBrand() { System.out.println("请输入要查询的手机品牌:"); String brand = sc.next(); List list = service.queryByBrand(brand); System.out.println("\n序号\t\t品牌\t\t型号\t\t价格\t\t数量\t\t版本"); for (Mob mob : list) { System.out.println(mob.getId() + "\t\t" + mob.getBrand() + "\t\t" + mob.getModel() + "\t\t" + mob.getPrice() + "\t\t" + mob.getCount() + "\t\t" + mob.getVersion()); } } /** * 录入手机信息 */ private void addMob() { Mob emp = new Mob(); System.out.println("请输入手机品牌:"); emp.setBrand(sc.next()); System.out.println("请输入手机型号:"); emp.setModel(sc.next()); System.out.println("请输入手机价格:"); emp.setPrice(sc.nextDouble()); System.out.println("请输入手机数量:"); emp.setCount(sc.nextInt()); System.out.println("请输入手机版本:"); emp.setVersion(sc.next()); if (service.save(emp)) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } }}
package com.cw.mobile.service;import com.cw.mobile.pojo.Mob;import java.util.List;public interface MobService { /** * 新增手机记录 * @param mob * @return */ boolean save(Mob mob); /** * 根据手机编号修改手机价格 * @param id * @param price * @return */ boolean modifyPrice(int id, double price); /** * 根据手机编号删除手机记录 * @param id * @return */ boolean deleteMob(int id); /** * 查询所有手机记录 * @return */ ListqueryAllMob(); /** * 根据手机品牌模糊查询手机记录 * @param brand * @return */ List queryByBrand(String brand);}
package com.cw.mobile.service.impl;import com.cw.mobile.dao.MobDao;import com.cw.mobile.dao.impl.MobDaoImpl;import com.cw.mobile.pojo.Mob;import com.cw.mobile.service.MobService;import java.util.List;public class MobServiceImpl implements MobService { private MobDao mobDao = new MobDaoImpl(); @Override public boolean save(Mob mob) { return mobDao.insertMob(mob) > 0 ? true : false; } @Override public boolean modifyPrice(int id, double price) { return mobDao.updatePriceById(id,price)>0; } @Override public boolean deleteMob(int id) { return mobDao.deleteById(id)>0; } @Override public ListqueryAllMob() { return mobDao.selectAll(); } @Override public List queryByBrand(String brand) { return mobDao.selectByBrand(brand); }}
package com.cw.mobile.dao;import com.cw.mobile.pojo.Mob;import java.util.List;public interface MobDao { /** * 新增手机记录 * @param mob * @return */ int insertMob(Mob mob); /** * 根据手机编号修改手机价格 * @param id * @param price * @return */ int updatePriceById(int id, double price); /** * 根据手机编号删除手机记录 * @param id * @return */ int deleteById(int id); /** * 查询所有手机记录 * @return */ ListselectAll(); /** * 根据手机品牌模糊查询手机记录 * @param brand * @return */ List selectByBrand(String brand);}
package com.cw.mobile.dao.impl;import com.cw.mobile.dao.BaseDao;import com.cw.mobile.dao.MobDao;import com.cw.mobile.pojo.Mob;import java.util.List;public class MobDaoImpl extends BaseDao implements MobDao { @Override public int insertMob(Mob mob) { String sql = "insert into mobile values (null,?,?,?,?,?)"; return update(sql, mob.getBrand(), mob.getModel(), mob.getPrice(), mob.getCount(), mob.getVersion()); } @Override public int updatePriceById(int id, double price) { String sql = "update mobile set price=? where id=?"; return update(sql, price, id); } @Override public int deleteById(int id) { String sql = "delete from mobile where id=?"; return update(sql, id); } @Override public ListselectAll() { String sql = "select * from mobile"; return queryList(Mob.class, sql); } @Override public List selectByBrand(String brand) { String sql = "select * from mobile where brand like ?"; return queryList(Mob.class, sql, "%" + brand + "%"); }}
package com.cw.mobile.dao;import com.cw.mobile.util.JDBCUtils;import java.lang.reflect.Method;import java.sql.*;import java.util.ArrayList;import java.util.List;/** * 当前dao用于封装查询和更新的统一方法, 要求所有其他dao应该继承 */public class BaseDao { /** * 执行DML操作的统一方法 * * @param sql * @param params * @return */ public int update(String sql, Object... params) { Connection conn = JDBCUtils.getConn(); PreparedStatement pstmt = JDBCUtils.getPstmt(conn, sql); JDBCUtils.bindParam(pstmt, params); try { return pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(null, pstmt, conn); } return 0; } /** * 查询多条数据的统一方法 * * @param sql * @param params * @return */ publicList queryList(Class cls, String sql, Object... params) { List list = new ArrayList<>(); Connection conn = JDBCUtils.getConn(); PreparedStatement pstmt = JDBCUtils.getPstmt(conn, sql); JDBCUtils.bindParam(pstmt, params); ResultSet rs = null; try { rs = pstmt.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); while (rs.next()) { //相当于Mob mob=new Mob(); T bean = cls.newInstance(); for (int i = 1; i <= metaData.getColumnCount(); i++) { String columnLabel = metaData.getColumnLabel(i); Class type = cls.getDeclaredField(columnLabel).getType(); Method method = cls.getMethod("set" + columnLabel.substring(0, 1).toUpperCase() + columnLabel.substring(1), type); method.invoke(bean, rs.getObject(columnLabel)); } list.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, pstmt, conn); } return list; } /** * 查询单个数据的统一方法 * * @param sql * @param params * @return */ public T queryOne(Class cls, String sql, Object... params) { List list = queryList(cls, sql, params); return list.size() > 0 ? list.get(0) : null; }}
package com.cw.mobile.pojo;import java.io.Serializable;public class Mob implements Serializable { private Integer id; private String brand; private String model; private Double price; private Integer count; private String version; public Mob() { } public Mob(Integer id, String brand, String model, Double price, Integer count, String version) { this.id = id; this.brand = brand; this.model = model; this.price = price; this.count = count; this.version = version; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } @Override public String toString() { return "Mob{" + "id=" + id + ", brand='" + brand + '\'' + ", model='" + model + '\'' + ", price=" + price + ", count=" + count + ", version='" + version + '\'' + '}'; }}
转载地址:http://pojnn.baihongyu.com/