1、引言
设计结课作业,课程设计无处下手,网页要求的总数量太多?没有合适的模板?数据库,java,python,vue,html作业复杂工程量过大?毕设毫无头绪等等一系列问题。你想要解决的问题,在微信公众号“coding加油站”中全部会得到解决
2、作品介绍
车辆管理系统采用java,swing,mysql,html技术来实现,符合所学知识体系,适用于常见的作业以及课程设计,需要获取更多的作品,请关注微信公众号:coding加油站,回复“java租车”免费获取,如需更多资料,可在微信后台留言。欢迎大家来提问,交流学习。
2.1、作品简介方面
车辆管理系统采用常规方式来实现,符合绝大部分的要求。代码配置有相关文档讲解,如需从代码中学到知识点,那么这个作品将是你的不二之选
2.2、作品二次开发工具
此作品代码相对简单,基本使用课堂中所学知识点来完成,只需要修改相关的介绍文字,一些图片,就可以改为自己独一无二的代码,网页作品下载后可使用任意编辑软件(例如:DW、HBuilder、NotePAD 、Vscode 、Sublime 、Webstorm 所有编辑器均可使用)。
2.3、作品技术介绍
界面开发是软件开发中的一项重要工作,且界面可以作为计算机用户和计算机系统交互的接口,采用面向对象的方式完成项目的开发,使用常见的swing,awt完成界面的开发。使用mysql数据库完成数据的存储。实现基本的租车逻辑、界面可视化、信息导出、数据存储等功能。实现管理员、用户两种角色登录,并结合Java开发中常用的开发技术或特性(工厂模式、继承、多态等)构建整个软件
2.4、注意事项
1.代码无法直接运行,需要先配置数据库(MySQL),并且修改DBUtil类里面的数据库连接地址等
2.整个项目不算复杂,之所以上传整个源代码,一方面是为了做个备份,另外一方面则是顺便给有需要的人一个参考(正面 or 反面教材)
3、作品演示
【coding加油站】java Swing程序设计---车辆管理系统
3.1、登录页
相关代码:
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
ResourceBundle bundle = ResourceBundle.getBundle("configuration.ConfigurationFile");
frame1 = new JFrame("登录");
panel1 = new JPanel();
splitPane3 = new JSplitPane();
splitPane4 = new JSplitPane();
label1 = new JLabel();
label2 = new JLabel();
splitPane5 = new JSplitPane();
inputAccount = new JTextField();
inputPassword = new JPasswordField();
splitPane1 = new JSplitPane();
checkBox1 = new JCheckBox();
splitPane2 = new JSplitPane();
loginButton = new JButton();
registerButton = new JButton();
title = new JLabel();
splitPane6 = new JSplitPane();
label3 = new JLabel();
moreButton = new JButton();
//======== frame1 ========
{
frame1.setMinimumSize(new Dimension(330, 240));
frame1.setIconImage(new ImageIcon(getClass().getResource("/huang/wen/source/logo_white.png")).getImage());
frame1.setResizable(false);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
var frame1ContentPane = frame1.getContentPane();
frame1ContentPane.setLayout(new BorderLayout());
//======== panel1 ========
{
panel1.setLayout(new BorderLayout());
//======== splitPane3 ========
{
splitPane3.setPreferredSize(new Dimension(78, 72));
splitPane3.setMinimumSize(new Dimension(80, 72));
splitPane3.setMaximumSize(new Dimension(80, 72));
//======== splitPane4 ========
{
splitPane4.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane4.setMinimumSize(new Dimension(29, 72));
splitPane4.setMaximumSize(new Dimension(29, 72));
splitPane4.setPreferredSize(new Dimension(29, 72));
//---- label1 ----
label1.setText(bundle.getString("Welcome.label1.text"));
label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
label1.setMaximumSize(new Dimension(27, 36));
label1.setMinimumSize(new Dimension(27, 36));
label1.setInheritsPopupMenu(false);
label1.setPreferredSize(new Dimension(27, 36));
splitPane4.setTopComponent(label1);
//---- label2 ----
label2.setText(bundle.getString("Welcome.label2.text"));
label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
label2.setMaximumSize(new Dimension(27, 36));
label2.setMinimumSize(new Dimension(27, 36));
label2.setInheritsPopupMenu(false);
label2.setPreferredSize(new Dimension(27, 36));
splitPane4.setBottomComponent(label2);
}
splitPane3.setLeftComponent(splitPane4);
//======== splitPane5 ========
{
splitPane5.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane5.setMaximumSize(new Dimension(51, 72));
splitPane5.setPreferredSize(new Dimension(49, 72));
splitPane5.setMinimumSize(new Dimension(51, 72));
splitPane5.setDividerLocation(40);
//---- inputAccount ----
inputAccount.setMaximumSize(new Dimension(49, 36));
inputAccount.setMinimumSize(new Dimension(49, 36));
inputAccount.setPreferredSize(new Dimension(49, 36));
inputAccount.setMargin(new Insets(2, 5, 2, 5));
splitPane5.setTopComponent(inputAccount);
//---- inputPassword ----
inputPassword.setMaximumSize(new Dimension(49, 36));
inputPassword.setMinimumSize(new Dimension(49, 36));
inputPassword.setPreferredSize(new Dimension(49, 36));
splitPane5.setBottomComponent(inputPassword);
}
splitPane3.setRightComponent(splitPane5);
}
panel1.add(splitPane3, BorderLayout.CENTER);
//======== splitPane1 ========
{
//---- checkBox1 ----
checkBox1.setText(bundle.getString("Welcome.checkBox1.text"));
checkBox1.setToolTipText("\u5982\u679c\u4f60\u662f\u8be5\u7cfb\u7edf\u7684\u7ba1\u7406\u5458\u8bf7\u52fe\u9009");
splitPane1.setLeftComponent(checkBox1);
//======== splitPane2 ========
{
splitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- loginButton ----
loginButton.setText(bundle.getString("Welcome.loginButton.text"));
loginButton.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/login.png")));
loginButton.setMaximumSize(new Dimension(82, 20));
splitPane2.setTopComponent(loginButton);
//---- registerButton ----
registerButton.setText(bundle.getString("Welcome.registerButton.text"));
registerButton.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/register.png")));
splitPane2.setBottomComponent(registerButton);
}
splitPane1.setRightComponent(splitPane2);
}
panel1.add(splitPane1, BorderLayout.SOUTH);
//---- title ----
title.setText(bundle.getString("Welcome.title.text"));
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setFont(new Font("\u534e\u6587\u6977\u4f53", Font.PLAIN, 18));
panel1.add(title, BorderLayout.NORTH);
}
frame1ContentPane.add(panel1, BorderLayout.CENTER);
//======== splitPane6 ========
{
//---- label3 ----
label3.setText(bundle.getString("Welcome.label3.text"));
splitPane6.setLeftComponent(label3);
//---- moreButton ----
moreButton.setText(bundle.getString("Welcome.moreButton.text"));
moreButton.setMinimumSize(new Dimension(10, 30));
moreButton.setMaximumSize(new Dimension(10, 30));
moreButton.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/more.png")));
splitPane6.setRightComponent(moreButton);
}
frame1ContentPane.add(splitPane6, BorderLayout.SOUTH);
frame1.setSize(335, 235);
frame1.setLocationRelativeTo(frame1.getOwner());
frame1.setVisible(true);
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
//登录
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 按钮点击时执行的事件
String temp_account = inputAccount.getText();
String temp_password = new String(inputPassword.getPassword());
if (checkBox1.isSelected()) {
//管理员登录
String sql = "select * from administrator where account = ? and password = ?";
Connection conn = null;
PreparedStatement pstmt = null;
DBUtil mysqlTool = new DBUtil();
try {
conn = mysqlTool.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, temp_account);//给占位符?传递值
pstmt.setString(2, temp_password);
//执行查询语句
ResultSet res = pstmt.executeQuery();
if (res.next()) {//若查询结果有数据返回则登录成功
frame1.dispose();
Administrator administrator=new Administrator(temp_account);
} else {
JOptionPane.showConfirmDialog(frame1, "您输入的管理员账号或密码有误,请重新输入!\n(如果为普通用户登录,请不要勾选“管理员”选项)", "Administrator-登录失败", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}finally {
mysqlTool.close(pstmt);
mysqlTool.close(conn); //必须关闭
}
} else {
//用户登录
String sql = "select * from users where account = ? and password = ?";
Connection conn = null;
PreparedStatement pstmt = null;
DBUtil mysqlTool = new DBUtil();
try {
conn = mysqlTool.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, temp_account);//给占位符?传递值
pstmt.setString(2, temp_password);
//执行查询语句
ResultSet res = pstmt.executeQuery();
if (res.next()) {//若查询结果有数据返回则登录成功
frame1.dispose();
User user=new User(temp_account);
} else {
JOptionPane.showConfirmDialog(frame1, "您输入的账号或密码有误,请重新输入!\n(没有注册账号?点击下方按钮立即注册)", "User-登录失败", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
mysqlTool.close(conn); //必须关闭
}
}
}
});
//注册
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame1.dispose();
Register register=new Register();
}
});
//更多
moreButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(frame1, "更多信息请关注公众号:coding加油站", "更多", JOptionPane.DEFAULT_OPTION);
}
});
}
3.2、管理员页面
相关代码:
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
ResourceBundle bundle = ResourceBundle.getBundle("configuration.ConfigurationFile");
Jframe_user = new JFrame("汽车租赁系统");
label1 = new JLabel();
panel1 = new JPanel();
menu = new JTabbedPane();
scrollPane1 = new JScrollPane();
car = new JTable();
scrollPane3 = new JScrollPane();
bus = new JTable();
scrollPane4 = new JScrollPane();
truck = new JTable();
scrollPane5 = new JScrollPane();
panel2 = new JPanel();
label3 = new JLabel();
splitPane3 = new JSplitPane();
splitPane2 = new JSplitPane();
splitPane6 = new JSplitPane();
label_greetings = new JLabel();
label5 = new JLabel();
splitPane13 = new JSplitPane();
label_name = new JLabel();
splitPane14 = new JSplitPane();
label_gender = new JLabel();
splitPane15 = new JSplitPane();
label_account = new JLabel();
splitPane16 = new JSplitPane();
label_accountBalance = new JLabel();
vSpacer1 = new JPanel(null);
splitPane4 = new JSplitPane();
splitPane5 = new JSplitPane();
Button_rentCar = new JButton();
Button_returnVehicle = new JButton();
splitPane1 = new JSplitPane();
Button_viewAllVehicles = new JButton();
splitPane7 = new JSplitPane();
Button_viewAvailableVehicles = new JButton();
splitPane8 = new JSplitPane();
Button_rentalSituation = new JButton();
splitPane9 = new JSplitPane();
label4 = new JLabel();
splitPane10 = new JSplitPane();
Button_accountRecharge = new JButton();
splitPane11 = new JSplitPane();
Button_changePassword = new JButton();
splitPane12 = new JSplitPane();
Button_signOut = new JButton();
splitPane17 = new JSplitPane();
button1 = new JButton();
label2 = new JLabel();
//======== Jframe_user ========
{
Jframe_user.setVisible(true);
Jframe_user.setMinimumSize(new Dimension(930, 620));
Jframe_user.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Jframe_user.setResizable(false);
Jframe_user.setIconImage(new ImageIcon(getClass().getResource("/huang/wen/source/logo_white.png")).getImage());
var Jframe_userContentPane = Jframe_user.getContentPane();
Jframe_userContentPane.setLayout(new BorderLayout());
//---- label1 ----
label1.setText(bundle.getString("User.label1.text"));
label1.setHorizontalTextPosition(SwingConstants.CENTER);
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setMaximumSize(new Dimension(234, 30));
label1.setMinimumSize(new Dimension(234, 30));
label1.setPreferredSize(new Dimension(234, 30));
label1.setFont(new Font("\u6977\u4f53", Font.PLAIN, 25));
Jframe_userContentPane.add(label1, BorderLayout.PAGE_START);
//======== panel1 ========
{
panel1.setMaximumSize(new Dimension(630, 620));
panel1.setMinimumSize(new Dimension(630, 620));
panel1.setPreferredSize(new Dimension(630, 620));
panel1.setLayout(new BorderLayout());
//======== menu ========
{
menu.setMinimumSize(new Dimension(630, 620));
menu.setPreferredSize(new Dimension(630, 620));
menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
//======== scrollPane1 ========
{
//---- car ----
car.setMinimumSize(new Dimension(30, 620));
car.setMaximumSize(new Dimension(2147483647, 620));
car.setPreferredSize(new Dimension(150, 620));
car.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 12));
car.setFillsViewportHeight(true);
scrollPane1.setViewportView(car);
}
menu.addTab("\u8f7f\u8f66", scrollPane1);
//======== scrollPane3 ========
{
//---- bus ----
bus.setPreferredSize(new Dimension(150, 620));
scrollPane3.setViewportView(bus);
}
menu.addTab("\u5ba2\u8f66", scrollPane3);
//======== scrollPane4 ========
{
//---- truck ----
truck.setPreferredSize(new Dimension(150, 620));
scrollPane4.setViewportView(truck);
}
menu.addTab("\u8d27\u8f66", scrollPane4);
}
panel1.add(menu, BorderLayout.NORTH);
}
Jframe_userContentPane.add(panel1, BorderLayout.LINE_END);
//======== scrollPane5 ========
{
scrollPane5.setPreferredSize(new Dimension(250, 199));
//======== panel2 ========
{
panel2.setLayout(new BorderLayout());
//---- label3 ----
label3.setText(bundle.getString("User.label3.text"));
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setMaximumSize(new Dimension(24, 30));
label3.setPreferredSize(new Dimension(24, 30));
label3.setMinimumSize(new Dimension(24, 30));
label3.setFont(new Font("\u6977\u4f53", Font.PLAIN, 18));
panel2.add(label3, BorderLayout.PAGE_START);
//======== splitPane3 ========
{
//======== splitPane2 ========
{
splitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
//======== splitPane6 ========
{
splitPane6.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_greetings ----
label_greetings.setText(bundle.getString("User.label_greetings.text"));
label_greetings.setMaximumSize(new Dimension(72, 30));
label_greetings.setMinimumSize(new Dimension(72, 30));
label_greetings.setPreferredSize(new Dimension(72, 30));
label_greetings.setFont(new Font("\u65b9\u6b63\u59da\u4f53", Font.PLAIN, 16));
splitPane6.setTopComponent(label_greetings);
//---- label5 ----
label5.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Female user.png")));
splitPane6.setBottomComponent(label5);
}
splitPane2.setTopComponent(splitPane6);
//======== splitPane13 ========
{
splitPane13.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_name ----
label_name.setText(bundle.getString("User.label_name.text"));
label_name.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
label_name.setMaximumSize(new Dimension(41, 30));
label_name.setMinimumSize(new Dimension(41, 30));
label_name.setPreferredSize(new Dimension(41, 30));
splitPane13.setTopComponent(label_name);
//======== splitPane14 ========
{
splitPane14.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_gender ----
label_gender.setText(bundle.getString("User.label_gender.text"));
label_gender.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
label_gender.setMaximumSize(new Dimension(41, 30));
label_gender.setMinimumSize(new Dimension(41, 30));
label_gender.setPreferredSize(new Dimension(41, 30));
splitPane14.setTopComponent(label_gender);
//======== splitPane15 ========
{
splitPane15.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_account ----
label_account.setText(bundle.getString("User.label_account.text"));
label_account.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
label_account.setMaximumSize(new Dimension(41, 30));
label_account.setMinimumSize(new Dimension(41, 30));
label_account.setPreferredSize(new Dimension(41, 30));
splitPane15.setTopComponent(label_account);
//======== splitPane16 ========
{
splitPane16.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_accountBalance ----
label_accountBalance.setText(bundle.getString("User.label_accountBalance.text"));
label_accountBalance.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
label_accountBalance.setPreferredSize(new Dimension(68, 30));
label_accountBalance.setMinimumSize(new Dimension(68, 30));
label_accountBalance.setMaximumSize(new Dimension(68, 30));
splitPane16.setTopComponent(label_accountBalance);
splitPane16.setBottomComponent(vSpacer1);
}
splitPane15.setBottomComponent(splitPane16);
}
splitPane14.setBottomComponent(splitPane15);
}
splitPane13.setBottomComponent(splitPane14);
}
splitPane2.setBottomComponent(splitPane13);
}
splitPane3.setLeftComponent(splitPane2);
//======== splitPane4 ========
{
splitPane4.setOrientation(JSplitPane.VERTICAL_SPLIT);
//======== splitPane5 ========
{
splitPane5.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_rentCar ----
Button_rentCar.setText(bundle.getString("User.Button_rentCar.text"));
Button_rentCar.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Hire.png")));
Button_rentCar.setFont(new Font("\u6977\u4f53", Font.PLAIN, 20));
Button_rentCar.setMaximumSize(new Dimension(151, 36));
Button_rentCar.setMinimumSize(new Dimension(151, 36));
Button_rentCar.setPreferredSize(new Dimension(151, 36));
Button_rentCar.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane5.setTopComponent(Button_rentCar);
//---- Button_returnVehicle ----
Button_returnVehicle.setText(bundle.getString("User.Button_returnVehicle.text"));
Button_returnVehicle.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Return the vehicle.png")));
Button_returnVehicle.setFont(new Font("\u6977\u4f53", Font.PLAIN, 20));
Button_returnVehicle.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane5.setBottomComponent(Button_returnVehicle);
}
splitPane4.setTopComponent(splitPane5);
//======== splitPane1 ========
{
splitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_viewAllVehicles ----
Button_viewAllVehicles.setText(bundle.getString("User.Button_viewAllVehicles.text"));
Button_viewAllVehicles.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/find all vehicles.png")));
Button_viewAllVehicles.setFont(new Font("\u6977\u4f53", Font.PLAIN, 20));
Button_viewAllVehicles.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane1.setTopComponent(Button_viewAllVehicles);
//======== splitPane7 ========
{
splitPane7.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_viewAvailableVehicles ----
Button_viewAvailableVehicles.setText(bundle.getString("User.Button_viewAvailableVehicles.text"));
Button_viewAvailableVehicles.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/find available vehicles.png")));
Button_viewAvailableVehicles.setFont(new Font("\u6977\u4f53", Font.PLAIN, 20));
Button_viewAvailableVehicles.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane7.setTopComponent(Button_viewAvailableVehicles);
//======== splitPane8 ========
{
splitPane8.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_rentalSituation ----
Button_rentalSituation.setText(bundle.getString("User.Button_rentalSituation.text"));
Button_rentalSituation.setFont(new Font("\u6977\u4f53", Font.PLAIN, 20));
Button_rentalSituation.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/my car rental situation.png")));
Button_rentalSituation.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane8.setTopComponent(Button_rentalSituation);
//======== splitPane9 ========
{
splitPane9.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label4 ----
label4.setText(bundle.getString("User.label4.text"));
label4.setMaximumSize(new Dimension(60, 40));
label4.setMinimumSize(new Dimension(60, 40));
label4.setPreferredSize(new Dimension(60, 40));
label4.setFont(new Font("\u7b49\u7ebf", Font.PLAIN, 11));
label4.setHorizontalAlignment(SwingConstants.CENTER);
label4.setForeground(new Color(102, 153, 255));
splitPane9.setTopComponent(label4);
//======== splitPane10 ========
{
splitPane10.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_accountRecharge ----
Button_accountRecharge.setText(bundle.getString("User.Button_accountRecharge.text"));
Button_accountRecharge.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/recharge.png")));
Button_accountRecharge.setFont(new Font("\u65b0\u5b8b\u4f53", Font.PLAIN, 18));
Button_accountRecharge.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane10.setTopComponent(Button_accountRecharge);
//======== splitPane11 ========
{
splitPane11.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_changePassword ----
Button_changePassword.setText(bundle.getString("User.Button_changePassword.text"));
Button_changePassword.setFont(new Font("\u65b0\u5b8b\u4f53", Font.PLAIN, 18));
Button_changePassword.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/change Password.png")));
Button_changePassword.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane11.setTopComponent(Button_changePassword);
//======== splitPane12 ========
{
splitPane12.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- Button_signOut ----
Button_signOut.setText(bundle.getString("User.Button_signOut.text"));
Button_signOut.setFont(new Font("\u65b0\u5b8b\u4f53", Font.PLAIN, 18));
Button_signOut.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/sign out.png")));
Button_signOut.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
splitPane12.setTopComponent(Button_signOut);
//======== splitPane17 ========
{
splitPane17.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button1 ----
button1.setText(bundle.getString("User.button1.text"));
button1.setMaximumSize(new Dimension(138, 36));
button1.setMinimumSize(new Dimension(138, 36));
button1.setPreferredSize(new Dimension(138, 36));
button1.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Delete account.png")));
button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
splitPane17.setTopComponent(button1);
//---- label2 ----
label2.setText(bundle.getString("User.label2.text"));
label2.setHorizontalTextPosition(SwingConstants.CENTER);
label2.setFont(new Font("Microsoft YaHei UI", Font.BOLD | Font.ITALIC, 18));
splitPane17.setBottomComponent(label2);
}
splitPane12.setBottomComponent(splitPane17);
}
splitPane11.setBottomComponent(splitPane12);
}
splitPane10.setBottomComponent(splitPane11);
}
splitPane9.setBottomComponent(splitPane10);
}
splitPane8.setBottomComponent(splitPane9);
}
splitPane7.setBottomComponent(splitPane8);
}
splitPane1.setBottomComponent(splitPane7);
}
splitPane4.setBottomComponent(splitPane1);
}
splitPane3.setRightComponent(splitPane4);
}
panel2.add(splitPane3, BorderLayout.CENTER);
}
scrollPane5.setViewportView(panel2);
}
Jframe_userContentPane.add(scrollPane5, BorderLayout.CENTER);
Jframe_user.setSize(1150, 660);
Jframe_user.setLocationRelativeTo(Jframe_user.getOwner());
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
//初始化用户信息(首次刷新)
RefreshUserInformation(bundle);
//显示所有轿车(1代表查找所有)
find_Cars(conn, 1);
//显示所有客车
find_Buses(conn, 1);
//显示所有货车
find_Trucks(conn, 1);
//租用车辆
Button_rentCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (is_rent_user.equals("0")) {
int index = -1;
Long timeStamp = System.currentTimeMillis(); //获取当前时间戳
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_date = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); //时间戳转化为时间
if (menu.getSelectedIndex() == 0) {
index = car.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(Jframe_user, "请先在右边的表格中\n选中你想租用的车辆", "租车失败", JOptionPane.WARNING_MESSAGE);
}else {
String flag = car.getValueAt(index, 0).toString();
if (flag.equals("可租")) {
String number_temp = car.getValueAt(index, 1).toString();
String brand_temp = car.getValueAt(index, 2).toString();
Object[] options = {"确定", "我再想想"};
int temp = JOptionPane.showOptionDialog(Jframe_user, "确定租用车牌号为:" + number_temp + "的" + brand_temp + "轿车吗?", "租车确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (temp == 0) {
PreparedStatement pstmt_rent = null;
PreparedStatement pstmt_rent_user = null;
try {
String sql_update = "update cars set flag =?, date=? where vehicleId=?";
pstmt_rent = conn.prepareStatement(sql_update);
pstmt_rent.setString(1, account_user);
pstmt_rent.setString(2, current_date);
pstmt_rent.setString(3, number_temp);
pstmt_rent.executeUpdate();
String sql_update_user = "update users set is_rent =? where account=?";
pstmt_rent_user = conn.prepareStatement(sql_update_user);
pstmt_rent_user.setString(1, number_temp);
pstmt_rent_user.setString(2, account_user);
pstmt_rent_user.executeUpdate();
find_Cars(conn, 1);
RefreshUserInformation(bundle);
JOptionPane.showMessageDialog(Jframe_user, "租车成功!\n祝您用车愉快^_^!", "租车成功", JOptionPane.PLAIN_MESSAGE);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt_rent);
mysqlTool.close(pstmt_rent_user);
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "您所选用的汽车已被租用\n请选择其他车辆!", "租车失败", JOptionPane.WARNING_MESSAGE);
}
}
} else if (menu.getSelectedIndex() == 1) {
index = bus.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(Jframe_user, "请先在右边的表格中\n选中你想租用的车辆", "租车失败", JOptionPane.WARNING_MESSAGE);
}else {
String flag = bus.getValueAt(index, 0).toString();
if (flag.equals("可租")) {
String number_temp = bus.getValueAt(index, 1).toString();
String brand_temp = bus.getValueAt(index, 2).toString();
Object[] options = {"确定", "我再想想"};
int temp = JOptionPane.showOptionDialog(Jframe_user, "确定租用车牌号为:" + number_temp + "的" + brand_temp + "客车吗?", "租车确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (temp == 0) {
PreparedStatement pstmt_rent = null;
PreparedStatement pstmt_rent_user = null;
try {
String sql_update = "update buses set flag =?, date=? where vehicleId=?";
pstmt_rent = conn.prepareStatement(sql_update);
pstmt_rent.setString(1, account_user);
pstmt_rent.setString(2, current_date);
pstmt_rent.setString(3, number_temp);
pstmt_rent.executeUpdate();
String sql_update_user = "update users set is_rent =? where account=?";
pstmt_rent_user = conn.prepareStatement(sql_update_user);
pstmt_rent_user.setString(1, number_temp);
pstmt_rent_user.setString(2, account_user);
pstmt_rent_user.executeUpdate();
find_Buses(conn, 1);
RefreshUserInformation(bundle);
JOptionPane.showMessageDialog(Jframe_user, "租车成功!\n祝您用车愉快^_^!", "租车成功", JOptionPane.PLAIN_MESSAGE);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt_rent);
mysqlTool.close(pstmt_rent_user);
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "您所选用的汽车已被租用\n请选择其他车辆!", "租车失败", JOptionPane.WARNING_MESSAGE);
}
}
} else if (menu.getSelectedIndex() == 2) {
index = truck.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(Jframe_user, "请先在右边的表格中\n选中你想租用的车辆", "租车失败", JOptionPane.WARNING_MESSAGE);
} else {
String flag = truck.getValueAt(index, 0).toString();
if (flag.equals("可租")) {
String number_temp = truck.getValueAt(index, 1).toString();
String brand_temp = truck.getValueAt(index, 2).toString();
Object[] options = {"确定", "我再想想"};
int temp = JOptionPane.showOptionDialog(Jframe_user, "确定租用车牌号为:" + number_temp + "的" + brand_temp + "货车吗?", "租车确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (temp == 0) {
PreparedStatement pstmt_rent = null;
PreparedStatement pstmt_rent_user = null;
try {
String sql_update = "update trucks set flag =?, date=? where vehicleId=?";
pstmt_rent = conn.prepareStatement(sql_update);
pstmt_rent.setString(1,account_user);
pstmt_rent.setString(2, current_date);
pstmt_rent.setString(3, number_temp);
pstmt_rent.executeUpdate();
String sql_update_user = "update users set is_rent =? where account=?";
pstmt_rent_user = conn.prepareStatement(sql_update_user);
pstmt_rent_user.setString(1, number_temp);
pstmt_rent_user.setString(2, account_user);
pstmt_rent_user.executeUpdate();
find_Trucks(conn, 1);
RefreshUserInformation(bundle);
JOptionPane.showMessageDialog(Jframe_user, "租车成功!\n祝您用车愉快^_^!", "租车成功", JOptionPane.PLAIN_MESSAGE);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt_rent);
mysqlTool.close(pstmt_rent_user);
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "您所选用的汽车已被租用\n请选择其他车辆!", "租车失败", JOptionPane.WARNING_MESSAGE);
}
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "系统检测到您已经租用了一辆汽车\n每个用户只能同时租一辆汽车!", "租车失败", JOptionPane.WARNING_MESSAGE);
}
}
});
//归还车辆
Button_returnVehicle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (is_rent_user.equals("0")) {
JOptionPane.showMessageDialog(Jframe_user, "您暂未租用任何汽车!", "还车失败", JOptionPane.WARNING_MESSAGE);
} else {
try {
int days = 0;
Object[] options = {"确定", "我再想想"};
int temp = JOptionPane.showOptionDialog(Jframe_user, "确定归还你所租的车辆吗?", "还车确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (temp == 0) {
int flag = 0;
PreparedStatement pstmt = null;
Long timeStamp = System.currentTimeMillis(); //获取当前时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); // 时间戳转换成时间
try {//轿车
pstmt = conn.prepareStatement("select * from cars where vehicleId = ?");
pstmt.setString(1, is_rent_user);
ResultSet res_car = pstmt.executeQuery();
while (res_car.next() && flag == 0) {
flag = 1;
Car car_temp = new Car();
//计算租用时间
String time = res_car.getString("date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
date = format.parse(time);
days = (int) ((System.currentTimeMillis() - date.getTime()) / (1000 * 24 * 3600));
//计算租金
car_temp.setPerRent(res_car.getFloat("rent"));
car_temp.calRent(days);
//添加营业额记录
PreparedStatement pstmt_1 = null;
try {
String sql_record = "insert into income(time,vehicleId,days,amount) values(?,?,?,?)";
pstmt = (PreparedStatement) conn.prepareStatement(sql_record);
pstmt.setString(1, sd);
pstmt.setString(2, is_rent_user);
pstmt.setInt(3, days);
pstmt.setFloat(4, car_temp.getPerRent());
pstmt.executeUpdate(); //执行
} catch (SQLException ex) {
ex.printStackTrace();
}
//更新营业额
PreparedStatement pstmt_2 = null;
pstmt_2 = conn.prepareStatement("update other_data set money = ? where number = 1");
pstmt_2.setFloat(1, turnover + car_temp.getPerRent());
pstmt_2.executeUpdate();
//更新车库信息
pstmt = conn.prepareStatement("update cars set flag = 1,date=0 where vehicleId = ?");
pstmt.setString(1, is_rent_user);
pstmt.executeUpdate();
//更新用户信息
pstmt = conn.prepareStatement("update users set is_rent = 0 where account = ?");
pstmt.setString(1, account_user);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("update users set balance = ? where account = ?");
pstmt.setFloat(1, balance_user - car_temp.getPerRent());
pstmt.setString(2, account_user);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Jframe_user, "本次共租车" + days + "天\n需支付" + car_temp.getPerRent() + "元" + "\n期待下次您的光临^_^!", "还车成功", JOptionPane.PLAIN_MESSAGE);
find_Cars(conn, 1);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(Jframe_user, "系统出错", "ERROR", JOptionPane.ERROR_MESSAGE);
}
try {//客车
pstmt = conn.prepareStatement("select * from buses where vehicleId = ?");
pstmt.setString(1, is_rent_user);
ResultSet res_bus = pstmt.executeQuery();
while (res_bus.next() && flag == 0) {
flag = 1;
Bus bus_temp = new Bus();
//计算租用时间
String time = res_bus.getString("date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
date = format.parse(time);
days = (int) ((System.currentTimeMillis() - date.getTime()) / (1000 * 24 * 3600));
//计算租金
bus_temp.setPerRent(res_bus.getFloat("rent"));
bus_temp.calRent(days);
//添加营业额记录
PreparedStatement pstmt_1 = null;
try {
String sql_record = "insert into income(time,vehicleId,days,amount) values(?,?,?,?)";
pstmt = (PreparedStatement) conn.prepareStatement(sql_record);
pstmt.setString(1, sd);
pstmt.setString(2, is_rent_user);
pstmt.setInt(3, days);
pstmt.setFloat(4, bus_temp.getPerRent());
pstmt.executeUpdate(); //执行
} catch (SQLException ex) {
ex.printStackTrace();
}
//更新营业额
PreparedStatement pstmt_2 = null;
pstmt_2 = conn.prepareStatement("update other_data set money = ? where number = 1");
pstmt_2.setFloat(1, turnover + bus_temp.getPerRent());
pstmt_2.executeUpdate();
//更新车库信息
pstmt = conn.prepareStatement("update buses set flag = 1,date=0 where vehicleId = ?");
pstmt.setString(1, is_rent_user);
pstmt.executeUpdate();
//更新用户信息
pstmt = conn.prepareStatement("update users set is_rent = 0 where account = ?");
pstmt.setString(1, account_user);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("update users set balance = ? where account = ?");
pstmt.setFloat(1, balance_user - bus_temp.getPerRent());
pstmt.setString(2, account_user);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Jframe_user, "本次共租车" + days + "天\n需支付" + bus_temp.getPerRent() + "元" + "\n期待下次您的光临^_^!", "还车成功", JOptionPane.PLAIN_MESSAGE);
find_Buses(conn, 1);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(Jframe_user, "系统出错", "ERROR", JOptionPane.ERROR_MESSAGE);
}
try {//货车
pstmt = conn.prepareStatement("select * from trucks where vehicleId = ?");
pstmt.setString(1, is_rent_user);
ResultSet res_truck = pstmt.executeQuery();
while (res_truck.next() && flag == 0) {
flag = 1;
Truck truck_temp = new Truck();
//计算租用时间
String time = res_truck.getString("date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
date = format.parse(time);
days = (int) ((System.currentTimeMillis() - date.getTime()) / (1000 * 24 * 3600));
//计算租金
truck_temp.setPerRent(res_truck.getFloat("rent"));
truck_temp.calRent(days);
//添加营业额记录
PreparedStatement pstmt_1 = null;
try {
String sql_record = "insert into income(time,vehicleId,days,amount) values(?,?,?,?)";
pstmt = (PreparedStatement) conn.prepareStatement(sql_record);
pstmt.setString(1, sd);
pstmt.setString(2, is_rent_user);
pstmt.setInt(3, days);
pstmt.setFloat(4, truck_temp.getPerRent());
pstmt.executeUpdate(); //执行
} catch (SQLException ex) {
ex.printStackTrace();
}
//更新营业额
PreparedStatement pstmt_2 = null;
pstmt_2 = conn.prepareStatement("update other_data set money = ? where number = 1");
pstmt_2.setFloat(1, turnover + truck_temp.getPerRent());
pstmt_2.executeUpdate();
//更新车库信息
pstmt = conn.prepareStatement("update trucks set flag = 1,date=0 where vehicleId = ?");
pstmt.setString(1, is_rent_user);
pstmt.executeUpdate();
//更新用户信息
pstmt = conn.prepareStatement("update users set is_rent = 0 where account = ?");
pstmt.setString(1, account_user);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("update users set balance = ? where account = ?");
pstmt.setFloat(1, balance_user - truck_temp.getPerRent());
pstmt.setString(2, account_user);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Jframe_user, "本次共租车" + days + "天\n需支付" + truck_temp.getPerRent() + "元" + "\n期待下次您的光临^_^!", "还车成功", JOptionPane.PLAIN_MESSAGE);
find_Trucks(conn, 1);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(Jframe_user, "系统出错", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(Jframe_user, "发生数据错误", "数据有误", JOptionPane.WARNING_MESSAGE);
}
RefreshUserInformation(bundle);
}
}
});
//查看所有车辆
Button_viewAllVehicles.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
find_Cars(conn, 1);
find_Buses(conn, 1);
find_Trucks(conn, 1);
}
});
//查看可租车辆
Button_viewAvailableVehicles.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
find_Cars(conn, 0);
find_Buses(conn, 0);
find_Trucks(conn, 0);
}
});
//我的租车情况
Button_rentalSituation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (is_rent_user.equals("0")) {
JOptionPane.showMessageDialog(Jframe_user, "您暂未租用任何汽车", "租车情况", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(Jframe_user, "您已租用车牌号为:"+is_rent_user+"汽车", "租车情况", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//账户充值
Button_accountRecharge.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = JOptionPane.showInputDialog(panel1, "请输入充值金额(仅支持整数金额)", "充值", JOptionPane.PLAIN_MESSAGE);
int money = 0;
try {
money = Integer.parseInt(str);
if (money >= 0) {
balance_user += money;
label_accountBalance.setText(bundle.getString("User.label_accountBalance.text") + balance_user);
//写入数据库
PreparedStatement pstmt_balance = null;
try {
String sql_update = "update users set balance =? where account=?";
pstmt_balance = conn.prepareStatement(sql_update);
pstmt_balance.setFloat(1, balance_user);
pstmt_balance.setString(2, account_user);
pstmt_balance.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt_balance);
}
JOptionPane.showMessageDialog(Jframe_user, "成功充值" + money + "\n祝您租车愉快!", "充值成功", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(Jframe_user, "请输入一个大于0的金额!", "充值失败", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(Jframe_user, "请输入一个整数金额!", "充值失败", JOptionPane.WARNING_MESSAGE);
}
}
});
//修改密码
Button_changePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ChangePassword change_paassword = new ChangePassword(conn, account_user);
}
});
//退出登录
Button_signOut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mysqlTool.close(conn); //关闭数据库连接
Jframe_user.dispose();
exit(0);
}
});
//注销账户
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RefreshUserInformation(bundle);
JOptionPane.showMessageDialog(Jframe_user, "请仔细阅读,然后进行下一步操作\n1.账号所有信息将彻底删除\n2.删除后的账户无法再恢复\n3.归还所租车辆后才能删除", "【请谨慎操作】", JOptionPane.WARNING_MESSAGE);
if (is_rent_user.equals("0")) {
String input_password = JOptionPane.showInputDialog(panel1, "为了确保是本人操作,请输入您的登录密码以进行下一步", "二次验证", JOptionPane.PLAIN_MESSAGE);
if (input_password instanceof String) {
String true_password = null;
try {
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("select * from users where account = ?");
pstmt.setString(1, account_user);
ResultSet res = pstmt.executeQuery();
while (res.next()) {
true_password = res.getString("password");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
if (true_password.equals(input_password)) {
Object[] options = {"确定", "我再想想"};
int temp = JOptionPane.showOptionDialog(Jframe_user, "注销后的账号将永远无法恢复\n确认继续删除账号吗?", "注销确认", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
if (temp == 0) {
String sql_1 = "delete from users where account = ?";
PreparedStatement pstmt = null;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql_1);
pstmt.setString(1, account_user);
pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
JOptionPane.showMessageDialog(Jframe_user, "您的账号已成功注销\n感谢您的使用^_^\n期待我们下一次更好的相遇", "注销成功", JOptionPane.INFORMATION_MESSAGE);
mysqlTool.close(conn); //关闭数据库连接
Jframe_user.dispose();
exit(0);
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "密码输入错误,我们暂时无法注销您的账号", "验证失败", JOptionPane.ERROR_MESSAGE);
}
}
} else {
JOptionPane.showMessageDialog(Jframe_user, "暂时不能删除您的账号\n原因:您还有未归还的车辆", "注销失败", JOptionPane.ERROR_MESSAGE);
}
}
});
}
3.3、用户页面
相关代码:
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
ResourceBundle bundle = ResourceBundle.getBundle("configuration.ConfigurationFile");
Administrator = new JFrame("后台管理");
panel1 = new JPanel();
splitPane1 = new JSplitPane();
splitPane2 = new JSplitPane();
label_name = new JLabel();
splitPane3 = new JSplitPane();
label_gender = new JLabel();
splitPane4 = new JSplitPane();
button_addCar = new JButton();
splitPane5 = new JSplitPane();
button_reviseCar = new JButton();
splitPane6 = new JSplitPane();
button_findCar = new JButton();
splitPane7 = new JSplitPane();
button_removeCar = new JButton();
splitPane8 = new JSplitPane();
label1 = new JLabel();
splitPane9 = new JSplitPane();
button_turnover = new JButton();
splitPane10 = new JSplitPane();
button_exportInformation = new JButton();
splitPane11 = new JSplitPane();
button_exitSystem = new JButton();
textField1 = new JTextField();
Menu = new JTabbedPane();
scrollPane1 = new JScrollPane();
table_garage = new JTable();
scrollPane2 = new JScrollPane();
table_users = new JTable();
//======== Administrator ========
{
Administrator.setMinimumSize(new Dimension(1000, 600));
Administrator.setVisible(true);
Administrator.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Administrator.setResizable(false);
Administrator.setIconImage(new ImageIcon(getClass().getResource("/huang/wen/source/logo_white.png")).getImage());
var AdministratorContentPane = Administrator.getContentPane();
AdministratorContentPane.setLayout(new BorderLayout());
//======== panel1 ========
{
panel1.setLayout(new BorderLayout());
//======== splitPane1 ========
{
splitPane1.setDividerLocation(200);
//======== splitPane2 ========
{
splitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_name ----
label_name.setText(bundle.getString("Administrator.label_name.text"));
label_name.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
splitPane2.setTopComponent(label_name);
//======== splitPane3 ========
{
splitPane3.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label_gender ----
label_gender.setText(bundle.getString("Administrator.label_gender.text"));
label_gender.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
splitPane3.setTopComponent(label_gender);
//======== splitPane4 ========
{
splitPane4.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_addCar ----
button_addCar.setText(bundle.getString("Administrator.button_addCar.text"));
button_addCar.setPreferredSize(new Dimension(150, 36));
button_addCar.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_addCar.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/New vehicle.png")));
button_addCar.setMinimumSize(new Dimension(150, 36));
button_addCar.setMaximumSize(new Dimension(150, 36));
splitPane4.setTopComponent(button_addCar);
//======== splitPane5 ========
{
splitPane5.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_reviseCar ----
button_reviseCar.setText(bundle.getString("Administrator.button_reviseCar.text"));
button_reviseCar.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_reviseCar.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Modify vehicle information.png")));
button_reviseCar.setMaximumSize(new Dimension(150, 36));
button_reviseCar.setPreferredSize(new Dimension(150, 36));
button_reviseCar.setMinimumSize(new Dimension(150, 36));
splitPane5.setTopComponent(button_reviseCar);
//======== splitPane6 ========
{
splitPane6.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_findCar ----
button_findCar.setText(bundle.getString("Administrator.button_findCar.text"));
button_findCar.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_findCar.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Inquire.png")));
button_findCar.setMaximumSize(new Dimension(150, 36));
button_findCar.setMinimumSize(new Dimension(150, 36));
button_findCar.setPreferredSize(new Dimension(150, 36));
splitPane6.setTopComponent(button_findCar);
//======== splitPane7 ========
{
splitPane7.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_removeCar ----
button_removeCar.setText(bundle.getString("Administrator.button_removeCar.text"));
button_removeCar.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_removeCar.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Remove.png")));
button_removeCar.setPreferredSize(new Dimension(150, 36));
button_removeCar.setMinimumSize(new Dimension(150, 36));
button_removeCar.setMaximumSize(new Dimension(150, 36));
splitPane7.setTopComponent(button_removeCar);
//======== splitPane8 ========
{
splitPane8.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- label1 ----
label1.setText(bundle.getString("Administrator.label1.text"));
splitPane8.setTopComponent(label1);
//======== splitPane9 ========
{
splitPane9.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_turnover ----
button_turnover.setText(bundle.getString("Administrator.button_turnover.text"));
button_turnover.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_turnover.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Turnover.png")));
button_turnover.setMaximumSize(new Dimension(150, 36));
button_turnover.setMinimumSize(new Dimension(150, 36));
button_turnover.setPreferredSize(new Dimension(150, 36));
splitPane9.setTopComponent(button_turnover);
//======== splitPane10 ========
{
splitPane10.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_exportInformation ----
button_exportInformation.setText(bundle.getString("Administrator.button_exportInformation.text"));
button_exportInformation.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_exportInformation.setIcon(new ImageIcon(getClass().getResource("/huang/wen/source/Export.png")));
button_exportInformation.setPreferredSize(new Dimension(150, 36));
button_exportInformation.setMinimumSize(new Dimension(150, 36));
button_exportInformation.setMaximumSize(new Dimension(150, 36));
splitPane10.setTopComponent(button_exportInformation);
//======== splitPane11 ========
{
splitPane11.setOrientation(JSplitPane.VERTICAL_SPLIT);
//---- button_exitSystem ----
button_exitSystem.setText(bundle.getString("Administrator.button_exitSystem.text"));
button_exitSystem.setFont(new Font("\u534e\u6587\u4e2d\u5b8b", Font.PLAIN, 16));
button_exitSystem.setMaximumSize(new Dimension(150, 30));
button_exitSystem.setPreferredSize(new Dimension(150, 30));
button_exitSystem.setMinimumSize(new Dimension(150, 30));
splitPane11.setTopComponent(button_exitSystem);
//---- textField1 ----
textField1.setText(bundle.getString("Administrator.textField1.text"));
textField1.setHorizontalAlignment(SwingConstants.CENTER);
textField1.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 12));
textField1.setEditable(false);
textField1.setMinimumSize(new Dimension(150, 30));
textField1.setMaximumSize(new Dimension(150, 100000));
textField1.setPreferredSize(new Dimension(150, 30));
textField1.setRequestFocusEnabled(false);
splitPane11.setBottomComponent(textField1);
}
splitPane10.setBottomComponent(splitPane11);
}
splitPane9.setBottomComponent(splitPane10);
}
splitPane8.setBottomComponent(splitPane9);
}
splitPane7.setBottomComponent(splitPane8);
}
splitPane6.setBottomComponent(splitPane7);
}
splitPane5.setBottomComponent(splitPane6);
}
splitPane4.setBottomComponent(splitPane5);
}
splitPane3.setBottomComponent(splitPane4);
}
splitPane2.setBottomComponent(splitPane3);
}
splitPane1.setLeftComponent(splitPane2);
//======== Menu ========
{
//======== scrollPane1 ========
{
scrollPane1.setViewportView(table_garage);
}
Menu.addTab("\u8f66\u5e93", scrollPane1);
//======== scrollPane2 ========
{
scrollPane2.setViewportView(table_users);
}
Menu.addTab("\u7528\u6237", scrollPane2);
}
splitPane1.setRightComponent(Menu);
}
panel1.add(splitPane1, BorderLayout.CENTER);
}
AdministratorContentPane.add(panel1, BorderLayout.CENTER);
Administrator.pack();
Administrator.setLocationRelativeTo(Administrator.getOwner());
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
//初始化管理员信息(首次刷新)
RefreshUserInformation(bundle);
//获取所有车辆信息并显示
DisplayVehicles(conn);
//获取所有用户的信息并显示
DisplayUserInformation(conn);
//新增车辆
button_addCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
VehicleFactory vehicleFactory = new VehicleFactory();
String newVehicle = JOptionPane.showInputDialog(Administrator, "请输入汽车类型\n[轿车、客车、货车]", "新增车辆——汽车类型",JOptionPane.PLAIN_MESSAGE);
if(newVehicle instanceof String) {
MotoVehicle Vehicle = vehicleFactory.getVehicleByType(newVehicle);
if (Vehicle == null) {
JOptionPane.showMessageDialog(Administrator, "目前仅支持轿车、客车、货车三种车型", "添加失败", JOptionPane.ERROR_MESSAGE);
} else {
Vehicle.AddProcess(Administrator);
DisplayVehicles(conn);
}
}
}
});
//修改车辆信息
button_reviseCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = -1;
if (Menu.getSelectedIndex() == 0) {
index = table_garage.getSelectedRow();
if (index != -1) {
String type_temp = table_garage.getValueAt(index, 0).toString();
String number_temp = table_garage.getValueAt(index, 1).toString();
String brand_temp = table_garage.getValueAt(index, 2).toString();
if (table_garage.getValueAt(index, 5).toString().equals("暂未租出")) {
String sql_update;
PreparedStatement pstmt = null;
try {
if (type_temp.equals("轿车")) {
sql_update = "update cars set vehicleId=?, brand=?, type=?, rent=? where vehicleId=?";
String str_vehicleId = JOptionPane.showInputDialog(Administrator, "请输入新的车牌号", "修改车牌号", JOptionPane.PLAIN_MESSAGE);
if (str_vehicleId instanceof String) {
String str_brand = JOptionPane.showInputDialog(Administrator, "请输入修改之后的汽车品牌", "修改汽车品牌", JOptionPane.PLAIN_MESSAGE);
if (str_brand instanceof String) {
String str_type = JOptionPane.showInputDialog(Administrator, "请输入修改之后的汽车型号", "修改汽车型号", JOptionPane.PLAIN_MESSAGE);
if (str_type instanceof String) {
String str_rent = JOptionPane.showInputDialog(Administrator, "请输入修改之后的单价(每日租金)", "修改每日租金", JOptionPane.PLAIN_MESSAGE);
if (str_rent instanceof String) {
Object[] options = {"确定", "取消"};
int temp = JOptionPane.showOptionDialog(Administrator, "确定对车牌号为:" + number_temp + "的" + brand_temp + type_temp + "进行修改吗?", "修改确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
if (temp == 0) {
pstmt = (PreparedStatement) conn.prepareStatement(sql_update);
pstmt.setString(1, str_vehicleId);
pstmt.setString(2, str_brand);
pstmt.setString(3, str_type);
pstmt.setFloat(4, Integer.parseInt(str_rent));
pstmt.setString(5, number_temp);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功修改原车牌号为\"" + number_temp + "\"的汽车", "修改成功", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
} else if (type_temp.equals("客车")) {
sql_update = "update buses set vehicleId=?, brand=?, seat=?, rent=? where vehicleId=?";
String str_vehicleId = JOptionPane.showInputDialog(Administrator, "请输入新的车牌号", "修改车牌号", JOptionPane.PLAIN_MESSAGE);
if (str_vehicleId instanceof String) {
String str_brand = JOptionPane.showInputDialog(Administrator, "请输入修改之后的客车品牌", "修改客车品牌", JOptionPane.PLAIN_MESSAGE);
if (str_brand instanceof String) {
String str_seat = JOptionPane.showInputDialog(Administrator, "请输入修改之后的座位数", "修改客车载客量", JOptionPane.PLAIN_MESSAGE);
if (str_seat instanceof String) {
String str_rent = JOptionPane.showInputDialog(Administrator, "请输入修改之后的单价(每日租金)", "修改每日租金", JOptionPane.PLAIN_MESSAGE);
if (str_rent instanceof String) {
Object[] options = {"确定", "取消"};
int temp = JOptionPane.showOptionDialog(Administrator, "确定对车牌号为:" + number_temp + "的" + brand_temp + type_temp + "座进行修改吗?", "修改确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
if (temp == 0) {
pstmt = (PreparedStatement) conn.prepareStatement(sql_update);
pstmt.setString(1, str_vehicleId);
pstmt.setString(2, str_brand);
pstmt.setInt(3, Integer.parseInt(str_seat));
pstmt.setFloat(4, Integer.parseInt(str_rent));
pstmt.setString(5, number_temp);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功修改原车牌号为\"" + number_temp + "\"的汽车", "修改成功", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
} else if (type_temp.equals("货车")) {
sql_update = "update trucks set vehicleId=?, brand=?, tonnage=?, rent=? where vehicleId=?";
String str_vehicleId = JOptionPane.showInputDialog(Administrator, "请输入新的车牌号", "修改车牌号", JOptionPane.PLAIN_MESSAGE);
if (str_vehicleId instanceof String) {
String str_brand = JOptionPane.showInputDialog(Administrator, "请输入修改之后的货车品牌", "修改货车品牌", JOptionPane.PLAIN_MESSAGE);
if (str_brand instanceof String) {
String str_tonnage = JOptionPane.showInputDialog(Administrator, "请输入修改之后的货车吨位", "修改货车载重", JOptionPane.PLAIN_MESSAGE);
if (str_tonnage instanceof String) {
String str_rent = JOptionPane.showInputDialog(Administrator, "请输入修改之后的单价(每日租金)", "修改每日租金", JOptionPane.PLAIN_MESSAGE);
if (str_rent instanceof String) {
Object[] options = {"确定", "取消"};
int temp = JOptionPane.showOptionDialog(Administrator, "确定对车牌号为:" + number_temp + "的" + brand_temp + type_temp + "进行修改吗?", "修改确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
if (temp == 0) {
pstmt = (PreparedStatement) conn.prepareStatement(sql_update);
pstmt.setString(1, str_vehicleId);
pstmt.setString(2, str_brand);
pstmt.setFloat(3, Integer.parseInt(str_tonnage));
pstmt.setFloat(4, Integer.parseInt(str_rent));
pstmt.setString(5, number_temp);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功修改原车牌号为\"" + number_temp + "\"的汽车", "修改成功", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
}
DisplayVehicles(conn);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
} else {
JOptionPane.showMessageDialog(Administrator, "该汽车正在外租中,暂时不能修改!", "修改失败", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(Administrator, "请在右方选择需要修改的汽车!", "修改失败", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(Administrator, "请在右方选择需要修改的汽车!", "修改失败", JOptionPane.WARNING_MESSAGE);
}
}
});
//查询车辆信息
button_findCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str_input = JOptionPane.showInputDialog(Administrator, "请输入你希望查询车辆的车牌号", "查询车辆信息", JOptionPane.PLAIN_MESSAGE);
if (str_input instanceof String) {
ResultSet res = null;
int flag = 0;//校验查询情况
PreparedStatement pstmt = null;
if (flag == 0) {
try {
String sql = "select * from cars where vehicleId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, str_input);
res = pstmt.executeQuery();
if (res.next()) {
flag = 1;
String flag_temp = res.getString("flag");
String vehicleId_temp = res.getString("vehicleId");
String brand_temp = res.getString("brand");
String type_temp = res.getString("type");
float rent_temp = res.getFloat("rent");
String date_temp = res.getString("date");
if (flag_temp.equals("1")) {
flag_temp = "暂未租出";
} else {
flag_temp = "已被" + res.getString("flag") + "租用";
}
if (date_temp.equals("0")) {
date_temp = "";
} else {
date_temp = "\n租用时间:" + res.getString("date");
}
JOptionPane.showMessageDialog(Administrator,
"查询到如下信息:\n车牌号:" + vehicleId_temp + "\n汽车类型:轿车\n汽车品牌:" + brand_temp + "\n汽车类型:" + type_temp + "\n每日租金:" + rent_temp + "\n租用情况:" + flag_temp + date_temp,
"查询成功", JOptionPane.PLAIN_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
if (flag == 0) {
try {
String sql = "select * from buses where vehicleId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, str_input);
res = pstmt.executeQuery();
if (res.next()) {
flag = 1;
String flag_temp = res.getString("flag");
String vehicleId_temp = res.getString("vehicleId");
String brand_temp = res.getString("brand");
int seat_temp = res.getInt("seat");
float rent_temp = res.getFloat("rent");
String date_temp = res.getString("date");
if (flag_temp.equals("1")) {
flag_temp = "暂未租出";
} else {
flag_temp = "已被" + res.getString("flag") + "租用";
}
if (date_temp.equals("0")) {
date_temp = "";
} else {
date_temp = "\n租用时间:" + res.getString("date");
}
JOptionPane.showMessageDialog(Administrator,
"查询到如下信息:\n车牌号:" + vehicleId_temp + "\n汽车类型:客车\n汽车品牌:" + brand_temp + "\n汽车类型:" + seat_temp + "\n每日租金:" + rent_temp + "\n租用情况:" + flag_temp + date_temp,
"查询成功", JOptionPane.PLAIN_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
if (flag == 0) {
try {
String sql = "select * from trucks where vehicleId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, str_input);
res = pstmt.executeQuery();
if (res.next()) {
flag = 1;
String flag_temp = res.getString("flag");
String vehicleId_temp = res.getString("vehicleId");
String brand_temp = res.getString("brand");
float tonnage_temp = res.getFloat("tonnage");
float rent_temp = res.getFloat("rent");
String date_temp = res.getString("date");
if (flag_temp.equals("1")) {
flag_temp = "暂未租出";
} else {
flag_temp = "已被" + res.getString("flag") + "租用";
}
if (date_temp.equals("0")) {
date_temp = "";
} else {
date_temp = "\n租用时间:" + res.getString("date");
}
JOptionPane.showMessageDialog(Administrator,
"查询到如下信息:\n车牌号:" + vehicleId_temp + "\n汽车类型:货车\n汽车品牌:" + brand_temp + "\n汽车类型:" + tonnage_temp + "\n每日租金:" + rent_temp + "\n租用情况:" + flag_temp + date_temp,
"查询成功", JOptionPane.PLAIN_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
if (flag == 0) {
JOptionPane.showMessageDialog(Administrator, "未在车库中找到车牌号为\"" + str_input + "\"的汽车\n以下原因会导致这种情况:\n1.输入的车牌号有误\n2.车库中不存在该车辆", "查询失败", JOptionPane.WARNING_MESSAGE);
}
}
}
});
//移除车辆
button_removeCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = -1;
if (Menu.getSelectedIndex() == 0) {
index = table_garage.getSelectedRow();
if (index != -1) {
String type_temp = table_garage.getValueAt(index, 0).toString();
String number_temp = table_garage.getValueAt(index, 1).toString();
String brand_temp = table_garage.getValueAt(index, 2).toString();
if(table_garage.getValueAt(index, 5).toString().equals("暂未租出")) {
Object[] options = {"确定", "取消"};
int temp = JOptionPane.showOptionDialog(Administrator, "确定移除车牌号为:" + number_temp + "的" + brand_temp + type_temp + "吗?", "移除确认", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
if (temp == 0) {
String sql_1 = "delete from cars where vehicleId = ?";
String sql_2 = "delete from buses where vehicleId = ?";
String sql_3 = "delete from trucks where vehicleId = ?";
PreparedStatement pstmt = null;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql_1);
pstmt.setString(1, number_temp);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_2);
pstmt.setString(1, number_temp);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_3);
pstmt.setString(1, number_temp);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功将\"" + number_temp + "\"移除", "移除成功", JOptionPane.INFORMATION_MESSAGE);
DisplayVehicles(conn);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
}else{
JOptionPane.showMessageDialog(Administrator, "该汽车正在外租中,暂时不能移除!", "移除失败", JOptionPane.ERROR_MESSAGE);
}
} else {
//提示用户输入车牌号
String str_input = JOptionPane.showInputDialog(Administrator, "请输入你希望移除车辆的车牌号", "移除指定车辆", JOptionPane.PLAIN_MESSAGE);
if(str_input instanceof String) {
String sql_1 = "delete from cars where vehicleId = ?";
String sql_2 = "delete from buses where vehicleId = ?";
String sql_3 = "delete from trucks where vehicleId = ?";
PreparedStatement pstmt = null;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql_1);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_2);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_3);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功将\"" + str_input + "\"移除", "移除成功", JOptionPane.INFORMATION_MESSAGE);
DisplayVehicles(conn);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
}
} else {
//提示用户输入车牌号
String str_input = JOptionPane.showInputDialog(Administrator, "请输入你希望移除车辆的车牌号", "移除指定车辆", JOptionPane.PLAIN_MESSAGE);
if(str_input instanceof String) {
String sql_1 = "delete from cars where vehicleId = ?";
String sql_2 = "delete from buses where vehicleId = ?";
String sql_3 = "delete from trucks where vehicleId = ?";
PreparedStatement pstmt = null;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql_1);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_2);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
pstmt = (PreparedStatement) conn.prepareStatement(sql_3);
pstmt.setString(1, str_input);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(Administrator, "已成功将\"" + str_input + "\"移除", "移除成功", JOptionPane.INFORMATION_MESSAGE);
DisplayVehicles(conn);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
mysqlTool.close(pstmt);
}
}
}
}
});
//营业额
button_turnover.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Administrator.dispose();
new Turnover(account_administrator);
}
});
//导出用户信息
button_exportInformation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Long timeStamp = System.currentTimeMillis(); //获取当前时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_date = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); //时间戳转化为时间
//选择文件保存路径
File file = null;
JFileChooser fileChooser = new JFileChooser(".\\"); //默认保存位置
// 设置打开文件选择框后默认输入的文件名
fileChooser.setSelectedFile(new File("用户信息表.html"));
// 打开文件选择框
int result = fileChooser.showSaveDialog(Administrator);
if (result == JFileChooser.APPROVE_OPTION) {
// 如果点击了"保存", 则获取选择的保存路径
file = fileChooser.getSelectedFile();
}
//写入中文字符时解决中文乱码问题
if (file != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(file.getAbsolutePath()));
OutputStreamWriter osw = null;
osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
//写入文件
bw.write("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta charset=\"UTF - 8\">\n" +
"<title>用户信息</title>\n" +
"</head>\n" +
"<body>\n" +
"<hr/>\n" +
"<br/>\n" +
"<h1 align=\"center\" style=\"font - size:30pt\">QuantumCloud汽车租赁系统</h1>\n" +
"<hr/>\n" +
"<h2 align=\"center\" style=\"font - size:10pt\">导出时间:" + current_date + "</h2>\n" +
"<br/>\n" +
"<table border=\"1\" align=\"center\" cellpadding=\"8\" cellspacing=\"0\">\n" +
"<tr align=\"center\" bgcolor=\"CC0000\">\n" +
"<th colspan=\"6\" style=\"color:white\">用户信息表</th>\n" +
"<tr/>\n" +
"<tr align=\"center\" bgcolor=\"#FAEBD7\">\n" +
" <th rowspan=\"1\">序号</th>\n" +
" <th rowspan=\"1\">用户账号</th>\n" +
" <th rowspan=\"1\">姓名</th>\n" +
" <th rowspan=\"1\">性别</th>\n" +
" <th rowspan=\"1\">租车情况</th>\n" +
" <th rowspan=\"1\">账户余额</th>\n" +
"</tr>" + "\t\n");
//循环打印用户信息
for (int i = 0; i < users.size(); i++) {
bw.write("<tr align=\"center\" bgcolor=\"#FAEBD7\">" + "\t\n");
bw.write("<th rowspan=\"1\">" + (i + 1) + "</th>" + "\t\n");
bw.write("<th rowspan=\"1\">" + users.get(i).getAccount() + "</th>" + "\t\n");
bw.write("<th rowspan=\"1\">" + users.get(i).getName() + "</th>" + "\t\n");
bw.write("<th rowspan=\"1\">" + users.get(i).getGender() + "</th>" + "\t\n");
if (users.get(i).getIs_rent().equals("0")) {
bw.write("<th rowspan=\"1\">暂未租车</th>" + "\t\n");
} else {
bw.write("<th rowspan=\"1\">已租用\"" + users.get(i).getIs_rent() + "\"</th>" + "\t\n");
}
bw.write("<th rowspan=\"1\">" + users.get(i).getBalance() + "</th>" + "\t\n");
bw.write("</tr>" + "\t\n");
}
bw.write("</table>\n" +
"</body>\n" +
"</html>" + "\t\n");
//先打开的后关闭,后打开的先关闭
bw.close();
osw.close();
fos.close();
JOptionPane.showMessageDialog(Administrator, "导出成功!已保存至\n"+file, "导出成功", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(Administrator, "发生一些错误\n导出失败!", "导出失败", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
}
});
//退出系统
button_exitSystem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mysqlTool.close(conn); //关闭数据库连接
Administrator.setVisible(false);
exit(0);
}
});
}
总结
虽说开发这个系统不算难,但也还是小结一下吧。首先是数据表的设计,由于开发此系统时,还没正式学过数据库相关的知识,所以数据库的逻辑设计得不是很合理,由于使用的全是基础的SQL语句,因此代码中与数据库有关操作的代码也还能进行再简化。
以上就是本次项目的全部内容,需要交流或者免费获取代码请关注微信公众号:coding加油站,回复“java租车”获取