mysql-connector-java-5.0.8-bin.jar
码云链接:https://gitee.com/vvcat/fruit_store
mysql-connector-java-5.0.8-bin.jar 微云下载链接:https://share.weiyun.com/5jp0fIr
将 mysql-connector-java-5.0.8-bin.jar 文件添加到resources目录下
此时并没有将 mysql-connector-java-5.0.8-bin.jar 加载到我们的程序中,需要进行以下操作
选择OK
此时这个jar包导入了我们的项目中,可以查看jar包中的代码
IDEA分为
商业版本:Ultimate Edition;
免费版本:Community Edition;
如果出现Failed 说明 数据库测试链接失败,请检查Database,User,Password等信息是否输入正确
出现Successful 说明 数据库 测试连接成功,我们可以使用和调用数据库的数据了。
CREATE DATABASE fruit_store;
USE fruit_store;
CREATE TABLE fruits(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NULL,
price DOUBLE NULL,
unit VARCHAR(50) NULL,
expiredDate DATE NULL,
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=1 CHARSET=utf8;
INSERT INTO fruits (id, name, price, unit, expiredDate)
VALUES
(1, '香蕉', '10.5', '斤', '2019-9-15'),
(2, '苹果', '1.5', '斤', '2019-9-16');
package com.gitee.vvcat;
import java.text.ParseException;
import java.util.Scanner;
import com.gitee.vvcat.ui.AdminController;
public class Main {
public static void main(String[] args) throws ParseException {
AdminController adminController = new AdminController();
while (true) {
int choice = adminController.menu();
switch (choice) {
case 1:
adminController.queryAll();
break;
case 2:
adminController.addFruit();
break;
case 3:
adminController.updateFruit();
break;
case 4:
adminController.deleteFruit();
break;
case 5:
System.out.println("欢迎下次使用!");
return;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
}
package com.gitee.vvcat.ui;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:59
* @Version 1.0
**/
import com.gitee.vvcat.model.Fruit;
import com.gitee.vvcat.services.AdminService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class AdminController {
AdminService adminService= new AdminService();
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("\n\t\t商品管理系统");
System.out.println("\t\t\t1:查询商品清单");
System.out.println("\t\t\t2:添加新信息");
System.out.println("\t\t\t3:更新信息");
System.out.println("\t\t\t4:删除信息");
System.out.println("\t\t\t5:退出系统");
System.out.print("\t\t请选择你的操作(1-5):");
return scanner.nextInt();
}
public void queryAll() {
ArrayList<Fruit> fruits = adminService.queryFruits();
System.out.println("商品的所有清单");
System.out.println("编号| 名称| 价格 | 单位 | 过期日期");
for(Fruit item:fruits){
System.out.println(item);
}
}
public void addFruit() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Scanner scanner = new Scanner(System.in);
System.out.println("添加信息:");
System.out.print("请输入编号:");
int id = scanner.nextInt();
System.out.print("请输入名称:");
String name = scanner.next();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
System.out.print("请输入单位:");
String unit = scanner.next();
System.out.print("请输入过期日期:");
Date expiredDate=sdf.parse(scanner.next());
if (adminService.addFruit(id,name,price, unit,expiredDate)){
System.out.println("\t信息添加成功!");
}else {
System.out.println("\t信息添加失败!");
}
}
public void updateFruit() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Scanner scanner = new Scanner(System.in);
System.out.println("更新某个信息:");
System.out.print("请输入要更改的编号:");
int id = scanner.nextInt();
System.out.print("请输入新的名称:");
String name = scanner.next();
System.out.print("请输入新的价格:");
double price = scanner.nextDouble();
System.out.print("请输入新的单位:");
String unit = scanner.next();
System.out.print("请输入新的过期日期:");
Date expiredDate=sdf.parse(scanner.next());
if (adminService.updateFruit(id,name,price, unit,expiredDate)){
System.out.println("\t信息更新成功!");
}else {
System.out.println("\t信息更新失败!");
}
}
public void deleteFruit() {
Scanner scanner = new Scanner(System.in);
System.out.println("删除某个信息:");
System.out.print("请输入要删除的编号:");
int id= scanner.nextInt();
if(adminService.deleteFruit(id)){
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
}
package com.gitee.vvcat.services;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:58
* @Version 1.0
**/
import com.gitee.vvcat.model.*;
import com.gitee.vvcat.data.FruitRepository;
import java.util.ArrayList;
import java.util.Date;
public class AdminService {
private Repository<Fruit> repository=new FruitRepository();
public ArrayList<Fruit> queryFruits(){
return repository.all();
}
public boolean addFruit(int id,String name ,double price,String unit,Date expiredDate){
Fruit item = repository.getById(id);
if (item==null){
Fruit fruit = new Fruit(id, name, price, unit, expiredDate);
repository.insert(fruit);
return true;
}
return false;
}
public boolean updateFruit(int id,String name ,double price,String unit,Date expiredDate){
Fruit item = repository.getById(id);
if (item!=null){
item.setName(name);
item.setPrice(price);
item.setUnit(unit);
item.setExpiredDate(expiredDate);
repository.update(item);
return true;
}
return false;
}
public boolean deleteFruit(int id){
Fruit item = repository.getById(id);
if (item!=null){
repository.delete(id);
return true;
}
return false;
}
}
package com.gitee.vvcat.model;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:57
* @Version 1.0
**/
import java.util.ArrayList;
public interface Repository<T> {
ArrayList<T> all();
T getById(int id);
void insert(T entity);
void update(T entity);
void delete(int id);
}
package com.gitee.vvcat.model;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:57
* @Version 1.0
**/
import java.text.SimpleDateFormat;
import java.util.Date;
public class Fruit {
private int id;
private String name;
private double price;
private String unit;
private Date expiredDate;
public Fruit() {
}
public Fruit(int id, String name, double price, String unit, Date expiration) {
this.id = id;
this.name = name;
this.price = price;
this.unit = unit;
this.expiredDate = expiration;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public Date getExpiredDate() {
return expiredDate;
}
public void setExpiredDate(Date expiredDate) {
this.expiredDate = expiredDate;
}
@Override
public String toString() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return "\t"+getId()+"\t"+getName()+"\t"+getPrice()+"\t"+getUnit()+"\t"+simpleDateFormat.format(getExpiredDate());
}
public boolean isExpired(){
if (new Date().before(expiredDate)){
return false;
}
return true;
}
}
package com.gitee.vvcat.data;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:58
* @Version 1.0
**/
import com.gitee.vvcat.model.Fruit;
import com.gitee.vvcat.model.Repository;
import com.gitee.vvcat.util.DbUtility;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
public class FruitRepository implements Repository<Fruit> {
@Override
public ArrayList<Fruit> all() {
Connection conn=null;
Statement smt=null;
ResultSet rs=null;
String sql = "select * from fruits";
ArrayList<Fruit> result = new ArrayList<Fruit>();
try {
conn = DbUtility.getConnection();
smt=conn.createStatement();
rs=smt.executeQuery(sql);
while(rs.next()){
Fruit fruit = new Fruit();
fruit.setId(rs.getInt("id"));
fruit.setName(rs.getString("name"));
fruit.setPrice(rs.getDouble("price"));
fruit.setUnit(rs.getString("unit"));
fruit.setExpiredDate(new Date(rs.getDate("expireddate").getTime()));
result.add(fruit);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DbUtility.release(smt,conn,rs);
}
return result;
}
@Override
public Fruit getById(int id) {
Connection conn=null;
PreparedStatement smt=null;
ResultSet rs=null;
String sql = "select * from fruits where id=?";
try {
conn = DbUtility.getConnection();
smt=conn.prepareStatement(sql);
smt.setInt(1,id);
rs=smt.executeQuery();
if (rs.next()){
Fruit fruit = new Fruit();
fruit.setId(rs.getInt("id"));
fruit.setName(rs.getString("name"));
fruit.setPrice(rs.getDouble("price"));
fruit.setUnit(rs.getString("unit"));
fruit.setExpiredDate(new Date(rs.getDate("expireddate").getTime()));
return fruit;
}
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DbUtility.release(smt,conn,rs);
}
return null;
}
@Override
public void insert(Fruit entity) {
Connection conn=null;
PreparedStatement smt=null;
String sql = "insert into fruits(id,name,price,unit,expireddate) values(?,?,?,?,?)";
try {
conn=DbUtility.getConnection();
smt=conn.prepareStatement(sql);
smt.setInt(1,entity.getId());
smt.setString(2,entity.getName());
smt.setDouble(3,entity.getPrice());
smt.setString(4,entity.getUnit());
smt.setDate(5,new java.sql.Date(entity.getExpiredDate().getTime()));
smt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DbUtility.release(smt,conn);
}
}
@Override
public void update(Fruit entity) {
Connection conn=null;
PreparedStatement smt=null;
String sql = "UPDATE fruits SET name=? , price=? ,unit=?,expireddate=? WHERE id=?";
try {
conn=DbUtility.getConnection();
smt=conn.prepareStatement(sql);
smt.setString(1,entity.getName());
smt.setDouble(2,entity.getPrice());
smt.setString(3,entity.getUnit());
smt.setDate(4,new java.sql.Date(entity.getExpiredDate().getTime()));
smt.setInt(5,entity.getId());
smt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DbUtility.release(smt,conn);
}
}
@Override
public void delete(int id) {
Connection conn=null;
PreparedStatement smt=null;
String sql = "DELETE FROM fruits WHERE id=?";
try {
conn=DbUtility.getConnection();
smt=conn.prepareStatement(sql);
smt.setInt(1,id);
smt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DbUtility.release(smt,conn);
}
}
}
package com.gitee.vvcat.util;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/9/11 11:59
* @Version 1.0
**/
import java.sql.*;
public class DbUtility {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/fruit_store";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void release(Statement smt,Connection conn){
release(smt);
release(conn);
}
private static<T extends AutoCloseable> void release(T smt) {
if (smt!=null){
try {
smt.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
smt=null;
}
}
public static void release(Statement smt,Connection conn,ResultSet rs){
release(smt);
release(conn);
release(rs);
}
}