数据库:Mysql 8.0
数据库设置:
代码构成:
代码部分:
1.CarManager类(系统总页面 增删改查功能显示在这里):
package current;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class CarManager extends JFrame implements ActionListener {
JPanel jp1,jp2;
JButton jb1,jb2,jb3,jb4;
JTextField jtf1,jtf2;
JLabel jl1;
JTable jt;
JScrollPane jsp;
CarModel sm;
String strRS;
private Object[] types = {"-请选择查询方式-","按车牌号查询","车型查询"};
private JComboBox searchType = new JComboBox(types); //创建一个组合框用来选取查询不同的学生信息
PreparedStatement ps;
Connection ct = null;
ResultSet rs = null;
public CarManager(){
jp1 = new JPanel(); //jp1 背景 灰色
jp1.setBackground(Color.GRAY);
jtf1 = new JTextField(15); //输入框
jtf2 = new JTextField();
jb1 = new JButton("查询"); //查询按钮
jb1.addActionListener(this);
jl1 = new JLabel("总个数:");
jp1.add(searchType);
jp1.add(jtf1);
jp1.add(jb1);
jp1.add(jl1);
jp1.add(jtf2);
jb2 = new JButton("添加");
jb2.setSize(1000,500);
jb2.addActionListener(this);
jb3 = new JButton("修改");
jb3.addActionListener(this);
jb4 = new JButton("删除");
jb4.addActionListener(this);
jp2 = new JPanel();
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.setBackground(Color.GRAY);
//创建模型对象
sm = new CarModel();
//初始化总个数
strRS = String.valueOf(sm.getRowCount());
jtf2.setText(strRS);
//初始化表和滚动面板
jt = new JTable(sm);
jsp = new JScrollPane(jt);
this.add(jsp);
this.add(jp1,BorderLayout.PAGE_START);
this.add(jp2,BorderLayout.PAGE_END);
this.setTitle("车辆租赁管理系统");
this.setSize(600,400);
//显示设置 默认居中显示
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
//判断是哪个按钮被点击
if (arg0.getSource() == jb1) { //查询按钮
int index = searchType.getSelectedIndex();
String sql = new String();
if (index == 0) {
sql = "select * from basicdata";
} else if (index == 1) {
String carid = this.jtf1.getText().trim();
sql = "select * from basicdata where carid = '" + carid + "'";
} else if (index == 2) {
String cartype = this.jtf1.getText().trim();
sql = "select * from basicdata where cartype = '" + cartype + "'";
}
//构建一个数据模型类,并更新
sm = new CarModel(sql);
strRS = String.valueOf(sm.getRowCount());
jtf2.setText(strRS); //填入“ ”总个数
jt.setModel(sm); //更新jtable
}
else if (arg0.getSource() == jb2){ //弹出添加界面
AddDialog add = new AddDialog(this,"添加车辆信息",true);
//重新获取新的数据模型
sm = new CarModel();
strRS = String.valueOf(sm.getRowCount());
jtf2.setText(strRS); //填入“ ”总个数
jt.setModel(sm); 更新jtable
}
else if (arg0.getSource() == jb4) { //删除操作
//1.鼠标点击选择一行并获取到
int rowNum = this.jt.getSelectedRow(); //getSelectedRow 会返回用户点中的行
//如果用户一行都没有选,返回-1
if(rowNum == -1){
JOptionPane.showMessageDialog(this,"请选中一行");
return;
}
String carid = (String) sm.getValueAt(rowNum,0);
//连接数据库,完成删除操作
try {
Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3307/demo", "root", "123456");
ps = ct.prepareStatement("delete from basicdata where carid = ?");
ps.setString(1,carid);
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"删除成功","删除情况(?)",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (ct != null) {
ct.close();
ct = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
sm = new CarModel();
strRS = String.valueOf(sm.getRowCount());
jtf2.setText(strRS);
//更新JLable
jt.setModel(sm);
}
else if (arg0.getSource() == jb3){ //修改操作
int rowNum = this.jt.getSelectedRow();
if(rowNum == -1){
JOptionPane.showMessageDialog(this,"请选择一行");
return;
}
UpdateDialog ud = new UpdateDialog(this,"修改车辆信息",true,sm,rowNum);
sm = new CarModel();
jt.setModel(sm);
}
}
}
运行结果:
2.CarModel类(创建系统的数据模型(上图中的表格)):
package current;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.*;
public class CarModel extends AbstractTableModel {
//rowData 存放行数据, columnNames 存放列名
Vector rowData,columnNames;
Statement s = null;
Connection con = null;
ResultSet rs = null;
public void init(String str){
if(str.equals("")){
str = "select * from basicdata";
}
//设置列名
columnNames = new Vector();
columnNames.add("车牌号");
columnNames.add("车型");
columnNames.add("日租金");
//rowData 存放多行
rowData = new Vector();
try{
//连接数据库demo
String url = "jdbc:mysql://127.0.0.1:3307/demo";
String user = "root";
String password = "123456";
con = DriverManager.getConnection(url,user,password);
s = con.createStatement();//创建s对象
rs = s.executeQuery(str);//查询结果
while(rs.next()){
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
//加入到rowData中
rowData.add(hang);//这里是二维向量,表示行;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs = null;
}
if(s != null){
s.close();
s = null;
}
if(con != null){
con.close();
con = null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
//第二个构造函数,通过传递的sql语句来获得数据模型
public CarModel(String str){
this.init(str);
}
//构造函数,用于初始化我的数据模型(表)
public CarModel(){
this.init("");
}
//得到共有多少行
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}
//得到共有多少列
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
//得到某行某列的数据
public Object getValueAt(int row, int column) {
// TODO Auto-generated method stub
return ((Vector)(this.rowData.get(row))).get(column);
}
//得到属性名字
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
}
3.AddDialog类(汽车信息的增加功能):
package current;
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
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.*;
public class AddDialog extends JDialog implements ActionListener {
//定义我需要的swing组件
JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
JPanel jp1,jp2,jp3;
JButton jb1,jb2;
//owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
public AddDialog(Frame owner, String title, boolean modal){
//调用父类方法
super(owner,title,modal);
jl1 = new JLabel("车牌号");
jl2 = new JLabel("车型");
jl3 = new JLabel("日租金");
jf1 = new JTextField(30);
jf2 = new JTextField(30);
jf3 = new JTextField(30);
jb1 = new JButton("添加");
jb1.addActionListener(this::actionPerformed);
jb2 = new JButton("取消");
jb2.addActionListener(this::actionPerformed);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//设置布局
jp1.setLayout(new GridLayout(7,1));
jp2.setLayout(new GridLayout(7,1));
jp3.add(jb1);
jp3.add(jb2);
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp2.add(jf1);
jp2.add(jf2);
jp2.add(jf3);
this.add(jp1, BorderLayout.WEST);
this.add(jp2, BorderLayout.CENTER);
this.add(jp3, BorderLayout.SOUTH);
this.setLocation(600, 350);
this.setSize(300,200);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb1){
Connection ct = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//连接数据库,进行增加操作
ct = DriverManager.getConnection("jdbc:mysql://localhost:3307/demo", "root", "123456");
//与编译语句对象
String strsql = "insert into basicdata values(?,?,?)";
pstmt = ct.prepareStatement(strsql);
//给对象赋值
pstmt.setString(1,jf1.getText());
pstmt.setString(2,jf2.getText());
pstmt.setString(3,jf3.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功", "添加情况(?)",-1);
this.dispose();//关闭对话框
}catch(Exception arg1){
arg1.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs = null;
}
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if(ct != null){
ct.close();
ct = null;
}
}catch(Exception arg2){
arg2.printStackTrace();
}
}
}else{
this.dispose();
}
}
}
运行结果:
4.UpdateDialog类(汽车信息的修改功能):
package current;
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
/*
// * 是修改学生信息
*/
public class UpdateDialog extends JDialog implements ActionListener {
//定义我需要的swing组件
JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
JPanel jp1,jp2,jp3;
JButton jb1,jb2;
//owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
public UpdateDialog(Frame owner, String title, boolean modal, CarModel cm, int rowNum){
//调用父类方法
super(owner,title,modal);
jl1 = new JLabel("车牌号");
jl2 = new JLabel("车型");
jl3 = new JLabel("日租金");
jf1 = new JTextField(30);
jf1.setText((cm.getValueAt(rowNum, 0)).toString());
jf2 = new JTextField(30);
jf2.setText((cm.getValueAt(rowNum, 1)).toString());
jf3 = new JTextField(30);
jf3.setText(cm.getValueAt(rowNum, 2).toString());
jb1 = new JButton("修改");
jb1.addActionListener(this::actionPerformed);
jb2 = new JButton("取消");
jb2.addActionListener(this::actionPerformed);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//设置布局
jp1.setLayout(new GridLayout(7,1));
jp2.setLayout(new GridLayout(7,1));
jp3.add(jb1);
jp3.add(jb2);
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp2.add(jf1);
jp2.add(jf2);
jp2.add(jf3);
this.add(jp1, BorderLayout.WEST);
this.add(jp2, BorderLayout.CENTER);
this.add(jp3, BorderLayout.SOUTH);
this.setLocation(600, 350);
this.setSize(300,200);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb1){
Connection ct = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//连接数据库,进行增加操作
ct = DriverManager.getConnection("jdbc:mysql://localhost:3307/demo", "root", "123456");
// 使用预处理语句防止SQL注入
String strsql = "UPDATE basicdata SET cartype = ?, daymoney = ? WHERE carid = ?";
pstmt = ct.prepareStatement(strsql);
// 设置参数
pstmt.setString(1, jf2.getText());
pstmt.setString(2, jf3.getText());
pstmt.setString(3, jf1.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功", "修改情况(?)",JOptionPane.PLAIN_MESSAGE);
this.dispose();//关闭对话框
}catch(Exception arg1){
arg1.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs = null;
}
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if(ct != null){
ct.close();
ct = null;
}
}catch(Exception arg2){
arg2.printStackTrace();
}
}
}else{
this.dispose();//关闭对话框
}
}
}
TestMain类(程序由此开始):
package current;
import java.sql.*;
public class TestMain {
public static void main(String[] args)throws SQLException {
new CarManager();
}
}
代码可运行
计算机小白,请多指教