后端学习 Java 面向指挥官的操作系统

闲的没事写的小练习
场景:用以 碧蓝航线 指挥官管理船坞
内容非常简单,没有什么用,只是写着玩的
构成:MySQL数据库、测试类、对象

测试类

package com.cookies.jdbc;

import com.cookies.pojo.Worship;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class JDBCDemo9_Worship {
    public static void main(String[] args) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎来到船坞,请输入用户名与密码");
        String url = "jdbc:mysql:///db1?useSSL=false";
        System.out.print("用户名:");
        String username = sc.next();
        System.out.print("密码:");
        String password = sc.next();
        while (!((username.equals("root"))&&(password.equals("1234")))) {
            System.out.println("用户名或密码错误,请重新输入");
            System.out.print("用户名:");
            username = sc.next();
            System.out.print("密码:");
            password = sc.next();
        }
        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stmt = conn.createStatement();
        String sqlDropTable = "DROP TABLE IF EXISTS tb_worship";
        String sqlCreateTable = "CREATE TABLE tb_worship(id INT,name VARCHAR(20),camp VARCHAR(10),description VARCHAR(20))";
        stmt.executeUpdate(sqlDropTable);
        stmt.executeUpdate(sqlCreateTable);
        worshipManagementSystem(conn);
        conn.close();
    }

    private static void worshipManagementSystem(Connection conn) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("---------------------");
        System.out.println("---------------------");
        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("请输入下一步操作");
        int chooseNumber = sc.nextInt();
        switch (chooseNumber) {
            case 1 -> createWorship(conn);
            case 2 -> deleteWorship(conn);
            case 3 -> changeWorship(conn);
            case 4 -> selectWorship(conn);
            case 5 -> System.exit(0);
        }
    }

    private static void selectWorship(Connection conn) throws SQLException {
        String sql = "SELECT * FROM tb_worship";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        ArrayList<Worship> list = new ArrayList<>();
        while (rs.next()) {
            Worship worship = new Worship();
            worship.setId(rs.getInt("id"));
            worship.setName(rs.getString("name"));
            worship.setCamp(rs.getString("camp"));
            worship.setDescription(rs.getString("description"));
            list.add(worship);
        }
        for (int i = 0; i < list.size(); i++) {
            Worship worship = list.get(i);
            System.out.print("id:");
            System.out.println(worship.getId());
            System.out.print("name:");
            System.out.println(worship.getName());
            System.out.print("阵营:");
            System.out.println(worship.getCamp());
            System.out.print("中文名:");
            System.out.println(worship.getDescription());
        }
        rs.close();
        stmt.close();
        worshipManagementSystem(conn);
    }

    private static void changeWorship(Connection conn) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.print("希望修改的序号:");
        int changeNumber = sc.nextInt();
        System.out.print("id:");
        int id = sc.nextInt();
        System.out.print("name:");
        String name = sc.next();
        System.out.print("阵营:");
        String camp = sc.next();
        System.out.print("中文名:");
        String description = sc.next();
        String sql = "UPDATE tb_worship SET id = ?, name = ?, camp = ?, description = ? WHERE id = ?";
        PreparedStatement pStmt = conn.prepareStatement(sql);
        pStmt.setInt(1,id);
        pStmt.setString(2,name);
        pStmt.setString(3,camp);
        pStmt.setString(4,description);
        pStmt.setInt(5,changeNumber);
        int count = pStmt.executeUpdate();
        if (count > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        pStmt.close();
        worshipManagementSystem(conn);
    }

    private static void deleteWorship(Connection conn) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.print("希望删除的序号:");
        int deleteNumber = sc.nextInt();
        String sql = "DELETE FROM tb_worship WHERE id = ?";
        PreparedStatement pStmt = conn.prepareStatement(sql);
        pStmt.setInt(1,deleteNumber);
        int count = pStmt.executeUpdate();
        if (count > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        pStmt.close();
        worshipManagementSystem(conn);
    }

    private static void createWorship(Connection conn) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.print("id:");
        int id = sc.nextInt();
        System.out.print("name:");
        String name = sc.next();
        System.out.print("阵营:");
        String camp = sc.next();
        System.out.print("中文名:");
        String description = sc.next();
        // 输入SQL语句
        String sql = "INSERT INTO tb_worship VALUES (?,?,?,?)";
        PreparedStatement pStmt = conn.prepareStatement(sql);
        pStmt.setInt(1,id);
        pStmt.setString(2,name);
        pStmt.setString(3,camp);
        pStmt.setString(4,description);
        int count =  pStmt.executeUpdate();
        if (count > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        pStmt.close();
        worshipManagementSystem(conn);
    }
}

对象

package com.cookies.pojo;

public class Worship {
    private int id;
    private String name;
    private String camp;
    private String description;

    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 String getCamp() {
        return camp;
    }

    public void setCamp(String camp) {
        this.camp = camp;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值