JavaSwing汽车租赁系统
时代在发展潮流在进步,个人的私家车已经逐渐的不能满足人们出行的要求
所以汽车租赁公司应运而生,越来越多的人选择租车来满足自己外出游玩的乐趣和兴趣,所以相应的租车系统就会产生。
实现功能截图
登录页面:
管理员页面:
车辆信息录入页面:
车辆详细信息:
车辆信息修改页面:
用户界面:
用户详细信息页面:
系统流程图
本系统主要完成前台浏览(用户注册,信息修改,车辆浏览),后台管理(车辆信息的录入,管理员登录)等功能。通过以上功能实现简单,便捷的租车系统。
功能流程图:
使用技术
数据库:mysql
开发工具:Eclipse(Myeclispe、Idea也可以)
知识点:JavaSwing
代码
注册
Zhuce.java
package Index;
import java.awt.FlowLayout;
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.ImageIcon;
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 javax.swing.text.PasswordView;
import com.mysql.jdbc.PreparedStatement;
public class Zhuce extends JFrame implements ActionListener {
JTextField field1;
JPasswordField field2,field3;
JButton buttonOfZhuce,buttonOfFanhui;
Box box1,box2,box3,box4,baseBox;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public Zhuce()
{
setLayout(new FlowLayout());
init();
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 500, 450);
setTitle("注册界面");
}
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);
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(200));
baseBox.add(box4);
add(baseBox);
}
public void connDB() { // 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {//创建连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/car_rental", "root", "");
stmt = con.createStatement();
} 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();
String username = null;
String user_password = null;
int success = 1;//如果已经存在了此账号,则赋值为0,作为进入创建账号方法的进入条件
if(source == buttonOfZhuce)
{
if (field1.getText().equals("") || field2.getText().equals("")||field3.getText().equals("") )
{// 判断是否输入了用户名和密码
JOptionPane.showMessageDialog(null, "请填写完整!");
}
else//密码账号都不为空后
{
try {
this.connDB();
String sql = "select * from user where username ='"+field1.getText()+"'";
rs = stmt.executeQuery(sql);
if(rs.next())
{
JOptionPane.showMessageDialog(null, "此账号已经存在!");
field1.setText("");
success = 0;
this.closeDB();
}
} catch (HeadlessException e2) {
e2.printStackTrace();
} catch (SQLException e2) {
e2.printStackTrace();
}//这个trycatch模块用来判断 创建账号之前数据库有没有此账号!,如果有 success = 0,就进入不了下面创建账号的语句
if(field2.getText().equals(field3.getText())&&success==1)
{
String str = "insert into user values('" + field1.getText() + "','" +field2.getText()+"')";
try {
stmt.executeUpdate(str);
} catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "注册成功!");
this.dispose();
new Login();
}
else
{
this.closeDB();
if(success == 1)
{
JOptionPane.showMessageDialog(null,"两次输入的密码不匹配!" );
}
}
}
}
else if(source == buttonOfFanhui)
{
this.dispose();
new Login();
}
}
}
登录
Login.java
package Index;
import java.awt.FlowLayout;
import java.sql.*;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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 Guanliyuan.*;
import Yonghu.*;
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);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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 connDB() { // 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {//创建连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/car_rental", "root", "");
} 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();
String username = null;
String adminname = null;
String admin_password = null;
String user_password = null;
if (source == login_button)//如果点击的是登陆按钮,就会判断radiobutton选择的是什么,做出相应的响应
{
if(!yonghu_radioButton.isSelected()&&!guanliyuan_radioButton.isSelected())//radiobutton没选择
{
JOptionPane.showMessageDialog(null, "请选择身份!");
}
else if (zhanghao_field.getText().equals("") || mima_field.getText().equals(""))
{// 判断是否输入了用户名和密码
JOptionPane.showMessageDialog(null, "登录名和密码不能为空!");
}
else
{
this.connDB();
try
{
stmt = con.createStatement();
}
catch (SQLException e2)
{
e2.printStackTrace();
}
if(guanliyuan_radioButton.isSelected())//如果选择的是管理员的按钮
{
try {
String sql ="select * from admin where adminname ='"+zhanghao_field.getText()+"'";
rs = stmt.executeQuery(sql);
if(rs.next())
{
adminname = rs.getString(1);
admin_password = rs.getString(2);
if(!mima_field.getText().equals(admin_password))
{
JOptionPane.showMessageDialog(null, "密码错误!");
mima_field.setText("");
}
else
{
this.dispose();
new Guanliyuan(false);
}
}
else
{
JOptionPane.showMessageDialog(null, "此账号没有管理员权限!");
zhanghao_field.setText("");
mima_field.setText("");
}
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
else //选择的是user按钮
{
try {
String sql ="select * from user where username ='"+zhanghao_field.getText()+"'";
rs = stmt.executeQuery(sql);
if(rs.next())
{
username = rs.getString(1);
user_password = rs.getString(2);
if(!mima_field.getText().equals(user_password))
{
JOptionPane.showMessageDialog(null, "密码错误!");
mima_field.setText("");
}
else
{
this.dispose();
new Yonghuduan(zhanghao_field.getText());
}
}
else
{
JOptionPane.showMessageDialog(null, "不存在此账号,请注册!");
zhanghao_field.setText("");
mima_field.setText("");
}
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
else if(source == zhuce_button)
{
this.dispose();
new Zhuce();
}
}
}
管理员
GunaLiYuan.java
package Guanliyuan;
import java.awt.BorderLayout;
import Yonghu.*;
import java.awt.Container;
import java.awt.FlowLayout;
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 java.text.FieldPosition;
import javax.naming.InitialContext;
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.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;
import Index.*;
public class Guanliyuan extends JFrame implements ActionListener{
/*
*
* 管理员端的界面
*/
JTable table;
JLabel label1,label2,label3,label4;
Object a[][];
Object name[] = {"编号","车型","车主","价格(元/天)","颜色","是否被租用","租用的用户"};
JButton buttonOfXinxiluru,buttonOfXinxiliulan,buttonOfDelete,buttonOfLogout,buttonOfXiangXi,buttonOfXiugai;
Box box1,box2;
JTextField field,field2,field3;
JPanel jPanel4,jPanel5;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public Guanliyuan(Boolean success)
{
init();
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 625, 490);
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);
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("待查询详情的编号:");
field = new JTextField();
field2 = new JTextField();
field3 = 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(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中。
// jPanel1 = new JPanel();
// jPanel2 = new JPanel();
// jPanel3 = new JPanel();
// jPanel4 = new JPanel();
// jPanel5 = new JPanel();
//
// jPanel1.setLayout(new BorderLayout());
// jPanel1.add(label2,BorderLayout.NORTH);
// jPanel1.add(field,BorderLayout.CENTER);
// jPanel1.add(buttonOfDelete,BorderLayout.SOUTH);//把删除的模块放一个jpanel1里
//
// jPanel2.setLayout(new BorderLayout());
// jPanel2.add(jPanel1,BorderLayout.NORTH);
// jPanel2.add(buttonOfLogout,BorderLayout.CENTER);//把删除模块和 退出登录模块放一个jpanel2里
//
// jPanel3.setLayout(new BorderLayout());
// jPanel3.add(buttonOfXinxiluru,BorderLayout.NORTH);
// jPanel3.add(buttonOfXinxiliulan,BorderLayout.CENTER);//信息录入浏览按钮放到jpanel3里
//
// jPanel4.setLayout(new BorderLayout());
// jPanel4.add(jPanel3,BorderLayout.NORTH);
// jPanel4.add(jPanel2,BorderLayout.CENTER);//把jpanel 2 3 放到 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("jdbc:mysql://localhost:3306/car_rental", "root", "");
} 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_information";
rs = stmt.executeQuery(sql);
while(rs.next())
{
String number = rs.getString("number");
String cartype = rs.getString("cartype");
String carower = rs.getString("carower");
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) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.closeDB();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == buttonOfXinxiluru)//点击信息修改按钮
{
this.dispose();
new Luru();
}
else if(source == buttonOfXinxiliulan)//点击信息浏览按钮
{
xinXiLiuLan();
}
else if(source == buttonOfXiugai)//点击修改按钮
{
if(field2.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "输入修改车型的编号!");
}
else
{
this.dispose();
new Xiugai(field2.getText());
}
}
else if(source == buttonOfXiangXi)//点击详细信息按钮
{
this.dispose();
new Xiangxi(field3.getText());
}
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();
}
else
{
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();
}
}
// 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();
}
else if(n == JOptionPane.NO_OPTION)
{
}
}
else
{
JOptionPane.showMessageDialog(null, "没有此编号的车辆信息!");
}
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
else if(source == buttonOfLogout)//退出
{
this.dispose();
new Login();
}
}
}
用户端
Yonghuduan.java
package Yonghu;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
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 java.text.FieldPosition;
import javax.naming.InitialContext;
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.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;
import org.omg.PortableInterceptor.SUCCESSFUL;
import Index.*;
public class Yonghuduan extends JFrame implements ActionListener{
/*
*
* 用户端的界面
*/
JTable table;
JLabel label1,label2,label3;
Object a[][];
Object name[] = {"序号","车型","车主","价格","颜色","是否被租用"};
JButton buttonOfKe,buttonOfXinxiliulan,buttonOfQyueding,buttonOfLogout,buttonOfXiangXi,buttonOfWo;
Box box1,box2;
JTextField field,field2;
JPanel jPanel4,jPanel5;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String username =null;
public Yonghuduan(String username)
{
init();
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 640, 420);
setTitle("用户界面");
this.username = username;
}
void init()
{
label1 = new JLabel("汽车租赁信息浏览系统");
buttonOfKe = new JButton(" 可 租 用 车 辆 ");
buttonOfKe.addActionListener(this);
buttonOfXinxiliulan = new JButton(" 汽车信息浏览 ");
buttonOfXinxiliulan .addActionListener(this);
buttonOfQyueding = new JButton(" 确 定 ");
buttonOfQyueding.addActionListener(this);
buttonOfLogout = new JButton(" 退 出 登 录 ");
buttonOfLogout.addActionListener(this);
buttonOfXiangXi = new JButton(" 详 细 信 息 ");
buttonOfXiangXi.addActionListener(this);
buttonOfWo = new JButton(" 我 租 的 车 ");
buttonOfWo.addActionListener(this);
label2 = new JLabel("输入需要租用的汽车:");
label3 = new JLabel("查询详细的信息编号");
field = new JTextField();
field2 = new JTextField();
a = new Object[50][6];
table = new JTable(a, name);//组件的创建
table.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(table);
box1 = Box.createVerticalBox();
box1.add(Box.createVerticalStrut(20));
box1.add(buttonOfKe);
box1.add(Box.createVerticalStrut(10));
box1.add(buttonOfXinxiliulan);
box1.add(Box.createVerticalStrut(15));
box1.add(label2);
box1.add(Box.createVerticalStrut(5));
box1.add(field);
box1.add(Box.createVerticalStrut(5));
box1.add(buttonOfQyueding);
box1.add(Box.createVerticalStrut(15));
box1.add(label3);
box1.add(Box.createVerticalStrut(5));
box1.add(field2);
box1.add(Box.createVerticalStrut(5));
box1.add(buttonOfXiangXi);
box1.add(Box.createVerticalStrut(20));
box1.add(buttonOfWo);
box1.add(Box.createVerticalStrut(10));
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中。
// jPanel1 = new JPanel();
// jPanel2 = new JPanel();
// jPanel3 = new JPanel();
// jPanel4 = new JPanel();
// jPanel5 = new JPanel();
//
// jPanel1.setLayout(new BorderLayout());
// jPanel1.add(label2,BorderLayout.NORTH);
// jPanel1.add(field,BorderLayout.CENTER);
// jPanel1.add(buttonOfDelete,BorderLayout.SOUTH);//把删除的模块放一个jpanel1里
//
// jPanel2.setLayout(new BorderLayout());
// jPanel2.add(jPanel1,BorderLayout.NORTH);
// jPanel2.add(buttonOfLogout,BorderLayout.CENTER);//把删除模块和 退出登录模块放一个jpanel2里
//
// jPanel3.setLayout(new BorderLayout());
// jPanel3.add(buttonOfXinxiluru,BorderLayout.NORTH);
// jPanel3.add(buttonOfXinxiliulan,BorderLayout.CENTER);//信息录入浏览按钮放到jpanel3里
//
// jPanel4.setLayout(new BorderLayout());
// jPanel4.add(jPanel3,BorderLayout.NORTH);
// jPanel4.add(jPanel2,BorderLayout.CENTER);//把jpanel 2 3 放到 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("jdbc:mysql://localhost:3306/car_rental?characterEncoding=utf-8", "root", "");
} 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]=" ";
i++;
}
i=0;
this.connDB();
try {
stmt = con.createStatement();
String sql= "select * from car_information";
rs = stmt.executeQuery(sql);
while(rs.next())
{
String number = rs.getString("number");
String cartype = rs.getString("cartype");
String carower = rs.getString("carower");
String price = rs.getString("price");
String color = rs.getString("color");
String hire= rs.getString("hire");
a[i][0]=number;
a[i][1]=cartype;
a[i][2]=carower;
a[i][3]=price;
a[i][4]=color;
a[i][5]=hire;
i++;
}
this.closeDB();
repaint();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.closeDB();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == buttonOfKe)//点击可以租用车辆的按钮
{
int success = 0;//用来判断 有没有 没被租用的车辆,如果能返回 “否” 值的集,则赋值为 1
int i=0;
while(i<50)
{
a[i][0]=" ";
a[i][1]=" ";
a[i][2]=" ";
a[i][3]=" ";
a[i][4]=" ";
a[i][5]=" ";
i++;
}
i=0;//i 赋值为 0,为下面的循环做准备
this.connDB();
try {
stmt = con.createStatement();
String sql= "select * from car_information where hire= '否';";//查询表里面 没被租用的车辆
rs = stmt.executeQuery(sql);
while(rs.next())//把查询到的信息写入 table
{
String number = rs.getString("number");
String cartype = rs.getString("cartype");
String carower = rs.getString("carower");
String price = rs.getString("price");
String color = rs.getString("color");
String hire = rs.getString("hire");
a[i][0]=number;
a[i][1]= cartype;
a[i][2]=carower;
a[i][3]=price;
a[i][4]=color;
a[i][5]=hire;
i++;
success = 1;
}
this.closeDB();
repaint();//刷新一下
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(success == 0)
{
JOptionPane.showMessageDialog(null, "都已经被租用!");
}
}
else if(source == buttonOfXinxiliulan)//点击信息浏览按钮
{
int i=0;
while(i<50)
{
a[i][0]=" ";
a[i][1]=" ";
a[i][2]=" ";
a[i][3]=" ";
a[i][4]=" ";
a[i][5]=" ";
i++;
}
i=0;
this.connDB();
try {
stmt = con.createStatement();
String sql= "select * from car_information";
rs = stmt.executeQuery(sql);
while(rs.next())
{
String number = rs.getString("number");
String cartype = rs.getString("cartype");
String carower = rs.getString("carower");
String price = rs.getString("price");
String color = rs.getString("color");
String hire = rs.getString("hire");
a[i][0]=number;
a[i][1]= cartype;
a[i][2]=carower;
a[i][3]=price;
a[i][4]=color;
a[i][5]=hire;
i++;
}
this.closeDB();
repaint();
} catch (SQLException e1){
e1.printStackTrace();
}
}
else if (source == buttonOfQyueding)//点击 租用的确定键
{
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()+"' and hire = '否'";//获取输入的 序号并且 没被租用的信息
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)//确认框
{
try
{
stmt = con.createStatement();
String sql2 = "update car_information set hire = '是',username = '"+username+"' where number='"+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 == buttonOfXiangXi)
{
this.dispose();
new Xiangxi_yonghu(field2.getText(),username);
}
else if(source == buttonOfLogout)
{
this.dispose();
new Login();
}
else if(source == buttonOfWo)//点击我租的车辆信息
{
int success = 0;//用来判断 有没有 没自己租用的车辆
int i=0;
while(i<50)
{
a[i][0]=" ";
a[i][1]=" ";
a[i][2]=" ";
a[i][3]=" ";
a[i][4]=" ";
a[i][5]=" ";
i++;
}
i=0;//i 赋值为 0,为下面的循环做准备
this.connDB();
try {
stmt = con.createStatement();
String sql= "select * from car_information where username= '"+username+"';";//查询表里面 自己已经租用的车辆
rs = stmt.executeQuery(sql);
while(rs.next())//把查询到的信息写入 table
{
String number = rs.getString("number");
String cartype = rs.getString("cartype");
String carower = rs.getString("carower");
String price = rs.getString("price");
String color = rs.getString("color");
String hire = rs.getString("hire");
a[i][0]=number;
a[i][1]= cartype;
a[i][2]=carower;
a[i][3]=price;
a[i][4]=color;
a[i][5]=hire;
i++;
success = 1;
}
this.closeDB();
repaint();//刷新一下
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(success == 0)
{
JOptionPane.showMessageDialog(null, "您还没有租用任何车辆!");
}
}
}
}
写在最后
如果运行代码中遇到问题,或者需要完整源码和报告,可以加博主V交流:Code2Life2