停车管理程序(Java&Mysql)

发布于 2020-08-22  3890 次阅读


最近用Java和Mysql数据库写了一个停车管理程序,效果如图。
停车管理程序(Java&Mysql)
dYgd76.png
停车管理程序(Java&Mysql)
Java主要用到窗体,最外层需要一个JFrame框架,内层可以嵌套多种组件,如JLable、JButton等,所有内部组件的位置可以由setBounds函数来设定。
与Mysql的连接使用JDBC,主要代码如下,大部分已注释:

package trying;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.io.*;
import javax.imageio.*;
public class trying {

    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/cars?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    static final String USER = "root";
    static final String PASS = "";
    //数据库配置
    static int loginflag=0;
    //登录标记
    static String find;
    //查询值

    public static void main(String[] args){
        JFrame car=new JFrame("汽车管理系统 黄文略");
        car.setSize(1073, 821);
        car.setLocation(400, 150);
        car.setLayout(null);
        car.setResizable(false);
        //构建框架并设定

        JPanel inputp=new JPanel();
        inputp.setBounds(0, 700, 900, 30);
        inputp.setLayout(new GridLayout(1,2,10,10));
        car.add(inputp);
        //用户名密码版块

        JPanel login=new JPanel();
        login.setBounds(490, 730, 90, 30);
        login.setLayout(new GridLayout(1,1,10,10));
        JButton log=new JButton("登录");
        login.add(log);
        car.add(login);
        //登录按钮版块

        BufferedImage img=null;
        try {
            img=ImageIO.read(new File("D:/IE下载/1.jpg"));
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }//读取首页图片

        JLabel pic=new JLabel(new ImageIcon(img));
        car.add(pic);
        pic.setBounds(0, 0, img.getWidth(), img.getHeight());
        //首页图片版块

        JLabel user=new JLabel("用户名:",JLabel.RIGHT);
        JTextField username=new JTextField();
        inputp.add(user);
        inputp.add(username);
        //用户名标签及其文本域

        JLabel key=new JLabel("密码:",JLabel.RIGHT);
        JTextField keyText=new JTextField();
        inputp.add(key);
        inputp.add(keyText);
        //密码标签及其文本域

        car.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        car.setVisible(true);
        //显示框架

        log.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                String a=username.getText();
                String b=keyText.getText();
                loginos(a,b);
                if(loginflag==1) {
                car.dispose();
                //关闭上一个窗口

                JFrame caros=new JFrame("汽车管理系统 黄文略");
                caros.setSize(1073, 821);
                caros.setLocation(400, 150);
                caros.setLayout(null);
                caros.setResizable(false);
                //构建新操作框架

                JMenuBar bar=new JMenuBar();
                bar.setBounds(0, 0, 40, 20);
                JMenu mset=new JMenu("选项");
                mset.setBounds(0, 0, 20, 15);
                JMenuItem mchange=new JMenuItem("修改用户名和密码");
                JMenuItem madd=new JMenuItem("添加用户");
                JMenuItem mopen=new JMenuItem("更换当前数据库");
                caros.add(bar);
                bar.add(mset);
                mset.add(mchange);mset.add(madd);mset.add(mopen);
                //添加菜单栏

                mchange.addActionListener(new ActionListener() {
                    @SuppressWarnings("resource")
                    public void actionPerformed(ActionEvent e){
                        String oldpassword=JOptionPane.showInputDialog("请输入密码");
                        Connection conn = null;
                        Statement stmt = null;
                        try{
                            // 注册 JDBC 驱动
                            Class.forName(JDBC_DRIVER);

                            // 打开链接
                            System.out.println("连接数据库...");
                            conn = DriverManager.getConnection(DB_URL,USER,PASS);

                            // 执行查询
                            System.out.println(" 实例化Statement对象...");
                            stmt = conn.createStatement();
                            String sql;
                            sql = "SELECT ID, USERNAME, PASSWORD FROM logindb";
                            ResultSet rs = stmt.executeQuery(sql);

                            // 展开结果集数据库
                            while(rs.next()){
                                // 通过字段检索
                                int id= rs.getInt("id");
                                String name = rs.getString("USERNAME");
                                String password = rs.getString("PASSWORD");
                                if(oldpassword.equals(password)) {
                                    String newname=JOptionPane.showInputDialog("请输入新用户名");
                                    String newpassword=JOptionPane.showInputDialog("请输入新密码");
                                    sql="UPDATE logindb SET USERNAME="+"\""+newname+"\""+" WHERE id="+id;
                                    stmt.executeUpdate(sql);
                                    sql="UPDATE logindb SET PASSWORD="+"\""+newpassword+"\""+" WHERE id="+id;
                                    stmt.executeUpdate(sql);
                                    JOptionPane.showMessageDialog(null,"更改成功!");
                                }
                            }
                            // 完成后关闭
                            rs.close();
                            stmt.close();
                            conn.close();
                        }catch(SQLException se){
                            // 处理 JDBC 错误
                            se.printStackTrace();
                        }catch(Exception e1){
                            // 处理 Class.forName 错误
                            e1.printStackTrace();
                        }finally{
                            // 关闭资源
                            try{
                                if(stmt!=null) stmt.close();
                            }catch(SQLException se2){
                            }
                            try{
                                if(conn!=null) conn.close();
                            }catch(SQLException se){
                                se.printStackTrace();
                            }
                        }
                    }
                });
                mopen.addActionListener(new ActionListener() {
                    @SuppressWarnings("resource")
                    public void actionPerformed(ActionEvent e){
                        String oldpassword=JOptionPane.showInputDialog("请输入密码");
                        Connection conn = null;
                        Statement stmt = null;
                        try{
                            // 注册 JDBC 驱动
                            Class.forName(JDBC_DRIVER);

                            // 打开链接
                            System.out.println("连接数据库...");
                            conn = DriverManager.getConnection(DB_URL,USER,PASS);

                            // 执行查询
                            System.out.println(" 实例化Statement对象...");
                            stmt = conn.createStatement();
                            String sql;
                            String newdb=JOptionPane.showInputDialog("请输入新数据库名");
                            sql = "use "+newdb;
                            ResultSet rs = stmt.executeQuery(sql);
                            JOptionPane.showMessageDialog(null,"操作成功!");

                            // 完成后关闭
                            rs.close();
                            stmt.close();
                            conn.close();
                        }catch(SQLException se){
                            // 处理 JDBC 错误
                            se.printStackTrace();
                        }catch(Exception e1){
                            // 处理 Class.forName 错误
                            e1.printStackTrace();
                        }finally{
                            // 关闭资源
                            try{
                                if(stmt!=null) stmt.close();
                            }catch(SQLException se2){
                            }
                            try{
                                if(conn!=null) conn.close();
                            }catch(SQLException se){
                                se.printStackTrace();
                            }
                        }
                    }
                });
                madd.addActionListener(new ActionListener() {
                    @SuppressWarnings("resource")
                    public void actionPerformed(ActionEvent e){
                        String oldpassword=JOptionPane.showInputDialog("请输入密码");
                        Connection conn = null;
                        Statement stmt = null;
                        try{
                            // 注册 JDBC 驱动
                            Class.forName(JDBC_DRIVER);

                            // 打开链接
                            System.out.println("连接数据库...");
                            conn = DriverManager.getConnection(DB_URL,USER,PASS);

                            // 执行查询
                            System.out.println(" 实例化Statement对象...");
                            stmt = conn.createStatement();
                            String sql;
                            sql = "SELECT ID, USERNAME, PASSWORD FROM logindb";
                            ResultSet rs = stmt.executeQuery(sql);

                            // 展开结果集数据库
                            while(rs.next()){
                                // 通过字段检索
                                int id= rs.getInt("id");
                                String name = rs.getString("USERNAME");
                                String password = rs.getString("PASSWORD");
                                if(oldpassword.equals(password)) {
                                    String newname=JOptionPane.showInputDialog("请输入添加的新用户名");
                                    String newpassword=JOptionPane.showInputDialog("请输入新用户的密码");
                                    sql="insert into logindb (USERNAME,PASSWORD) VALUES ("+"\""+newname+"\""+","+"\""+newpassword+"\""+")";
                                    stmt.executeUpdate(sql);
                                    JOptionPane.showMessageDialog(null,"操作成功!");
                                }
                            }
                            // 完成后关闭
                            rs.close();
                            stmt.close();
                            conn.close();
                        }catch(SQLException se){
                            // 处理 JDBC 错误
                            se.printStackTrace();
                        }catch(Exception e1){
                            // 处理 Class.forName 错误
                            e1.printStackTrace();
                        }finally{
                            // 关闭资源
                            try{
                                if(stmt!=null) stmt.close();
                            }catch(SQLException se2){
                            }
                            try{
                                if(conn!=null) conn.close();
                            }catch(SQLException se){
                                se.printStackTrace();
                            }
                        }
                    }
                });
                //菜单栏活动

                String[] columnNames = {"序号", "车牌号","车主姓名","车主电话","停放区域"};
                DefaultTableModel model = new DefaultTableModel(columnNames,0);
                JTable table = new JTable(model);
                table.setBounds(370, 170, 570, 521);
                JScrollPane tablecarry=new JScrollPane(table);
                tablecarry.setBounds(370, 170, 570, 521);
                caros.add(tablecarry);
                //车辆信息表格

                JButton allcar=new JButton("所有车辆");
                allcar.setBounds(70, 60, 180, 40);
                caros.add(allcar);
                allcar.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    model.setRowCount( 0 );
                    search(model,0);
                    }
                });
                //所有车辆切换按钮

                JButton alrdst=new JButton("已停放汽车");
                alrdst.setBounds(70, 210, 180, 40);
                caros.add(alrdst);
                alrdst.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    model.setRowCount( 0 );
                    search(model,1);
                    }
                });
                //已停放汽车切换按钮

                JButton ntst=new JButton("未停放汽车");
                ntst.setBounds(70, 360, 180, 40);
                caros.add(ntst);
                ntst.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    model.setRowCount( 0 );
                    search(model,2);
                    }
                });
                //未停放汽车切换按钮

                JButton alrdpy=new JButton("已缴费汽车");
                alrdpy.setBounds(70, 510, 180, 40);
                caros.add(alrdpy);
                alrdpy.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    model.setRowCount( 0 );
                    search(model,3);
                    }
                });
                //已缴费汽车切换按钮

                JButton ntpy=new JButton("未缴费汽车");
                ntpy.setBounds(70, 660, 180, 40);
                caros.add(ntpy);
                ntpy.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    model.setRowCount( 0 );
                    search(model,4);
                    }
                });
                //未缴费汽车切换按钮

                JTextField lookcar=new JTextField();
                lookcar.setBounds(370, 60, 500, 40);
                caros.add(lookcar);
                //查询文本框

                JButton lookbt=new JButton("查询汽车");
                lookbt.setBounds(920, 60, 100, 40);
                caros.add(lookbt);
                lookbt.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    find=new String(lookcar.getText());
                    model.setRowCount( 0 );
                    search(model,5);
                    }
                });
                //查询按钮

                caros.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                caros.setVisible(true);
                //显示操作框架
                }
            }
        });//登录事件

        } 

    protected static JScrollPane JScrollPane(JTable table) {
        // TODO 自动生成的方法存根
        return null;
    }

    public static void loginos(String a,String b) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT ID, USERNAME, PASSWORD FROM logindb";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("ID");
                String name = rs.getString("USERNAME");
                String password = rs.getString("PASSWORD");
                if(name.equals(a)&&password.equals(b)) loginflag=1;
            }
            if(loginflag==1) {
                JOptionPane.showMessageDialog(null, "登录成功");

            }//登录操作
            else {
                JOptionPane.showMessageDialog(null, "用户名或者密码错误!");
            }

            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }


        System.out.println("Goodbye!");
    }

    public static void search(DefaultTableModel model,int a) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, carnum, name, phone, setpos, set_status, pay_status FROM carsinfo";
            ResultSet rs = stmt.executeQuery(sql);
            // 展开结果集数据库
            switch(a) {
            case 1:
            while(rs.next()){
                // 通过字段检索
                Vector row=new Vector();
                int id= rs.getInt("id");
                String carnum = rs.getString("carnum");
                String name = rs.getString("name");
                String phone = rs.getString("phone");
                String setpos = rs.getString("setpos");
                int set_status = rs.getInt("set_status");
                int pay_status = rs.getInt("pay_status");
                if(set_status==1) {
                row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                model.addRow(row);
                }
            }
            break;
            case 2:
            while(rs.next()){
                // 通过字段检索
                Vector row=new Vector();
                int id= rs.getInt("id");
                String carnum = rs.getString("carnum");
                String name = rs.getString("name");
                String phone = rs.getString("phone");
                String setpos = rs.getString("setpos");
                int set_status = rs.getInt("set_status");
                int pay_status = rs.getInt("pay_status");
                if(set_status==0) {
                row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                model.addRow(row);
                }
                }
            break;
            case 3:
                while(rs.next()){
                    // 通过字段检索
                    Vector row=new Vector();
                    int id= rs.getInt("id");
                    String carnum = rs.getString("carnum");
                    String name = rs.getString("name");
                    String phone = rs.getString("phone");
                    String setpos = rs.getString("setpos");
                    int set_status = rs.getInt("set_status");
                    int pay_status = rs.getInt("pay_status");
                    if(pay_status==1) {
                    row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                    model.addRow(row);
                    }
                    }
                break;
            case 4:
                while(rs.next()){
                    // 通过字段检索
                    Vector row=new Vector();
                    int id= rs.getInt("id");
                    String carnum = rs.getString("carnum");
                    String name = rs.getString("name");
                    String phone = rs.getString("phone");
                    String setpos = rs.getString("setpos");
                    int set_status = rs.getInt("set_status");
                    int pay_status = rs.getInt("pay_status");
                    if(pay_status==0) {
                    row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                    model.addRow(row);
                    }
                    }
                break;
            case 5:
                while(rs.next()){
                    // 通过字段检索
                    Vector row=new Vector();
                    int id= rs.getInt("id");
                    String carnum = rs.getString("carnum");
                    String name = rs.getString("name");
                    String phone = rs.getString("phone");
                    String setpos = rs.getString("setpos");
                    int set_status = rs.getInt("set_status");
                    int pay_status = rs.getInt("pay_status");
                    if(carnum.equals(find)||name.equals(find)||phone.equals(find)||setpos.equals(find)) {
                    row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                    model.addRow(row);
                    }
                    }
                break;
                default:
                    while(rs.next()){
                        // 通过字段检索
                        Vector row=new Vector();
                        int id= rs.getInt("id");
                        String carnum = rs.getString("carnum");
                        String name = rs.getString("name");
                        String phone = rs.getString("phone");
                        String setpos = rs.getString("setpos");
                        int set_status = rs.getInt("set_status");
                        int pay_status = rs.getInt("pay_status");
                        row.add(id);row.add(carnum);row.add(name);row.add(phone);row.add(setpos);
                        model.addRow(row);
                        }
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    }

    }

华科菜鸡计科学生