根据自己的需求创建自己所需要的数据库,建议使用IDEA自带的数据库,然后实现检验。
将Mysql的驱动放到文件中的Lib路径,如果没有lib文件路径,则需要创建文件路径。
public class DBUtils {
//数据库名
public static String db_url = "jdbc:mysql://localhost:3306/supermanager?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
public static String db_user = "root"; //用户名
public static String db_password = "zyx123456"; //密码
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
conn = DriverManager.getConnection(db_url, db_user, db_password);//获取连接对象
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param rs
* @param state
* @param conn
*/
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
其中重要的部分为
jdbc:mysql://localhost:3306/supermanager?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
package com.NAME;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @ClassName until.java
* Created by sky.
* Date:2021/11/16.
* Time:15:44.
* Description:
* PackgageName:IntelliJ IDEA.
*/
public class Until {
public static String url = "jdbc:mysql://localhost:3306/javashopping?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
public static String user = "root"; //用户名
public static String password = "root"; //密码
public static Connection getconnection(){
Connection connection = null;
/**
* 异常处理
* @des:
*/
try{
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
connection = DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
}
return connection;
}
public static void close(ResultSet resultSet,Statement statement,Connection connection){
if(resultSet != null){
/**
* 异常处理
* @des:判断是否为空
*/
try{
resultSet.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(statement != null){
/**
* 异常处理
* @des:
*/
try{
statement.close();
}catch(Exception e){
e.printStackTrace();
}
}
if (connection != null){
/**
* 异常处理
* @des:
*/
try{
connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
包括管理员的登录注册操作,以及货物的相关操作。
package com.NAME;
import com.NAME.*;
import java.sql.*;
import java.util.Scanner;
/**
* @ClassName CRUD.java
* Created by sky.
* Date:2021/11/16.
* Time:15:45.
* Description:
* PackgageName:IntelliJ IDEA.
*/
public class CRUD {
//这是一个main方法,是程序的入口
public static void main(String[] args) {
menu();
}
/**
**空格隔开
*/
/* public static void clear() {
for(int i=0;i<20;i++)
System.out.println();
}*/
/**
**主菜单
**@pagarm
*/
public static void menu() {
System.out.println(" ***************超市管理系统****************");
System.out.println(" * 1. 登录 *");
System.out.println(" * 2. 注册 *");
System.out.println(" * 0. 退出 *");
System.out.println(" *********************************************");
System.out.println("请选择您需要的功能:");
int mid;
String pname, classes;
Scanner scanner = new Scanner(System.in);
int next = scanner.nextInt();
try {
switch (next) {
case 1:
load();
break;
case 2:
enroll();
break;
case 3:
System.out.println("程序结束!");
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void enroll() throws SQLException {
System.out.println("欢迎进入注册界面");
System.out.println("请输入注册名称");
//原因中间出现延迟所以同时输出
Connection connection = Until.getconnection();//注册驱动、获取连接
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
String sql = "select * from admin where name = ?";
preparedStatement = connection.prepareStatement(sql);
//SQl语句中的?表示的是占位符,如果输入的数据是String类型的数据,则在SQl语句中直接默认的是’字符串‘
//为占位符中的位置赋值
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
System.out.println("注册名称重复,请重新选择注册名称");
enroll();
}else {
resultSet = null;
System.out.println("请输入注册密码");
String password = scanner.nextLine();
String sql1 = "insert into admin(name,pass)values(?,?)";
preparedStatement = connection.prepareStatement(sql1);
//SQl语句中的?表示的是占位符,如果输入的数据是String类型的数据,则在SQl语句中直接默认的是’字符串‘
//为占位符中的位置赋值
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
int count = preparedStatement.executeUpdate();
if (count>0){
System.out.println("注册成功");
menu();
}else{
System.out.println("注册失败,请重新注册");
menu();
}
connection.close();
resultSet.close();
preparedStatement.close();
}
}
private static void load() throws SQLException {
System.out.println("************************欢迎进入登录界面********************");
System.out.println("请输入登录账号:");
Scanner scanner = new Scanner(System.in);
String use1 = scanner.nextLine();
System.out.println("请输入登录密码:");
String pass1 = scanner.nextLine();
Connection connection = Until.getconnection();//注册驱动,并且获取连接
// 获取预处理数据库操作对象
String sql = "select * from admin where name = ? and pass = ?";
PreparedStatement ps = null;
ps = connection.prepareStatement(sql);
//SQl语句中的?表示的是占位符,如果输入的数据是String类型的数据,则在SQl语句中直接默认的是’字符串‘
//为占位符中的位置赋值
ps.setString(1,use1);
ps.setString(2,pass1);
ResultSet res = ps.executeQuery();
if(res.next()){
System.out.println("登录成功,欢迎进入到超市操作系统");
dispaly();
}else {
System.out.println("登陆失败,请重新登录或者注册账号");
menu();
}
ps.close();
connection.close();
res.close();
}
private static void dispaly() {
System.out.println("*********************货物管理功能**************************");
System.out.println("| 1. 货物添加 |");
System.out.println("| 2. 货物修改 |");
System.out.println("| 3. 货物删除 |");
System.out.println("| 4. 货物查找 |");
System.out.println("| 5. 货物总览 |");
System.out.println("| 0. 退出功能 |");
System.out.println("********************请输入需要执行的功能********************************");
System.out.println("选择如下:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
/**
* 异常处理
* @des:
*/
try{
switch (choice){
case 1:
addgoods();
dispaly();
break;
case 2:
modifygoods();
dispaly();
break;
case 3:
delgoods();
dispaly();
break;
case 4:
findgoods();
dispaly();
break;
case 5:
showgoods();
break;
case 6:
return;
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void showgoods()throws Exception {
System.out.println("下面是商品的总览结果");
//实现连接操作
Connection connection = Until.getconnection();
//获取预处理对象
PreparedStatement preparedStatement = null;
//结果集处理
ResultSet resultSet = null;
String sql = "select * from goods";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int goods_id = resultSet.getInt("goods_id");
String name = resultSet.getString("name");
String style = resultSet.getString("style");
int number = resultSet.getInt("number");
double price = resultSet.getDouble("price");
String comments = resultSet.getString("comments");
System.out.println("货物ID:"+goods_id +" 货物名称"+ name+" 货物类型"+style+" 货物数量"+number+" 货物价格"+price+" 货物评论"+comments);
}
dispaly();
}
private static void findgoods()throws Exception {
System.out.println("欢迎进入到商品查找界面");
System.out.println("通过姓名或者模糊商品名称进行查找");
Scanner scanner = new Scanner(System.in);
String name1 = scanner.nextLine();
//实现连接操作
Connection connection = Until.getconnection();
//获取预处理对象
PreparedStatement preparedStatement = null;
//结果集处理
ResultSet resultSet = null;
String sql = "select * from goods where name Like '%"+name1+"%'";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int goods_id = resultSet.getInt("goods_id");
String name = resultSet.getString("name");
String style = resultSet.getString("style");
int number = resultSet.getInt("number");
double price = resultSet.getDouble("price");
String comments = resultSet.getString("comments");
System.out.println("货物ID:"+goods_id +" 货物名称"+ name+" 货物类型"+style+" 货物数量"+number+" 货物价格"+price+" 货物评论"+comments);
}
}
private static void delgoods()throws Exception {
System.out.println("进入删除功能");
System.out.println("输入要删除的商品名称");
Scanner scanner = new Scanner(System.in);
String name1 = scanner.nextLine();
int count = 0;
//预处理对象创建
PreparedStatement preparedStatement = null;
//实现连接操作
Connection connection = Until.getconnection();
Statement stmt = connection.createStatement();
String sql="delete from goods where name='"+name1+"'";
try {
count = stmt.executeUpdate(sql);
}catch (Exception e){
e.printStackTrace();
}
System.out.println(sql);
System.out.println(count);
if(count>0){
System.out.println("删除成功!!!!!");
showgoods();
}else{
System.out.println("删除失败,重新删除");
dispaly();
}
//释放关闭资源
stmt.close();
connection.close();
preparedStatement.close();
}
private static void modifygoods()throws Exception {
System.out.println("哈哈哈哈");
//可以先进行总览然后在进行修改操作,可以对表中的主键进行操作。
}
private static void addgoods()throws Exception {
System.out.println("欢迎添加商品货物");
//使用preparedStatement对象进行预处理操作,使用?占位符进行操作,后续操作直接省略
}
}