Commodity management system (stored in database)
brief introduction
For the first time, if there is an error in my blog, I can send a private letter. Thank you for your correction.
If you can write, I hope you can pay attention to it, and will continue to update the learning process and make progress together.
The source code is at the end
Project requirements
- Able to design reasonable entity classes according to project requirements
- Standard coding
- After closing the program, the entered data will still exist when the program is opened next time
Project requirements
- Commodity addition (enter commodity code, name, price and quantity)
- Product list display
- Commodity warehousing (enter the commodity number, and then prompt the user to enter the warehousing quantity for warehousing)
- Commodity delivery (enter the commodity number and prompt the user to enter the delivery quantity for delivery)
- Modify the commodity price (prompt the user to enter the number and enter the new price)
- Delete goods (prompt the user to enter the number and confirm whether to delete)
preparation
Software preparation
-
There is a database; IDEA
-
Download jar package Click here to search and download
- mysql
- Of c3p0 connection pool
-
Import jar package
Document preparation
- create package
-
DataSouce.properties configuration class information file
# Database configuration information jdbc_url=jdbc:mysql://localhost:3306/tables jdbc_driver=com.mysql.cj.jdbc.Driver jdbc_user=123 jdbc_password=123
-
Env class reads the component class of the property file
import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * Read the properties file and load the class */ public final class Env extends Properties { public static final String JDBC_URL; public static final String JDBC_DRIVER; public static final String JDBC_USER; public static final String JDBC_PASSWORD; public static Env env; /* The path and name of the database properties file */ public static final String CONF_FILE="com\\sjtest\\conf\\DataSouce.properties"; static { if(env==null){//Assign value when Env is empty env=new Env(); } //Gets the file stream of the properties file InputStream input = env.getClass().getClassLoader().getResourceAsStream(CONF_FILE); try { env.load(input);//Load file stream } catch (IOException e) { e.printStackTrace(); }finally { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } //assignment JDBC_URL=env.getProperty("jdbc_url"); JDBC_DRIVER=env.getProperty("jdbc_driver"); JDBC_USER=env.getProperty("jdbc_user"); JDBC_PASSWORD=env.getProperty("jdbc_password"); } }
-
DataSourPool class data source management component class
import com.mchange.v2.c3p0.ComboPooledDataSource; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DataSourPool { private static ComboPooledDataSource c3p0; /** * Create ComboPooledDataSource data source */ private static void createComboPooledDataSource(){ if(c3p0==null){//If c3p0 is an empty assignment c3p0=new ComboPooledDataSource(); } /* Data source related properties */ try { c3p0.setDriverClass(Env.JDBC_DRIVER); c3p0.setUser(Env.JDBC_USER); c3p0.setPassword(Env.JDBC_PASSWORD); c3p0.setJdbcUrl(Env.JDBC_URL); } catch (PropertyVetoException e) { e.printStackTrace(); } } /** * Method to get Connection interface * @return */ public static Connection getConnection(){ Connection conn=null; createComboPooledDataSource();//create data source try { conn=c3p0.getConnection();//There is a data source to get and open a connection } catch (SQLException throwables) { throwables.printStackTrace(); } return conn; } /* Method overloading */ /** * Closing method * @param conn */ public static void close(Connection conn) { try { if(conn!=null) { conn.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void close(Statement state) { try { if (state!=null) { state.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void close(ResultSet resu) { try { if (resu != null) { resu.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
-
Entity class of Goods
/** * Map item table */ public class Goods { //Item number private String id; //Trade name private String name; //commodity price private int price; //Quantity of goods private int number; /* Defines a constructor with and without parameters */ public Goods() { } public Goods(String id, String name, int price, int number) { this.id = id; this.name = name; this.price = price; this.number = number; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } /* Override toString method */ @Override public String toString() { return "Goods{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", price=" + price + ", number=" + number + '}'; } }
-
Util tool class note
import com.sjtest.Exception.NoneException; import com.sjtest.pojo.Goods; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class Util { /** * Get a list of all products * * @return All product information */ public static List<Goods> AllGoods() { List<Goods> list = new ArrayList<>(); String sql = "select id Item number ,name Trade name,price commodity price, number Quantity of goods from goods"; Connection conn = DataSourPool.getConnection();//Get database connection PreparedStatement ps = null;//Create PreparedStatement interface ResultSet set = null; //Create ResultSet interface try { ps = conn.prepareStatement(sql);//Gets the PreparedStatement interface object set = ps.executeQuery(); //Query returns ResultSet result set object /* Processing result sets encapsulates the objects in the result set */ while (set.next()) { Goods goods = new Goods(); goods.setId(set.getString(1)); goods.setName(set.getString(2)); goods.setPrice(set.getInt(3)); goods.setNumber(set.getInt(4)); list.add(goods); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally {//Close connection DataSourPool.close(conn); DataSourPool.close(ps); DataSourPool.close(set); } return list; } /** * Query goods for an id * * @param id Item number * @return */ public static Goods findIDOutGoods(String id) { List<Goods> list = Util.AllGoods(); for (Goods goods : list) { if (goods.getId().equals(id)) { return goods; } } //If the target has no throwing exception try { throw new NoneException("Find target does not exist");//Custom exception class } catch (NoneException e) { e.printStackTrace(); } return null; } /** * Show all IDs and names */ public static void showIDAndName(){ List<Goods> list = Util.AllGoods(); for (Goods goods : list) { System.out.println(goods.getId()+"---"+goods.getName()); } } }
-
GoodsDao interface definition method
import com.sjtest.pojo.Goods; public interface GoodsDao { /** * Commodity addition (enter commodity code, name, price and quantity) * * @param goods commodity */ void addGoods(Goods goods); /** * Commodity display */ void showGoods(); /** Commodity warehousing (enter the commodity number, and then prompt the user to enter the warehousing quantity for warehousing) * @param id Item number */ void inGoods(String id); /** * Commodity delivery (enter the commodity number and prompt the user to enter the delivery quantity for delivery) * @param id Item number */ void outGoods(String id); /** * Modify the commodity price (prompt the user to enter the number and enter the new price) * @param id Item number */ void setGoodsPrice(String id); /** * Delete item * @param id Item number */ void deleteGoods(String id); }
-
Exception class
public class ExistExcetion extends Exception{ public ExistExcetion() { } public ExistExcetion(String message) { super(message); } }
The other two are the same
Realize function
The GoodsDaoImp class implements the functions of the GoodsDao interface
import com.sjtest.Exception.ExistExcetion; import com.sjtest.Exception.NotNumberException; import com.sjtest.dao.GoodsDao; import com.sjtest.pojo.Goods; import com.sjtest.util.DataSourPool; import com.sjtest.util.Util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Scanner; public class GoodsDaoImp implements GoodsDao { Scanner scan = new Scanner(System.in); //Get interface object Connection conn = null; PreparedStatement ps = null; ResultSet set = null; @Override public void addGoods(Goods goods) { int res = 0; List<Goods> list = Util.AllGoods();//Get all items for (Goods goods1 : list) { if(goods1.getId().equals(goods.getId())){ try { throw new ExistExcetion("Goods already exist"); } catch (ExistExcetion existExcetion) { existExcetion.printStackTrace(); } } } String sql = " insert into goods(id,name,price,number) values(?,?,?,?)"; conn = DataSourPool.getConnection(); try { ps = conn.prepareStatement(sql); //Gets the PreparedStatement interface object /* Replace placeholders in sql */ ps.setString(1, goods.getId()); ps.setString(2, goods.getName()); ps.setInt(3, goods.getPrice()); ps.setInt(4, goods.getNumber()); res = ps.executeUpdate();//Execute sql statement } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(conn); DataSourPool.close(ps); } System.out.println("Added successfully"); System.out.println("Add rows: " + res + "that 's ok"); } @Override public void showGoods() { List<Goods> list = Util.AllGoods(); System.out.println("Item number\t Trade name\t commodity price\t\t Quantity of goods"); for (Goods goods : list) { System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t\t" + goods.getNumber()); } } @Override public void inGoods(String id) { int res = 0; String sql = "update goods set number =? where id=?"; try { conn = DataSourPool.getConnection();//Get connection ps = conn.prepareStatement(sql); Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "also" + goods.getNumber() + "individual"); /* Replace placeholder */ System.out.println("Enter the quantity to be warehoused:"); int number = scan.nextInt(); number = number + goods.getNumber(); ps.setInt(1, number); ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Warehousing succeeded"); System.out.println("commodity " + goods.getName() + "also" + number + "individual"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(conn); DataSourPool.close(ps); } } @Override public void outGoods(String id) { int res = 0; String sql = "update goods set number =? where id=?"; try { conn = DataSourPool.getConnection();//Get connection ps = conn.prepareStatement(sql); Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "also" + goods.getNumber() + "individual"); /* Replace placeholder */ System.out.println("Enter the quantity you want to issue:"); int numberOut = scan.nextInt(); int numberNow = goods.getNumber();//Existing quantity if (numberNow > numberOut) { ps.setInt(1, numberNow - numberOut); } else { try { throw new NotNumberException("Insufficient inventory"); } catch (NotNumberException e) { e.printStackTrace(); } } ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Delivery succeeded"); System.out.println("commodity " + goods.getName() + "also" + (numberNow - numberOut) + "individual"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(conn); DataSourPool.close(ps); } } @Override public void setGoodsPrice(String id) { int res = 0; String sql = "update goods set price =? where id=?"; try { conn = DataSourPool.getConnection();//Get connection to database ps = conn.prepareStatement(sql); Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "Price" + goods.getPrice() + "element"); System.out.println("Price to modify:"); int priceNew = scan.nextInt(); ps.setInt(1, priceNew); ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Modified successfully"); System.out.println("commodity " + goods.getName() + "Price" + priceNew + "element"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(ps); DataSourPool.close(conn); } } @Override public void deleteGoods(String id) { int res = 0; String sql = "delete from goods where id=?"; try { conn = DataSourPool.getConnection();//Get connection to database ps = conn.prepareStatement(sql); Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println(goods); System.out.println("Delete input 1 confirm other keys skip enter confirmation"); if (scan.nextInt() == 1) { ps.setString(1, id); res = ps.executeUpdate(); System.out.println("Deleted successfully"); System.out.println("Number of modified rows: " + res + "that 's ok"); }else { System.out.println("Delete failed"); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(ps); DataSourPool.close(conn); } } }
Each function is detailed below
Product addition
The method of Util class above is used
public void addGoods(Goods goods) { int res = 0;//Record the number of rows in the modified database /* Judge whether the goods already exist */ List<Goods> list = Util.AllGoods();//Get all items for (Goods goods1 : list) { if(goods1.getId().equals(goods.getId())){ try { throw new ExistExcetion("Goods already exist");//Custom exception class } catch (ExistExcetion existExcetion) { existExcetion.printStackTrace(); } } } String sql = " insert into goods(id,name,price,number) values(?,?,?,?)"; Connection conn = DataSourPool.getConnection();//Get connection to database PreparedStatement ps = null;// Create PreparedStatement interface object try { ps = conn.prepareStatement(sql); //Gets the PreparedStatement interface object /* Replace placeholders in sql */ ps.setString(1, goods.getId()); ps.setString(2, goods.getName()); ps.setInt(3, goods.getPrice()); ps.setInt(4, goods.getNumber()); res = ps.executeUpdate();//Execute sql statement } catch (SQLException throwables) { throwables.printStackTrace(); } finally {//close resource DataSourPool.close(conn); DataSourPool.close(ps); } System.out.println("Added successfully"); System.out.println("Add rows: " + res + "that 's ok"); }
Commodity display
@Override public void showGoods() { List<Goods> list = Util.AllGoods(); System.out.println("Item number\t Trade name\t commodity price\t\t Quantity of goods"); for (Goods goods : list) { System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t\t" + goods.getNumber()); } }
Goods warehousing
public void inGoods(String id) { int res = 0; String sql = "update goods set number =? where id=?"; try { Connection conn = DataSourPool.getConnection();//Get database connection PreparedStatement ps = conn.prepareStatement(sql); //Gets the PreparedStatement interface object Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "also" + goods.getNumber() + "individual"); /* Placeholder replacement */ System.out.println("Enter the quantity to be warehoused:"); int number = scan.nextInt(); number = number + goods.getNumber(); ps.setInt(1, number); ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Warehousing succeeded"); System.out.println("commodity " + goods.getName() + "also" + number + "individual"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(conn); DataSourPool.close(ps); } }
Commodity ex warehouse
public void outGoods(String id) { int res = 0; String sql = "update goods set number =? where id=?"; try { Connection conn = DataSourPool.getConnection();//Get database connection PreparedStatement ps = conn.prepareStatement(sql); //Gets the PreparedStatement interface object Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "also" + goods.getNumber() + "individual"); /* Replace placeholder */ System.out.println("Enter the quantity you want to issue:"); int numberOut = scan.nextInt(); int numberNow = goods.getNumber();//Existing quantity if (numberNow > numberOut) { ps.setInt(1, numberNow - numberOut); } else { try { throw new NotNumberException("Insufficient inventory"); } catch (NotNumberException e) { e.printStackTrace(); } } ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Delivery succeeded"); System.out.println("commodity " + goods.getName() + "also" + (numberNow - numberOut) + "individual"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(conn); DataSourPool.close(ps); } }
Commodity modification price
public void setGoodsPrice(String id) { int res = 0; String sql = "update goods set price =? where id=?"; try { Connection conn = DataSourPool.getConnection();//Get connection to database PreparedStatement ps = conn.prepareStatement(sql);//Get PreparedStatement object interface Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println("commodity " + goods.getName() + "Price" + goods.getPrice() + "element"); System.out.println("Price to modify:"); int priceNew = scan.nextInt(); ps.setInt(1, priceNew); ps.setString(2, id); res = ps.executeUpdate(); System.out.println("Modified successfully"); System.out.println("commodity " + goods.getName() + "Price" + priceNew + "element"); System.out.println("Number of modified rows: " + res + "that 's ok"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(ps); DataSourPool.close(conn); } }
Product deletion
public void deleteGoods(String id) { int res = 0; String sql = "delete from goods where id=?"; try { Connection conn = DataSourPool.getConnection();//Get connection to database PreparedStatement ps = conn.prepareStatement(sql);//Gets the PreparedStatement interface object Goods goods = Util.findIDOutGoods(id);//Item corresponding to id System.out.println(goods); System.out.println("Whether to delete input 1 and confirm other keys to skip enter confirmation"); if (scan.nextInt() == 1) { ps.setString(1, id); res = ps.executeUpdate(); System.out.println("Deleted successfully"); System.out.println("Number of modified rows: " + res + "that 's ok"); }else { System.out.println("Delete failed"); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { DataSourPool.close(ps); DataSourPool.close(conn); } }
Demo class test class
import com.sjtest.dao.imp.GoodsDaoImp; import com.sjtest.pojo.Goods; import com.sjtest.util.Util; import java.util.Scanner; /** * Project requirements: * 1.Able to design reasonable entity classes according to project requirements * 2.Standard coding * 3.Reasonable selection of collection type * 4.After closing the program, the entered data will still exist when the program is opened next time * Commodity addition (enter commodity code, name, price and quantity) * Product list display * Commodity warehousing (enter the commodity number, and then prompt the user to enter the warehousing quantity for warehousing) * Commodity delivery (enter the commodity number and prompt the user to enter the delivery quantity for delivery) * Modify the commodity price (prompt the user to enter the number and enter the new price). * Delete item */ public class Demo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); GoodsDaoImp goodsManager = new GoodsDaoImp(); while (true) { System.out.println("-------------------"); System.out.println("Welcome to the backstage of commodity management"); System.out.println("Select your action:"); System.out.println("1.Product addition"); System.out.println("2.Product list display"); System.out.println("3.Goods warehousing "); System.out.println("4.Commodity ex warehouse"); System.out.println("5.Modify commodity price"); System.out.println("6.Delete item"); System.out.println("7.sign out"); System.out.println("-------------------"); switch (scan.nextInt()) { case 1: System.out.println("Enter the commodity code, name, price and quantity in sequence"); Goods goods = new Goods(scan.next(), scan.next(), scan.nextInt(), scan.nextInt()); goodsManager.addGoods(goods); break; case 2: goodsManager.showGoods(); break; case 3: //Get item number Util.showIDAndName(); System.out.println("Please enter the item number:"); goodsManager.inGoods(scan.next()); break; case 4: //Get item number Util.showIDAndName(); System.out.println("Please enter the item number:"); goodsManager.outGoods(scan.next()); break; case 5: //Get item number Util.showIDAndName(); System.out.println("Please enter the item number:"); goodsManager.setGoodsPrice(scan.next()); break; case 6: //Get item number Util.showIDAndName(); System.out.println("Please enter the item number:"); goodsManager.deleteGoods(scan.next()); case 7: System.exit(0); break; default: System.out.println("Input error"); break; } } } }
This is the end of the project. If there are better suggestions, please comment and correct.
se 4:
//Get item number
Util.showIDAndName();
System.out.println("please enter the product number:");
goodsManager.outGoods(scan.next());
break;
case 5:
//Get item number
Util.showIDAndName();
System.out.println("please enter the product number:");
goodsManager.setGoodsPrice(scan.next());
break;
case 6:
//Get item number
Util.showIDAndName();
System.out.println("please enter the product number:");
goodsManager.deleteGoods(scan.next());
case 7:
System.exit(0);
break;
default:
System.out.println("input error");
break;
}
}
}
}
This is the end of the project. If there are better suggestions, please comment and correct. Source code[click here ](https://download.csdn.net/download/m0_55916987/84207084)