基于java+mysql的swing+mysql车租赁管理系统(java+gui+文档)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于java+mysql的Swing+MySQL车租赁管理系统(java+gui+文档)
功能介绍:
登陆界面、管理员界面、用户界面、汽车租赁文档
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);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == buttonOfFanHui)
{
this.dispose();
new Yonghuduan(username);
}
}
}
/*
* 详细详细的管理员编辑页面
*/
public class Xinxitijiao extends JFrame implements ActionListener{
JTextArea area;
Box box1,baseBox,box2,baseBox2;
JButton buttonOfTijiao,buttonOfFanHui;
public Xinxitijiao()
{
init();
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 620, 360);
setTitle("详细信息编辑界面");
}
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull",
"root", "123456");
} 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");
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里
//
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());
} 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;
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(" 删 除 ");
}
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,"两次输入的密码不匹配!" );
}
}
box4.add(field4);
box4.add(box4.createHorizontalStrut(8));
box5 = Box.createHorizontalBox();
box5.add(box5.createHorizontalStrut(8));
box5.add(label5);
box5.add(box5.createHorizontalStrut(8));
box5.add(field5);
box5.add(box5.createHorizontalStrut(8));
box6 = Box.createHorizontalBox();
box6.add(box6.createHorizontalStrut(8));
box6.add(label6);
box6.add(box6.createHorizontalStrut(8));
box6.add(field6);
box6.add(box6.createHorizontalStrut(8));
box7 = Box.createHorizontalBox();
box7.add(box7.createHorizontalStrut(8));
box7.add(buttonOfQueDing);
box7.add(box7.createHorizontalStrut(8));
box7.add(buttonOfQuXIAO);
box7.add(box7.createHorizontalStrut(8));
box7.add(buttonOfReset);
box7.add(box7.createHorizontalStrut(8));
baseBox = Box.createVerticalBox();
baseBox.add(Box.createVerticalStrut(15));
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(box4);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(box5);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(box6);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(box7);
baseBox.add(Box.createVerticalStrut(15));
add(baseBox);
}
public void connDB() { // 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动
} catch (ClassNotFoundException e) {
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/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull",
"root", "123456");
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, "请填写完整!");
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String number = field1.getText();
String cartype = field2.getText() ;
String carower = field3.getText() ;
String price = field4.getText();
String color = field5.getText();
String hire = field6.getText();
// if(field6.getText().equals("是")&&field6.getText().equals("已经")&&field6.getText().equals("已租用")&&field6.getText().equals("已"))
// {
// hire = "1";
// }
// else
// {
// hire = "0";
// }
if(source == buttonOfQueDing)
{
if(number.equals("")||cartype.equals("")||carower.equals("")||price.equals("")||color.equals("")||hire.equals(""))
{
JOptionPane.showMessageDialog(null, "请填写完整!");
}
else if(!isNumeric(number))
{
JOptionPane.showMessageDialog(null, "序号 请输入整数!");
}
else
{
this.connDB();
try {
int numberint = Integer.parseInt(field1.getText());
stmt = con.createStatement();
String str = "insert into car_information (number,cartype,carower,price,color,hire)values('"+numberint+"','"+cartype+"','"+carower+"','"+price+"','"+color+"','"+hire+"');";
stmt.executeUpdate(str);
JOptionPane.showMessageDialog(null, "录入成功!");
this.closeDB();
this.dispose();
new Guanliyuan(true);
} catch (SQLException e1) {
// e1.printStackTrace();
JOptionPane.showMessageDialog(null, "此编号已经被使用,请换一个编号!");
}
box2.add(buttonOfTijiao);
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);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
public class StartMain {
/**
* @param args
*/
public static void main(String[] args) {
Login login = new Login();
// Zhuce zhuce = new Zhuce();
//
// Guanliyuan guanliyuan = new Guanliyuan();
//
// Luru luru = new Luru();
//
// Xiangxi xiangxi = new Xiangxi();
//
// Xiangxi_yonghu xiangxi_yonghu = new Xiangxi_yonghu();
//
// Xinxitijiao xinxitijiao = new Xinxitijiao();
//
// Yonghuduan yonghuduan = new Yonghuduan();
* 用户端的界面
*/
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();
stmt.executeUpdate(str);
JOptionPane.showMessageDialog(null, "修改成功!");
this.closeDB();
this.dispose();
new Guanliyuan(true);//为ture
} catch (SQLException e1) {
e1.printStackTrace();
}
}
else
{
}
}
}
else if(source == buttonOfQuXIAO)
{
this.dispose();
new Guanliyuan(false);
}
else if(source == buttonOfReset)
{
field2.setText("");
field3.setText("");
field4.setText("");
field5.setText("");
field6.setText("");
}
}
}
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)
{
}
}
try {//创建连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull",
"root", "123456");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeDB() // 关闭连接
{
try {
stmt.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void setText()//此函数功能是 把数据库 需要修改的信息 都打印到界面的文本框中
{
String cartype =null;
String carower=null;
String price=null;
String color=null;
String hire=null;
field1.setText(number);
field1.setEnabled(false);
this.connDB();
try {
stmt = con.createStatement();
//String sql = "select * from car_information (cartype,carower,price,color,hire);";
String sql = "select * from car_information where number = '"+number+"';";
rs = stmt.executeQuery(sql);
if(rs.next())
{
cartype = rs.getString("cartype");
carower = rs.getString("carower");
price = rs.getString("price");
color = rs.getString("color");
hire = rs.getString("hire");
}
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+"';";//查询表里面 自己已经租用的车辆