アイデアデータベースを使用してエクスプレスeステーションを作成する

データベースを使用してエクスプレスeステーションを作成します(この記事ではアイデアのコードのみを作成します)


エクスプレス業界の継続的な発展に伴い、エクスプレスeステーションが増えています。簡単なエクスプレスeステーションアップルレットを書いてみましょう。この記事では、データベースの知識を使用してエクスプレスeステーションを作成する方法を紹介します。

##完成品は次のとおりです。ここに写真の説明を挿入


1.IDEAはどのようにデータベースに接続しますか

最初の方法:接続情報をメソッド本体に直接追加します。
長所:1つのデータベース操作のみを使用する場合は、選択できます。
短所:複数のデータベース操作を毎回書き込む必要があり、面倒です。

public void select() {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.定义sql
            String sql = "select * from kuaidi";
            //3.获取conn
            conn = DriverManager.getConnection("jdbc:mysql:///kuaidi", "root", "123");
            //4.获取执行sql对象Statement
            stmt = conn.createStatement();
            //5.执行sql
            rs = stmt.executeQuery(sql);
            //6处理结果
            while (rs.next()) {
                int danhao = rs.getInt(1);
                int qujianma = rs.getInt(2);
                String gongsi = rs.getString("gongsi");
                String guizi = rs.getString(4);
                System.out.println("单号为:" + danhao + "   " + "取件码为:" + qujianma + "   " + "公司是:" + gongsi + "    " + "柜子在第" + guizi + "个");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        }
    }

方法2:
JDBCHelperとプロパティを作成してデータベースアカウントとパスワードを保存し、ドライバーをすばやくロードしてメモリを解放できるようにします。
長所:1回だけ書き込む必要があり、使用時に呼び出す必要があります。
短所:一度に大量に書き込む
ここに写真の説明を挿入

メモリを解放するとき、2つまたは3つのパラメータが渡され、解放する必要がある場合があるため、オーバーロードされた形式を使用して解決します


    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     * 文件的读取,只需要读取一次即可
     */
    static {
        //读取资源文件,并获取值
        try {
            //1.创建properties集合类
            Properties pro = new Properties();
            //2.加载文件
            //获取src路径下的文件的方式--->ClassLoader类加载器
            ClassLoader classLoader = JDBCHelper.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            
            //pro.load(new FileReader("src/jdbc.properties"));
            pro.load(new FileReader(path));

            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");

            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取连接
     *
     * @return连接对象
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
    /**
     * 释放资源
     *
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
//释放内存
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }

第二に、メソッドコードの実現

1.クーリエが速達を増やす

コードは次のように表示されます。

public class Add {
    
    
    Connection conn = null;
    Statement stmt = null;
    Random r= new Random();
    public void addq(int danhao,String gongsi){
    
    
        try {
    
    
            conn = JDBCHelper.getConnection();
            int qujianma = r.nextInt((999999)+1);
            String guizi = Integer.toString(r.nextInt(100)+1);
            String sql = "insert into kuaidi values(" + danhao + "," + qujianma+",'"+gongsi+"','"+guizi+"')";
            stmt = conn.createStatement();
            int a = stmt.executeUpdate(sql);
            System.out.println("取件码为:"+qujianma);
            if(a>0){
    
    
                System.out.println("添加成功");
            }
            else {
    
    
                System.out.println("添加失败");
            }
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }
    }
    }

2.宅配便業者が宅配便業者を削除します

コードは次のように表示されます。

public class Delete {
    
    

    Connection conn = null;
    Statement stmt = null;

    public void delete(int danhao) {
    
    

        try {
    
    
            conn = JDBCHelper.getConnection();
            String sql = "delete from kuaidi where danhao = "+danhao;

            stmt = conn.createStatement();
            int a = stmt.executeUpdate(sql);
            if(a>0){
    
    
                System.out.println("删除成功");
            }
            else {
    
    
                System.out.println("删除失败");
            }

        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }
    finally {
    
    
        JDBCHelper.close(stmt,conn);
    }
}

メインコンテンツコード:

public class Imp { public static void main(String [] args){ kuaidi(); }


public static void kuaidi() {
    System.out.println("====欢迎使用新职课快递柜====");
    Scanner sc = new Scanner(System.in);
    Random r = new Random();
    while (true) {

        System.out.println("请输入你的身份:1-快递员,2-用户");
        try {
            int a = sc.nextInt();

            if (a < 1 || a > 2) {
                System.out.println("输入有误,请重新输入");
            } else if (a == 1) {
                System.out.println("====欢迎使用新职课快递柜====");
                System.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
                int b = sc.nextInt();
                switch (b) {
                    case 1: {
                        System.out.println("====欢迎使用新职课快递柜====");
                        System.out.println("请输入快递单号:");
                        int danhao = sc.nextInt();
                        System.out.println("请输入公司名称:");
                        String gongsi = sc.next();
                        Add add = new Add();
                        add.addq(danhao, gongsi);

                        break;
                    }
                    case 2: {
                        System.out.println("====欢迎使用新职课快递柜====");
                        System.out.print("请输入要删除的订单号:");
                        int dingdan = sc.nextInt();
                        Delete delete = new Delete();
                        delete.delete(dingdan);
                        break;
                    }
                    case 3: {
                        System.out.println("====欢迎使用新职课快递柜====");
                        System.out.print("请输入要修改的订单号:");
                        int danhao = sc.nextInt();
                        Alter al = new Alter();
                        boolean q = al.select(danhao);
                        if (q = true) {
                            System.out.println("请输入更改后的单号");
                            int afdanhao = sc.nextInt();
                            al.alter(afdanhao, danhao);
                        } else {
                            System.out.println("请核实订单号");
                        }

                        break;
                    }
                    case 4: {
                        System.out.println("====欢迎使用新职课快递柜====");
                        SelectAll s = new SelectAll();
                        s.select();
                    }
                }

            } else if (a == 2) {
                System.out.println("====欢迎使用新职课快递柜====");
                System.out.println("请输入取件码");
                int qujianma = sc.nextInt();
                Take t = new Take();
                t.take(qujianma);

            }


        } catch (InputMismatchException e) {
            System.out.println();
            System.out.println("请输入数字序号!!!!!!!!");
            System.out.println();
            kuaidi();
        }
    }

}

}


他のいくつかのコードブロックも同様ですが、sqlステートメントを変更するだけで済みます

すべてのコードが必要な場合、または質問がある場合は、プライベートメッセージを送信できます

おすすめ

転載: blog.csdn.net/weixin_48305172/article/details/112304491