Professional Documents
Culture Documents
Programmer Help
Programmer Help
Programmer Help
I. overview
1. Peng GE has written java project supermarket management system project in front of him, Portal
2. received many friends' personal letters to me, and I also appreciated the feedback and
communication from the old fellow. The previous project was just a basic knowledge and object-
oriented thinking exercise for java, but it didn't involve how java manipulate the database, how to get
data out of the database through java, and save these problems. For java novices, they don't know
where to start. There are a lot of new java novices suggest that I write a practical project of java
operation database for the reference of the entrants. I think if I can help them spend less time out of the
pit, why not? Peng Ge took the exam for a day, sorted out his ideas, and today took the time to write a
supermarket shopping management system..... go go go
3. Knowledge involved in the supermarket shopping management system: java basic syntax, java object-
oriented development ideas, how Java operates the database, knowledge of collection
4. Suitable for java beginners, do not know how Java operates the database
6. Because it is a practical project of java operating database, it will not explain the knowledge of java
environment construction, database installation and sql syntax in detail. Too much. I can't finish it in a
week... Or I can go to pengge station java 1 View related articles. So before you start, you should install
the java development environment, the database
7. The development tool eclipse; jdk is 1.8; the database uses mysql5.5 (of course, the SQL Server
database is OK)
8. source code acquisition: I will paste out the core code, all code attention will be given to Peng Peng
public number java actual combat project.
1. Goods warehousing
3. Commodity list
4. Purchase of goods
5. Delete goods
6. Update goods
1. Goods warehousing
3. Commodity list
4. Purchase of goods
5. Delete goods
0. Exit the system
1235
1234
Purchase success
1236
1. Goods warehousing
3. Commodity list
4. Purchase of goods
5. Delete goods
4564
1. Goods warehousing
3. Commodity list
4. Purchase of goods
5. Delete goods
1. Goods warehousing
2. Query commodities according to commodity number
3. Commodity list
4. Purchase of goods
5. Delete goods
1234
Purchase success
1234
Purchase success
Next, we introduce the database driven jar package, which can be downloaded from the official website
or can be obtained from my public address java reply jdbc.
What does java have to do with this database driver? The simple understanding is that this database
driver is the bridge between Java and database, which is used for communication between them.
It's very important, very important, very important to create the project structure. These are the things
that we should pay attention to in development. For these small projects, we can put all the code classes
under src. But later, the project is getting larger and larger. There are hundreds of all classes in src.
Without subcontracting, other developers of the project can see how much damage the code bears.
This system is relatively simple. The following four packages can be used. The app package is used to put
the main entry class of the system, the entity class involved in the pojo system, the service system
business, the tool class in the utils system, and the test temporary test class
Because the system will involve multiple access to the data in the database, we will write a tool class
DbUtil for the operation of connecting the database, and do not write repeated connection codes every
time it involves the operation of the database.
package com.javayihao.top.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @date 2019-12-9
* @author com.javayihao.top
*/
//Several strings used to connect to the database are defined as constants, which need not be
created every time
//drive
//Registration driven
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//Get the function of database Connection object Connection
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
test
package com.javayihao.top.test;
import java.sql.Connection;
import java.sql.SQLException;
import com.javayihao.top.utils.DbUtil;
/**
* @date 2019-12-9
* @author com.javayihao.top
*/
System.out.println(con);
package com.javayihao.top.pojo;
/**
* @date 2019-12-9
* @author com.javayihao.top
*/
//Commodity number
//Trade name
//Commodity price (the price may involve a small number, and float is used here. Of course, the
real large shopping platform will not use float, and interested friends can learn about it online)
//Stock
public Good() {
super();
//Printing method
@Override
return "Good [id=" + id + ", name=" + name + ", price=" + price + ", num=" + num + "]";
this.id = id;
this.name = name;
this.price = price;
this.num = num;
return id;
this.id = id;
return name;
this.name = name;
return price;
this.price = price;
}
public int getNum() {
return num;
this.num = num;
Six: Database
package com.javayihao.top.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import com.javayihao.top.pojo.Good;
import com.javayihao.top.utils.DbUtil;
/**
* @date 2019-12-9
* @author com.javayihao.top
*/
/*
*/
String isGo="y";
do{
showMenu();
switch (select) {
case "1":
insertGood();
break;
case "2":
//existence
if(good!=null){
System.out.println("Commodity number:"+goodId+"
Trade name:"+good.getName()
}else{
break;
case "3":
getGoodList();
break;
case "4":
buyGood();
break;
case "5":
int id = input.nextInt();
if(searchGoodById(id)!=null){
deleteGood(id);
}else{
break;
case "6":
updateGood();
break;
//Exit system
case "0":
//Termination procedure
System.exit(0);
default:
continue;
isGo = input.next();
}while(isGo.equals("y"));
/**
* 1.First query whether the current product inventory to be updated does not exist
*/
if(good!=null){
try {
pst.setString(1, name);
pst.setFloat(2, price);
pst.setInt(3, num);
pst.setInt(4, gid);
if(!pst.execute()){
System.out.println("Update success");
//Close connection
DbUtil.close(con, pst);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Update exception"+e.getMessage());
}else{
System.out.println("This product does not exist");
System.out.println("1.Goods warehousing");
System.out.println("3.List of commodities");
System.out.println("4.Purchase goods");
System.out.println("5.Delete merchandise");
System.out.println("6.Update commodity");
System.out.println("0.Exit system");
/*
* Delete merchandise
*/
try {
pst.setInt(1, id);
if(!pst.execute()){
System.out.println("Delete successful");
//Close connection
DbUtil.close(con, pst);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Delete exception"+e.getMessage());
/*
* Goods warehousing
* Here we only deal with the logic that the number cannot be repeated,
* As for whether the input number is a number, there is no judgment here. Interested friends
can try it
*/
//Commodity number
int id=0;
while(true){
id= input.nextInt();
//Judge whether the number entered is duplicate or not, and re-enter it
repeatedly
if(searchGoodById(id)==null){
break;
//To execute the sql statement, use placeholders here to prevent sql intrusion
try {
pst.setInt(1, id);
pst.setString(2, name);
pst.setFloat(3, price);
pst.setInt(4, num);
System.out.println("Warehousing success");
//Close connection
DbUtil.close(con, pst);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Abnormal storage"+e.getMessage());
/*Commodity inquiry
* The returned object is a product object. If there is no such product, null will be returned
*/
try {
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
return good;
//Close connection
DbUtil.close(con, pst);
} catch (SQLException e) {
e.printStackTrace();
return null;
//List of commodities
try {
ResultSet rs = pst.executeQuery();
System.out.println("number\t"+"Name\t"+"Unit
Price\t"+"Number\t");
if(rs.wasNull()){
System.out.println("No goods");
}else{
while(rs.next()){//Results, printing
//Through rs.getxxx("yy") method parameter is
the database column name
System.out.println(rs.getInt("id")
+"\t"+rs.getString("name")+"\t"+
rs.getFloat("price")
+"\t"+rs.getInt("num")+"\t");
//Close connection
DbUtil.close(con, pst);
} catch (SQLException e) {
e.printStackTrace();
//Purchase goods
do{
int id = input.nextInt();
if(good!=null){
}else{
try{
pst.setInt(2, id);
if(pst.executeUpdate()==1){
Good g = new
Good(id,good.getName(),good.getPrice(),num);
if(goods.size()>0){
if(goods.get(i).getId()==id){//If
the shopping cart has the product quantity plus
goods.get(0).setNum(num+goods.get(0).getNum());
goods.add(g);
}
}
goods.add(g);
System.out.println("Purchase success");
}else{
System.out.println("Purchase failure");
}catch(Exception e){
e.printStackTrace();
System.out.println("Purchase
exception"+e.getMessage());
flag = input.next();
if(!flag.equals("y")){
//Settlement
account(goods);
}else{
}while(flag.equals("y"));
}
//Checkout cart
System.out.println("number\t"+"Name\t"+"Number\t"+"Total price");
goods.forEach(in->System.out.println(in.getId()+"\t"+in.getName()+
"\t"+in.getNum()+"\t"+in.getNum()*in.getPrice()));
//Total
float sum=0;
sum += (goods.get(i).getNum())*(goods.get(i).getPrice());
System.out.println("Aggregate consumption:"+sum+"element");
summary
So far, the project has been completed, of course, there are still shortcomings in the project itself, for
example, in accepting the legitimacy judgment of keyboard input. For java operation database, you can
refer to,
I put the source code in the public number java No. 1, I need to mention it.
Hot Tags
Java - 6272
Attribute - 2963
Programming - 2894
Database - 2805
Python - 2462
Spring - 2314
Javascript - 2287
xml - 2256
JSON - 2139
less - 2131
network - 2064
Android - 2061
github - 2047
Linux - 1810
MySQL - 1546
PHP - 1510
SQL - 1489
encoding - 1360
Mobile - 1172
Apache - 1114