javaSwing车辆租赁管理系统

发布时间:2023年12月28日

一、 引言

车辆租赁管理系统是一种便捷的解决方案,用于有效管理汽车租赁业务。本博客将介绍一个基于Java Swing的车辆租赁管理系统的开发,并展示相应的数据库设计。

二、 开发环境

Java Swing
MySQL数据库

三、 数据库设计

在MySQL数据库中,我们创建了四张表:car、rental、stores、user,分别用于存储车辆信息、租赁记录、门店信息和用户信息。

3.1 car表

car表用于存储车辆信息,包括车牌号、车型、数量、租金、颜色、是否可租、车辆信息详情和所属用户。

CREATE TABLE car (
carnum varchar(11) NOT NULL,
cartype varchar(25) DEFAULT NULL,
num varchar(3) DEFAULT NULL,
price double(20,0) DEFAULT NULL,
color varchar(10) DEFAULT NULL,
hire varchar(10) DEFAULT NULL,
information text,
username varchar(20) DEFAULT NULL,
PRIMARY KEY (carnum)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3.2 rental表

rental表用于记录车辆租赁信息,包括车牌号、用户名、租赁天数和总租金。

CREATE TABLE rental (
carnum varchar(11) NOT NULL,
username varchar(20) NOT NULL,
days int(20) DEFAULT NULL,
allprice double(20,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.3 stores表

stores表用于存储门店信息,包括门店编号、地址、联系方式、管理员用户名和密码。

CREATE TABLE stores (
num varchar(3) NOT NULL,
address varchar(100) NOT NULL,
contact char(11) NOT NULL,
adminname varchar(20) DEFAULT NULL,
admin_password varchar(20) DEFAULT NULL,
PRIMARY KEY (num)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.4 user表

user表用于存储用户信息,包括用户名、密码、身份证号和手机号。

CREATE TABLE user (
username varchar(20) NOT NULL,
user_password varchar(20) NOT NULL,
user_cardid varchar(20) NOT NULL,
user_phone char(11) NOT NULL,
PRIMARY KEY (username)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

四、数据库初始化数据

为了方便演示,我们在数据库中插入了一些初始数据,包括车辆信息、租赁记录、门店信息和用户信息。

五、Java Swing车辆租赁管理系统开发

使用Java Swing进行开发,可以实现一个用户友好的界面。在开发过程中,可以通过JDBC连接MySQL数据库,实现对车辆、租赁、门店和用户信息的增删改查功能。

六、程序截图

1.管理员界面

在这里插入图片描述

2.租赁信息浏览

在这里插入图片描述

3.用户信息浏览

在这里插入图片描述

4.车辆信息修改

在这里插入图片描述

5.车辆信息录入

在这里插入图片描述

6.删除

在这里插入图片描述

7.修改用户

在这里插入图片描述

2.用户登录

1.用户浏览界面

在这里插入图片描述

2.个人信息修改界面

在这里插入图片描述

3.注册界面

在这里插入图片描述

七、代码

1.adminUI

package controller;
import java.awt.BorderLayout;

import util.DbUtil;
import vo.User;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import service.UserService;

@SuppressWarnings({ "unused", "serial" })
public class AdminUI extends JFrame implements ActionListener{
	/*
	 * 
	 * 管理员端的界面
	 */
	JTable table;
	JLabel label1,label2,label3,label4,label5;
	Object a[][];
	Object name[] = {"编号","车型","车主","价格(元/天)","颜色","是否被租用","租用的用户"};
	JButton buttonOfXinxiluru,buttonOfuserliulan,buttonOfuserxiugai,buttonOfUdelete,
	        buttonOfXinxiliulan,buttonOfDelete,buttonOfLogout,buttonOfXiangXi,buttonOfXiugai,buttonOfRental;
	Box box1,box2;
	JTextField field,field2,field3,field4;
	JPanel jPanel4,jPanel5;
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	
	public AdminUI(Boolean success)
	{
		
		init();
		setVisible(true);
		setBounds(500, 200, 700, 750);
		setTitle("管理员界面");
		if(success)//successs是一个boolean类型,如果为true,打开此窗口直接信息浏览,false表里面没有信息,需要点击信息浏览!
		{
			xinXiLiuLan();			
		}
	}
	
	void init()
	{
		label1 = new JLabel("汽车租赁信息管理系统");
		buttonOfXinxiluru = new JButton("  汽车信息录入  ");
		buttonOfXinxiluru.addActionListener(this);
		
		buttonOfXinxiliulan = new JButton("  汽车信息浏览  ");
		buttonOfXinxiliulan.addActionListener(this);
		
		buttonOfuserliulan = new JButton("  用户信息浏览  ");
		buttonOfuserliulan.addActionListener(this);
		
		buttonOfRental = new JButton("  租赁信息浏览  ");
		buttonOfRental.addActionListener(this);
		
		buttonOfuserxiugai = new JButton("  用户信息修改  ");
		buttonOfuserxiugai.addActionListener(this);
		
		buttonOfUdelete = new JButton("  用户信息删除  ");
		buttonOfUdelete.addActionListener(this);
		
		buttonOfDelete = new JButton("    删	            除      ");
		buttonOfDelete.addActionListener(this);
		buttonOfLogout = new JButton("  退   出   登   录  ");
		buttonOfLogout.addActionListener(this);
		buttonOfXiugai = new JButton("    修	           改      ");
		buttonOfXiugai.addActionListener(this);
		buttonOfXiangXi = new JButton("  详   细   信   息  ");
		buttonOfXiangXi.addActionListener(this);
		label2 = new JLabel("待删除信息车牌号:");
		label3 = new JLabel("待修改信息的车牌号:");
		label4 = new JLabel("待查询车辆备注的车牌号:");
		label5 = new JLabel("需操作的用户名 :   ");
		field = new JTextField();
		field2 = new JTextField();
		field3 = new JTextField();
		field4 = new JTextField();
		
		
		
		a = new Object[50][7];
		table = new JTable(a, name);//组件的创建
		table.setEnabled(false);
		JScrollPane scrollPane = new JScrollPane(table);
		
		box1 = Box.createVerticalBox();
		box1.add(Box.createVerticalStrut(20));
		box1.add(buttonOfXinxiluru);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(buttonOfXinxiliulan);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(buttonOfuserliulan);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(buttonOfRental);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(label5);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(field4);
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(buttonOfUdelete);		
		
		box1.add(Box.createVerticalStrut(10));
		box1.add(buttonOfuserxiugai);
		
		box1.add(Box.createVerticalStrut(15));
		box1.add(label2);
		
		box1.add(Box.createVerticalStrut(5));
		box1.add(field);
		box1.add(Box.createVerticalStrut(5));
		box1.add(buttonOfDelete);
		box1.add(Box.createVerticalStrut(25));
		box1.add(label3);
		box1.add(Box.createVerticalStrut(5));
		box1.add(field2);
		box1.add(Box.createVerticalStrut(5));
		box1.add(buttonOfXiugai);
		box1.add(Box.createVerticalStrut(25));
		box1.add(label4);
		box1.add(Box.createVerticalStrut(5));
		box1.add(field3);
		box1.add(Box.createVerticalStrut(5));
		box1.add(buttonOfXiangXi);
		box1.add(Box.createVerticalStrut(40));
		box1.add(buttonOfLogout);
		
		box2 = Box.createHorizontalBox();
		box2.add(Box.createHorizontalStrut(10));
		box2.add(box1);   //左边的按钮部分用 box布局
		
		jPanel4 = new JPanel();
		jPanel5 = new JPanel();
		jPanel4.setLayout(new BorderLayout());
		jPanel4.add(box2,BorderLayout.NORTH);//把左边的按钮部分放到jpanel4中。
		jPanel5.setLayout(new BorderLayout());
		jPanel5.add(label1,BorderLayout.NORTH);
		jPanel5.add(scrollPane,BorderLayout.CENTER);//把表格 放jpanel5里
	
		this.setLayout(new BorderLayout());
		add(jPanel5,BorderLayout.EAST);
		add(jPanel4,BorderLayout.WEST);//把两个大的panel放到窗口里面				
	}
	
	
	public void connDB() {
		try {
			Class.forName("com.mysql.jdbc.Driver");//注册驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {//创建连接
			con = DriverManager.getConnection(DbUtil.dbUrlString, DbUtil.dbUser, DbUtil.dbpassword);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	public void closeDB() 
	{
		try {
			stmt.close();
			con.close();
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public void xinXiLiuLan()//信息浏览的方法,因为删除数据后会刷新一下,自动调用此函数。
	{
		int i=0;
		 while(i<50)
		 {
			 a[i][0]=" ";
			 a[i][1]=" ";
			 a[i][2]=" ";
			 a[i][3]=" ";
			 a[i][4]=" ";
			 a[i][5]=" ";
			 a[i][6]=" "; 
			 i++;
		 }
		 i=0;
		 this.connDB();
		 try {
			stmt = con.createStatement();
			 String sql= "select * from car";
			 rs = stmt.executeQuery(sql);
			 while(rs.next())
			 {
				 String number = rs.getString("carnum");
				 String cartype = rs.getString("cartype");
				 String carower = rs.getString("num");
				 String price = rs.getString("price");
				 String color = rs.getString("color");
				 String  hire= rs.getString("hire");
				 String username= rs.getString("username");
				 a[i][0]=number;
				 a[i][1]=cartype;
				 a[i][2]=carower;
				 a[i][3]=price;
				 a[i][4]=color;
				 a[i][5]=hire;
				 a[i][6]=username;
				 i++;
				 
			 }
			 this.closeDB();
			 repaint();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		 this.closeDB();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == buttonOfXinxiluru)
		{
			this.dispose();
			new Input();
		}
		else if(source == buttonOfXinxiliulan)
		{
			xinXiLiuLan();
			
		}
		else if (source == buttonOfUdelete) {
			if(field4.getText().equals(""))
			{
				 JOptionPane.showMessageDialog(null, "输入用户名!");
			}else {
				vo.User user = new User();
				UserService us = new UserService();
				user.setUsername(field4.getText());
				if (us.showUser(user).getUsername().equals(field4.getText())) {
					us.delete(user);
					JOptionPane.showMessageDialog(null, "删除成功!");
				}else {
					JOptionPane.showMessageDialog(null, "用户名不存在!");
				}
			}
		}
		else if(source == buttonOfXiugai)
		{
			
			if(field2.getText().equals(""))
			{
				 JOptionPane.showMessageDialog(null, "输入修改车型的编号!");
			}
			else
			{
				this.dispose();
				new InfoMod(field2.getText());
			}
		}
		else if(source == buttonOfXiangXi)//点击详细信息按钮
		{
			this.dispose();
			new Info(field3.getText());
		}
		else if(source == buttonOfuserliulan)
		{
			this.dispose();
			new UserAll();
		}
		else if(source == buttonOfDelete)//点击删除按钮
		{
			if(field.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "请输入删除车辆的编号!");
			}
			else
			{
				this.connDB();
				String sql;
				try {
					stmt = con.createStatement();
					sql = "select * from car_information  where number='"+field.getText()+"'";//表里找到需要删除的车信息
					rs = stmt.executeQuery(sql);
				} catch (SQLException e2) {
					e2.printStackTrace();
				}
				try {
					if(rs.next())//判断是否有 输入编号的 车辆
					{
						
						int n = JOptionPane.showConfirmDialog(this, "确定删除此车辆信息?","确认对话框",JOptionPane.YES_NO_OPTION);//确认文本框
						if(n == JOptionPane.YES_OPTION)
						{	
							String hire2 = rs.getString("hire");
							if(hire2.equals("是"))
							{
								int m = JOptionPane.showConfirmDialog(this, "此车辆正在被租用,是否删除?","确认对话框",JOptionPane.YES_NO_OPTION);//确认文本框
								if(m == JOptionPane.YES_OPTION)
								{
									try
									{
										stmt = con.createStatement();
										String sql2 = "delete from car_information where number='"+field.getText()+"';";
										stmt.executeUpdate(sql2);
									}
									catch (SQLException e1)
									{
										e1.printStackTrace();
									}
									this.closeDB();
									repaint();
									field.setText("");
									JOptionPane.showMessageDialog(null,"删除成功!");
									xinXiLiuLan();
									return;
								}
								else 
								{
									return;				
								}
							}
							try
							{
								stmt = con.createStatement();
								String sql2 = "delete from car where carnum='"+field.getText()+"';";
								stmt.executeUpdate(sql2);
							}
							catch (SQLException e1)
							{
								e1.printStackTrace();
							}
							this.closeDB();
							repaint();
							field.setText("");
							JOptionPane.showMessageDialog(null,"删除成功!");
							xinXiLiuLan();
							
						}
						else if(n == JOptionPane.NO_OPTION)
						{
							
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "没有此编号的车辆信息!");
					}
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}												
			}
			
		}
		
		else if (source == buttonOfuserxiugai) {
			new AdminUserRevise(field4.getText());
		}
		
		else if (source == buttonOfRental) {
			new Rental();
		}
		else if(source == buttonOfLogout)//退出
		{
			this.dispose();
			new Login();
			
		}
		
	}

}


2.AdminUserRevise


package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import util.DbUtil;

	@SuppressWarnings("serial")
	public class AdminUserRevise extends JFrame implements ActionListener {
		JTextField field1,field4,field5;
		JPasswordField field2,field3;
		JButton buttonOfZhuce,buttonOfFanhui;
		Box box1,box2,box3,box4,box5,box6,baseBox;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		public AdminUserRevise(String username)
		{
			
			init();
			setVisible(true);
			setBounds(500, 200, 500, 450);
			setTitle("管理员修改用户界面");
		}
		@SuppressWarnings("static-access")
		void init()
		{			
			box1= Box.createHorizontalBox();
			box1.add(new JLabel("新用户名:"));
			box1.add(Box.createHorizontalStrut(8));
			field1 = new JTextField(15);
			box1.add(field1);
			
			box2= Box.createHorizontalBox();
			box2.add(new JLabel("密         码:"));
			box2.add(Box.createHorizontalStrut(8));
			field2 = new JPasswordField(15);
			box2.add(field2);
			
			box3= Box.createHorizontalBox();
			box3.add(new JLabel("再次输入:"));
			box3.add(Box.createHorizontalStrut(8));
			field3 = new JPasswordField(15);
			box3.add(field3);
			
			box5= Box.createHorizontalBox();
			box5.add(new JLabel("驾驶证号:"));
			box5.add(Box.createHorizontalStrut(8));
			field4 = new JTextField(15);
			box5.add(field4);
			
			box6= Box.createHorizontalBox();
			box6.add(new JLabel("手  机  号:"));
			box6.add(Box.createHorizontalStrut(8));
			field5 = new JTextField(15);
			box6.add(field5);
			
			box4= Box.createHorizontalBox();
			buttonOfZhuce = new JButton("修改");
			buttonOfZhuce.addActionListener(this);
			buttonOfFanhui = new JButton("返回");
			buttonOfFanhui.addActionListener(this);
			box4.add(buttonOfZhuce);
			box4.add(box4.createHorizontalStrut(5));
			box4.add(buttonOfFanhui);
								
			baseBox = Box.createVerticalBox();
			baseBox.add(Box.createVerticalStrut(50));
			baseBox.add(box1);
			baseBox.add(Box.createVerticalStrut(10));
			baseBox.add(box2);
			baseBox.add(Box.createVerticalStrut(10));
			baseBox.add(box3);
			baseBox.add(Box.createVerticalStrut(10));
			baseBox.add(box5);
			baseBox.add(Box.createVerticalStrut(10));
			baseBox.add(box6);
			baseBox.add(Box.createVerticalStrut(20));
			baseBox.add(box4);
			
			add(baseBox);
			
		}
		
		public void connDB() { // 连接数据库
			try {
				Class.forName("com.mysql.jdbc.Driver");//注册驱动
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			try {//创建连接
				con = DriverManager.getConnection(DbUtil.dbUrlString, DbUtil.dbUser, DbUtil.dbpassword);
				stmt = con.createStatement();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}

		public void closeDB() // 关闭连接
		{
			try {
				stmt.close();
				con.close();
			} 
			catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();			
			if(source == buttonOfZhuce)
			{
			 if (field1.getText().equals("")||field4.getText().equals("")||field5.getText().equals("")
					 ||String.valueOf(field2.getPassword()).equals("")||String.valueOf(field3.getPassword()).equals("")
				 )
				{
					JOptionPane.showMessageDialog(null, "请填写完整!");
				}				
					else 
						{
							if(String.valueOf(field2.getPassword()).equals(String.valueOf(field3.getPassword())))
							{
								String str ="update user set username='"+field1.getText()+"', user_password='"+String.valueOf(field2.getPassword())+
										"',user_cardid='"+field4.getText()+"',user_phone='"+field5.getText()+"';";
								try {
									stmt.executeUpdate(str);
									rs.close();
								} catch (SQLException e1) {
									e1.printStackTrace();
								}
								JOptionPane.showMessageDialog(null, "修改成功!");
								this.dispose();
							}
							else {
								JOptionPane.showMessageDialog(null,"两次输入的密码不匹配!" );
							}
						}
					}																																			 												
			else if(source == buttonOfFanhui)
			{
				this.dispose();				
			}						
		}
	
	}

3.CarRemark

package controller;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import util.DbUtil;

/*
 * 
 * 这个车辆界面详细信息 用户端
 */
@SuppressWarnings("serial")
public class CarRemak extends JFrame implements ActionListener{
	
	JTextArea area;
	Box box1,baseBox,box2,baseBox2;
	JButton buttonOfBianJi,buttonOfFanHui;
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	String number=null;
	int setEnable =0;
	String information;
	String username = null;
	
	public  CarRemak(String number,String username)
	{
		
		init();
		setVisible(true);
		setBounds(500, 200, 620, 360);
		setTitle("车辆详细信息界面");
		this.number = number;
		this.username = username;
		setArea();
	}
	
	public  void setArea()//打开界面就把 数据库中的详细信息写入到area中
	{
		this.connDB();
		try {
			stmt = con.createStatement();
			String sql = "select * from car_information where number='"+number+"'; ";
			rs = stmt.executeQuery(sql);
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		try {
			if(rs.next())
			{
				information = rs.getString("information");
				area.setText(information);
				area.setEnabled(false);
				
			}
			else
			{
				JOptionPane.showMessageDialog(null,"没有此编号的车辆的详细信息!");
			}
			
		} catch (HeadlessException e2) {
			e2.printStackTrace();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
	}
	
	@SuppressWarnings("static-access")
	void init()
	{
		JLabel label = new JLabel("车辆的详细信息:");
		area = new JTextArea(10, 10);
		buttonOfFanHui = new JButton("返回");
		buttonOfFanHui.addActionListener(this);
	
		box1 = Box.createVerticalBox();
		box1.add(box1.createVerticalStrut(8));
		box1.add(label);
		box1.add(area);
		
		
		box2 = Box.createHorizontalBox();
		box2.add(box2.createHorizontalStrut(8));
		box2.add(buttonOfFanHui);
		
		
		baseBox = Box.createHorizontalBox();
		baseBox.add(baseBox.createHorizontalStrut(10));
		baseBox.add(box1);
		baseBox.add(baseBox.createHorizontalStrut(10));
		
		baseBox2 = Box.createVerticalBox();
		baseBox2.add(baseBox);
		baseBox2.add(baseBox2.createVerticalStrut(10));
		baseBox2.add(box2);
		baseBox2.add(baseBox2.createVerticalStrut(10));
		
		add(baseBox2);
		
		
		
	}
	
	public void connDB() { // 连接数据库
		try {
			Class.forName("com.mysql.jdbc.Driver");//注册驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {//创建连接
			con = DriverManager.getConnection(DbUtil.dbUrlString, DbUtil.dbUser, DbUtil.dbpassword);
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		
	}

	public void closeDB() // 关闭连接
	{
		try {
			stmt.close();
			con.close();
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
	 if (source == buttonOfFanHui)
		{
			this.dispose();
			new UserUI(username);
		}							
		}			
}

4.Info


package controller;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import util.DbUtil;


@SuppressWarnings("serial")
public class Info extends JFrame implements ActionListener{
	JTextArea area;
	Box box1,baseBox,box2,baseBox2;
	JButton buttonOfBianJi,buttonOfFanHui,buttonOfTi;
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	String number=null;
	int setEnable =0;
	String information;
/*	
 * 管理员查询车辆的详细信息
 */
	public Info(String number)
	{
		
		init();
		setVisible(true);
		setBounds(500, 200, 620, 360);
		setTitle("管理员详细信息界面");
		this.number = number;
		setArea();
		
	}
	public  void setArea()//打开界面就把 数据库中的详细信息写入到area中
	{
		this.connDB();
		try {
			stmt = con.createStatement();
			String sql = "select * from car where carnum='"+number+"'; ";
			rs = stmt.executeQuery(sql);
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		try {
			if(rs.next())
			{
				information = rs.getString("information");
				area.setText(information);
				area.setEnabled(false);
				
			}
			else
			{
				JOptionPane.showMessageDialog(null,"没有此编号的车辆的详细信息!");
				new AdminUI(true);
			}
			
		} catch (HeadlessException e2) {
			e2.printStackTrace();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
	}
	@SuppressWarnings("static-access")
	void init()
	{
		JLabel label = new JLabel("车辆的详细信息:");
		area = new JTextArea(10, 10);
		buttonOfBianJi = new JButton("编辑");
		buttonOfBianJi.addActionListener(this);
		buttonOfFanHui = new JButton("返回");
		buttonOfFanHui.addActionListener(this);
		buttonOfTi = new JButton("提交");
		buttonOfTi.addActionListener(this);
		
		box1 = Box.createVerticalBox();
		box1.add(box1.createVerticalStrut(8));
		box1.add(label);
		box1.add(area);
		
		
		box2 = Box.createHorizontalBox();
		box2.add(box2.createHorizontalStrut(8));
	    box2.add(buttonOfBianJi);
		box2.add(box2.createHorizontalStrut(8));
		box2.add(buttonOfTi);
		box2.add(box2.createHorizontalStrut(8));
		box2.add(buttonOfFanHui);
		
		
		baseBox = Box.createHorizontalBox();
		baseBox.add(baseBox.createHorizontalStrut(10));
		baseBox.add(box1);
		baseBox.add(baseBox.createHorizontalStrut(10));
		
		baseBox2 = Box.createVerticalBox();
		baseBox2.add(baseBox);
		baseBox2.add(baseBox2.createVerticalStrut(10));
		baseBox2.add(box2);
		baseBox2.add(baseBox2.createVerticalStrut(10));
		
		add(baseBox2);
	}
	
	public void connDB() { // 连接数据库
		try {
			Class.forName("com.mysql.jdbc.Driver");//注册驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {//创建连接
			con = DriverManager.getConnection(DbUtil.dbUrlString, DbUtil.dbUser, DbUtil.dbpassword);
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		
	}

	public void closeDB() // 关闭连接
	{
		try {
			stmt.close();
			con.close();
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == buttonOfBianJi)
		{
			area.setEnabled(true);
			setEnable = 1;
			
		}
		else if (source == buttonOfFanHui)
		{
			this.dispose();
			new AdminUI(false);
		}
		else if(source == buttonOfTi)
		{
			if(setEnable == 1)
			{
				String areaString = area.getText();
				this.connDB();
				try 
				{
					stmt = con.createStatement();		
					String sqlString ="update car_information set information='"+areaString+"'where number = '"+number+"';";
					stmt.executeUpdate(sqlString);
				} catch (SQLException e1) 
				{
					e1.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, "提交成功!");
				this.closeDB();
				this.dispose();
				new AdminUI(true);
			}
			else
			{
				JOptionPane.showMessageDialog(null, "请点击编辑变换成编辑状态!");
			}
			
		}
		
	}

}

5.Login

package controller;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import service.AdminService;
import service.UserService;
import vo.Stores;
import vo.User;
@SuppressWarnings("serial")
public class Login extends JFrame implements ActionListener{

	JTextField zhanghao_field;
	JPasswordField mima_field;
	JRadioButton yonghu_radioButton,guanliyuan_radioButton;
	JButton login_button,zhuce_button;
	Box box1,box2,box3,box4,basebBox;//账号,密码,两个radiobutton,两个按钮都是用行式盒子布局。 basebox用列式把他们包裹起来。
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	public Login()
	{
		setLayout(new FlowLayout());
		init();
		setVisible(true);
		setBounds(500, 200, 500, 500);
		setTitle("登陆界面");
	}
	
	void init(){		
		box1 = Box.createHorizontalBox();
		box1.add(new JLabel("账号:"));
		box1.add(Box.createHorizontalStrut(8));
		zhanghao_field = new JTextField(15);
		box1.add(zhanghao_field);//登陆界面 账号和输入框的一行
		
		box2 = Box.createHorizontalBox();
		box2.add(new JLabel("密码:"));
		box2.add(Box.createHorizontalStrut(8));
		mima_field = new JPasswordField(15);
		box2.add(mima_field);//登陆界面密码和输入框的一行
		
		box3 = Box.createHorizontalBox();
		ButtonGroup group = new ButtonGroup();
		yonghu_radioButton = new JRadioButton("用户");
		group.add(yonghu_radioButton);
		yonghu_radioButton.addActionListener(this);
		box3.add(yonghu_radioButton);
		box3.add(Box.createHorizontalStrut(8));
		guanliyuan_radioButton = new JRadioButton("管理员");
		group.add(guanliyuan_radioButton);
		guanliyuan_radioButton.addActionListener(this);
		box3.add(guanliyuan_radioButton);//登陆界面 单选框
		
		
		box4 = Box.createHorizontalBox();
		login_button = new JButton("登陆");
		login_button.addActionListener(this);
		box4.add(login_button);
		box4.add(Box.createHorizontalStrut(8));
		zhuce_button = new JButton("注册");
		zhuce_button.addActionListener(this);
		box4.add(zhuce_button);//登陆界面两个按钮
		
		
		basebBox = Box.createVerticalBox();
		basebBox.add(Box.createVerticalStrut(50));
		basebBox.add(box1);
		basebBox.add(Box.createVerticalStrut(10));
		basebBox.add(box2);
		basebBox.add(Box.createVerticalStrut(30));
		basebBox.add(box3);
		basebBox.add(Box.createVerticalStrut(80));
		basebBox.add(box4);//把4个盒子放一个大盒子		
		add(basebBox);	
		}		

	 public void actionPerformed(ActionEvent e) {
	        Object source = e.getSource();
	        if (source == login_button) {
	            if (!yonghu_radioButton.isSelected() && !guanliyuan_radioButton.isSelected()) {
	                JOptionPane.showMessageDialog(null, "请选择身份!");
	            } else if (zhanghao_field.getText().equals("") || mima_field.toString().equals("")) {
	                JOptionPane.showMessageDialog(null, "登录名和密码不能为空!");
	            } else {
	                if (guanliyuan_radioButton.isSelected()) {
	                    if (zhanghao_field.getText().equals("admin") && String.valueOf(mima_field.getPassword()).equals("admin")) {
	                        new AdminUI(false);
	                    } else {
	                        JOptionPane.showMessageDialog(null, "管理员账号或密码错误!");
	                        zhanghao_field.setText("");
	                        mima_field.setText("");
	                    }
	                } else {
	                    UserService userService = new UserService();
	                    vo.User user = new User();
	                    user.setUsername(zhanghao_field.getText());
	                    userService.showUser(user);
	                    if (userService.showUser(user).getUsername().equals(zhanghao_field.getText())) {
	                        if (userService.showUser(user).getUser_password().equals(String.valueOf(mima_field.getPassword()))) {
	                            new UserUI(zhanghao_field.getText());
	                        } else {
	                            JOptionPane.showMessageDialog(null, "密码错误!");
	                            mima_field.setText("");
	                        }
	                    } else {
	                        JOptionPane.showMessageDialog(null, "不存在此账号,请注册!");
	                        zhanghao_field.setText("");
	                        mima_field.setText("");
	                    }
	                }
	            }
	        } else if (source == zhuce_button) {
	            new Enrol();
	        }
	    }
}	

八、总结

通过本博客,我们了解了一个基于Java Swing的车辆租赁管理系统的数据库设计和开发流程。这个系统可以帮助车辆租赁公司更好地管理业务,提高效率,提供更好的服务。

九、联系与交流

:969060742 完整代码、sql、程序资源
文章来源:https://blog.csdn.net/m0_58065010/article/details/135277386
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。