JavaSwing图形界面编写简单的图书管理系统(jdbc)

题目要求
编写一个图书管理程序,该程序具有如下功能:
1)录入图书信息(Book),图书的属性包括书名(name)、作者(author)、出版社(press)、刊号(ISBN)、出版日期(pubDate)、页数(pages)等。
2)录入用户信息(User),用户属性包括用户名(account),密码(password)、用户类型(type),用户分为管理员和普通用户。
3)记录每个用户的借书记录,借书记录(BorrowRecord)包括:用户(user)、借书日期(borrowDate)、图书(book)。
4)记录每个用户的还书记录,还书记录(ReturnRecord)包括:用户(user)、还书日期(returnDate)、图书(book)。

  • 代码
    录入书籍信息
package com.book.view;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class book extends JFrame {

    private JPanel contentPane;
    private JTextField jt1;
    private JTextField jt2;
    private JTextField jt3;
    private JTextField jt4;
    private JTextField jt5;
    private JTextField jt6;

    Connection coon = null;
    PreparedStatement st;
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    String sql = "insert into book values(?,?,?,?,?,?)";
    /**
     * Launch the application.
     */
    /**
     * Create the frame.
     */
    public book() {
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 520 ;
        int windowsHeight = 520;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("Name");
        jlb1.setBounds(57, 39, 66, 39);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("Author");
        jlb2.setBounds(57, 88, 66, 32);
        contentPane.add(jlb2);

        JLabel jlb3 = new JLabel("Press");
        jlb3.setBounds(57, 139, 66, 32);
        contentPane.add(jlb3);

        JLabel jlb4 = new JLabel("ISBN");
        jlb4.setBounds(57, 190, 54, 36);
        contentPane.add(jlb4);

        JLabel jlb5 = new JLabel("PubDate");
        jlb5.setBounds(57, 236, 66, 37);
        contentPane.add(jlb5);

        JLabel jlb6 = new JLabel("Pages");
        jlb6.setBounds(57, 292, 66, 29);
        contentPane.add(jlb6);

        JButton jb1 = new JButton("Input");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Class.forName(driver);
                    coon = DriverManager.getConnection(url,user,password);
                    st = coon.prepareStatement(sql);
                    String str1  = jt1.getText();
                    String str2  = jt2.getText();
                    String str3  = jt3.getText();
                    String str4  = jt4.getText();
                    String str5  = jt5.getText();
                    String str6  = jt6.getText();
                    st.setString(1, str1);
                    st.setString(2, str2);
                    st.setString(3, str3);
                    st.setString(4, str4);
                    st.setString(5,str5);
                    st.setString(6,str6);
                    st.executeUpdate();
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, "关闭数据库失败!", "提示", 1);
                        e.printStackTrace();
                    }
                }
            }
        });
        jb1.setBounds(57, 378, 124, 52);
        contentPane.add(jb1);

        JButton jb2 = new JButton("Exit");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jt1.setText("");
                jt2.setText("");
                jt3.setText("");
                jt4.setText("");
                jt5.setText("");
                jt6.setText("");
            }
        });
        jb2.setBounds(273, 378, 124, 52);
        contentPane.add(jb2);
        jt1 = new JTextField();
        jt1.setBounds(151, 46, 215, 32);
        contentPane.add(jt1);
        jt1.setColumns(10);
        jt2 = new JTextField();
        jt2.setBounds(151, 91, 215, 29);
        contentPane.add(jt2);
        jt2.setColumns(10);
        jt3 = new JTextField();
        jt3.setBounds(151, 143, 215, 32);
        contentPane.add(jt3);
        jt3.setColumns(10);
        jt4 = new JTextField();
        jt4.setBounds(151, 194, 215, 32);
        contentPane.add(jt4);
        jt4.setColumns(10);
        jt5 = new JTextField();
        jt5.setBounds(151, 242, 215, 31);
        contentPane.add(jt5);
        jt5.setColumns(10);
        jt6 = new JTextField();
        jt6.setBounds(151, 300, 215, 32);
        contentPane.add(jt6);
        jt6.setColumns(10);
    }
}


记录

package com.book.view;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.SystemColor;
import java.awt.Toolkit;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class record extends JFrame {

    private JPanel contentPane;

    /**
     * Create the frame.
     */
    public record() {
        setTitle("Record");
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 400 ;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,428,375);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton jb1 = new JButton("Borrow");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                borrowrecord br = new borrowrecord();
            }
        });
        jb1.setBounds(143, 51, 137, 37);
        contentPane.add(jb1);

        JButton jb2 = new JButton("Return");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                returnrecord rr = new returnrecord();
            }
        });
        jb2.setBounds(143, 117, 137, 37);
        contentPane.add(jb2);

        JButton jb3 = new JButton("ShowBorrow");
        jb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                show1 s1 = new show1();
            }
        });
        jb3.setBounds(143, 186, 137, 37);
        contentPane.add(jb3);

        JButton jb4 = new JButton("ShowReturn");
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                show2 s2 = new show2();
            }
        });
        jb4.setBounds(143, 259, 137, 37);
        contentPane.add(jb4);
    }

}

借书记录

package com.book.view;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class borrowrecord extends JFrame {

    private JPanel contentPane;
    private JTextField jtf2;
    private JLabel jlb1;
    private JLabel jlb2;
    private JTextField jtf3;
    private JLabel lblNewLabel_1;
    private JTextField jtf1;

    Connection coon = null;
    PreparedStatement st;
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    String sql = "insert into borrow values(?,?,?)";
    /**
     * Create the frame.
     */
    public borrowrecord() {
        setTitle("Record");
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 430 ;
        int windowsHeight = 400;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        jtf2 = new JTextField();
        jtf2.setBounds(160, 88, 200, 40);
        contentPane.add(jtf2);
        jtf2.setColumns(10);
        jlb1 = new JLabel("Bookname");
        jlb1.setBounds(47, 95, 76, 33);
        contentPane.add(jlb1);
        jlb2 = new JLabel("BorrowDate");
        jlb2.setBounds(47, 155, 98, 40);
        contentPane.add(jlb2);
        JButton jb1 = new JButton("Set");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Class.forName(driver);
                    coon = DriverManager.getConnection(url,user,password);
                    st = coon.prepareStatement(sql);
                    String str1  = jtf1.getText();
                    String str2  = jtf2.getText();
                    String str3  = jtf3.getText();
                    st.setString(1, str1);
                    st.setString(2, str2);
                    st.setString(3, str3);
                    st.executeUpdate();
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, "关闭数据库失败!", "提示", 1);
                        e.printStackTrace();
                    }
                }

            }
        });
        jb1.setBounds(71, 247, 93, 23);
        contentPane.add(jb1);

        JButton jb2 = new JButton("Reset");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jtf1.setText("");
                jtf2.setText("");
                jtf3.setText("");
            }
        });
        jb2.setBounds(274, 247, 93, 23);
        contentPane.add(jb2);

        jtf3 = new JTextField();
        jtf3.setBounds(160, 155, 200, 40);
        contentPane.add(jtf3);
        jtf3.setColumns(10);

        lblNewLabel_1 = new JLabel("Username");
        lblNewLabel_1.setBounds(47, 40, 76, 38);
        contentPane.add(lblNewLabel_1);

        jtf1 = new JTextField();
        jtf1.setBounds(160, 37, 200, 39);
        contentPane.add(jtf1);
        jtf1.setColumns(10);
    }
}

还书记录

package com.book.view;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class returnrecord extends JFrame {

    private JPanel contentPane;
    private JTextField jtf3;
    private JTextField jtf2;
    private JTextField jtf1;

    Connection coon = null;
    PreparedStatement st;
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    String sql = "insert into returnr values(?,?,?)";
    /**
     * Create the frame.
     */
    public returnrecord() {
        setTitle("Record");
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 430 ;
        int windowsHeight = 400;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("Bookname");
        jlb1.setBounds(52, 102, 87, 39);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("ReturnDate");
        jlb2.setBounds(52, 169, 108, 33);
        contentPane.add(jlb2);

        jtf3 = new JTextField();
        jtf3.setBounds(168, 169, 193, 33);
        contentPane.add(jtf3);
        jtf3.setColumns(10);

        JButton jb1 = new JButton("Set");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Class.forName(driver);
                    coon = DriverManager.getConnection(url,user,password);
                    st = coon.prepareStatement(sql);
                    String str1  = jtf1.getText();
                    String str2  = jtf2.getText();
                    String str3  = jtf3.getText();
                    st.setString(1, str1);
                    st.setString(2, str2);
                    st.setString(3, str3);
                    st.executeUpdate();
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, "关闭数据库失败!", "提示", 1);
                        e.printStackTrace();
                    }
                }
            }
        });
        jb1.setBounds(71, 242, 108, 33);
        contentPane.add(jb1);

        JButton jb2 = new JButton("Reset");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jtf1.setText("");
                jtf2.setText("");
                jtf3.setText("");
            }
        });
        jb2.setBounds(245, 242, 108, 33);
        contentPane.add(jb2);

        jtf2 = new JTextField();
        jtf2.setBounds(168, 108, 193, 33);
        contentPane.add(jtf2);
        jtf2.setColumns(10);

        JLabel jlb3 = new JLabel("Username");
        jlb3.setBounds(52, 60, 69, 32);
        contentPane.add(jlb3);

        jtf1 = new JTextField();
        jtf1.setBounds(168, 67, 193, 31);
        contentPane.add(jtf1);
        jtf1.setColumns(10);
    }
}

管理员登录

package com.book.view;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

class login_admin extends JFrame {

    private JPanel contentPane;
    private JPasswordField passwordField;
    private JTextField jtf1;

    //数据库信息
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    Connection coon = null;
    /**
     * Create the frame.
     */
    public login_admin() {
        setVisible(true);
        setTitle("Welcome");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 450 ;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("User");
        jlb1.setBounds(70, 59, 56, 44);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("Password");
        jlb2.setBounds(45, 125, 81, 35);
        contentPane.add(jlb2);

        passwordField = new JPasswordField();
        passwordField.setBounds(143, 129, 187, 31);
        contentPane.add(passwordField);

        jtf1 = new JTextField();
        jtf1.setBounds(143, 72, 186, 31);
        contentPane.add(jtf1);
        jtf1.setColumns(10);

        JButton jb1 = new JButton("\u767B\u5F55");		//登录按钮
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String str1 = jtf1.getText();
                String str2 = passwordField.getPassword().toString();
                try {
                    Class.forName(driver);
                    coon = (Connection) DriverManager.getConnection(url,user,password);
                    String sql="select count(1) from register where user='"+str1+"' and password ='"+str2+"'";
                    if (sql!=null) {
                        JOptionPane.showMessageDialog(null, "登录成功!", "提示", 1);
                        book bo = new book();
                    }
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null,"关闭数据库失败!","提示",1);
                        e.printStackTrace();
                    }
                }

            }
        });
        jb1.setBounds(70, 199, 93, 23);
        contentPane.add(jb1);

        JButton jb2 = new JButton("\u6CE8\u518C");		//注册按钮
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                register_admin re = new register_admin();
            }
        });
        jb2.setBounds(253, 199, 93, 23);
        contentPane.add(jb2);
    }
}

用户登录

package com.book.view;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

class login_user extends JFrame {

    private JPanel contentPane;
    private JPasswordField passwordField;
    private JTextField jtf1;

    //数据库信息
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    Connection coon = null;
    PreparedStatement st;
    /**
     * Create the frame.
     */
    public login_user() {
        setVisible(true);
        setTitle("Welcome");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 450 ;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("User");
        jlb1.setBounds(70, 59, 56, 44);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("Password");
        jlb2.setBounds(45, 125, 81, 35);
        contentPane.add(jlb2);

        passwordField = new JPasswordField();
        passwordField.setBounds(143, 129, 187, 31);
        contentPane.add(passwordField);

        jtf1 = new JTextField();
        jtf1.setBounds(143, 72, 186, 31);
        contentPane.add(jtf1);
        jtf1.setColumns(10);

        JButton jb1 = new JButton("\u767B\u5F55");		//登录按钮
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String str1 = jtf1.getText();
                String str2 = passwordField.getPassword().toString();
                try {
                    Class.forName(driver);
                    coon = (Connection) DriverManager.getConnection(url,user,password);
                    String sql="select count(1) from register where user='"+str1+"' and password ='"+str2+"'";
                    if (sql!=null) {
                        JOptionPane.showMessageDialog(null, "登录成功!", "提示", 1);
                    }
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null,"关闭数据库失败!","提示",1);
                        e.printStackTrace();
                    }
                }
                record rd = new record();
            }
        });
        jb1.setBounds(70, 199, 93, 23);
        contentPane.add(jb1);

        JButton jb2 = new JButton("\u6CE8\u518C");		//注册按钮
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                register_user re = new register_user();
            }
        });
        jb2.setBounds(253, 199, 93, 23);
        contentPane.add(jb2);
    }
}

管理员注册

package com.book.view;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

class register_admin extends JFrame {

    private JPanel contentPane;
    private JTextField jt1;
    private JTextField jt2;
    private JButton jb1;
    private JButton jb2;
    public String[] typelist = {"用户","管理员"};
    //数据库信息
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    Connection coon = null;
    PreparedStatement st;
    /**
     * Launch the application.
     */
    /**
     * Create the frame.
     */
    public register_admin() {
        setTitle("\u6CE8\u518C");
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 450;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBackground(SystemColor.activeCaption);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("Username");
        jlb1.setBounds(42, 57, 76, 21);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("Password");
        jlb2.setBounds(42, 109, 76, 18);
        contentPane.add(jlb2);

        jt1 = new JTextField();
        jt1.setBounds(152, 57, 178, 21);
        contentPane.add(jt1);
        jt1.setColumns(10);

        jt2 = new JTextField();
        jt2.setBounds(151, 106, 179, 21);
        contentPane.add(jt2);
        jt2.setColumns(10);


        jb2 = new JButton("\u91CD\u7F6E");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jt1.setText("");
                jt2.setText("");
            }
        });
        jb2.setBounds(267, 194, 93, 23);
        contentPane.add(jb2);

        JLabel lblNewLabel = new JLabel("Type");
        lblNewLabel.setBounds(42, 153, 54, 15);
        contentPane.add(lblNewLabel);

        JComboBox comboBox = new JComboBox(typelist);
        comboBox.setBounds(148, 150, 182, 23);
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str3 = comboBox.getSelectedItem().toString();
            }
        });
        contentPane.add(comboBox);

        String sql = "insert into register values(?,?,?)";
        jb1 = new JButton("\u6CE8\u518C");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Class.forName(driver);
                    coon = (Connection) DriverManager.getConnection(url,user,password);
                    st = (PreparedStatement) coon.prepareStatement(sql);
                    String str1 = jt1.getText();
                    String str2 = jt2.getText();
                    String str3 = comboBox.getSelectedItem().toString();
                    st.setString(1, str1);
                    st.setString(2, str2);
                    st.setString(3, str3);
                    st.executeUpdate();
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null,"关闭数据库失败!","提示",1);
                        e.printStackTrace();
                    }
                }
            }
        });
        jb1.setBounds(87, 194, 93, 23);
        contentPane.add(jb1);

    }
}

用户注册

package com.book.view;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

class register_user extends JFrame {

    private JPanel contentPane;
    private JTextField jt1;
    private JTextField jt2;
    private JButton jb1;
    private JButton jb2;
    public String[] typelist = {"用户","管理员"};
    //数据库信息
    public static final String driver = "com.mysql.jdbc.Driver";
    public static final String url = "jdbc:mysql://localhost:3306/mydb";
    public static final String user = "root";
    public static final String password = "1492949670";
    Connection coon = null;
    PreparedStatement st;
    /**
     * Create the frame.
     */
    public register_user() {
        setTitle("\u6CE8\u518C");
        setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 450;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlb1 = new JLabel("Username");
        jlb1.setBounds(42, 57, 76, 21);
        contentPane.add(jlb1);

        JLabel jlb2 = new JLabel("Password");
        jlb2.setBounds(42, 109, 76, 18);
        contentPane.add(jlb2);

        jt1 = new JTextField();
        jt1.setBounds(152, 57, 178, 21);
        contentPane.add(jt1);
        jt1.setColumns(10);

        jt2 = new JTextField();
        jt2.setBounds(151, 106, 179, 21);
        contentPane.add(jt2);
        jt2.setColumns(10);


        jb2 = new JButton("\u91CD\u7F6E");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jt1.setText("");
                jt2.setText("");
            }
        });
        jb2.setBounds(267, 194, 93, 23);
        contentPane.add(jb2);

        JLabel lblNewLabel = new JLabel("Type");
        lblNewLabel.setBounds(42, 153, 54, 15);
        contentPane.add(lblNewLabel);

        JComboBox comboBox = new JComboBox(typelist);
        comboBox.setBounds(148, 150, 182, 23);
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str3 = comboBox.getSelectedItem().toString();
            }
        });
        contentPane.add(comboBox);

        String sql = "insert into register values(?,?,?)";
        jb1 = new JButton("\u6CE8\u518C");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Class.forName(driver);
                    coon = (Connection) DriverManager.getConnection(url,user,password);
                    st = (PreparedStatement) coon.prepareStatement(sql);
                    String str1 = jt1.getText();
                    String str2 = jt2.getText();
                    String str3 = comboBox.getSelectedItem().toString();
                    st.setString(1, str1);
                    st.setString(2, str2);
                    st.setString(3, str3);
                    st.executeUpdate();
                }catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
                if (null != coon) {
                    try {
                        coon.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null,"关闭数据库失败!","提示",1);
                        e.printStackTrace();
                    }
                }
            }
        });
        jb1.setBounds(87, 194, 93, 23);
        contentPane.add(jb1);

    }
}

借书记录一览表

package com.book.view;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

class show1 extends JFrame  {

    //定义一些控件
    private JTable jt;
    private JScrollPane jsp;
    private JPanel jp1;
    //定义操作数据库需要的变量
    PreparedStatement ps = null;
    Connection ct = null;
    ResultSet rs = null;
    //连接数据库
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    //rowData用来存放行数据
    //columnNames存放列名
    Vector rowData,columnNames;

    //构造函数
    public show1()
    {
        //中间
        columnNames = new Vector();
        //设置列名
        columnNames.add("用户名");
        columnNames.add("书名");
        columnNames.add("借书日期");

        rowData = new Vector();
        try {
            Class.forName( driver );
            ct = DriverManager.getConnection( url,user,password );
            ps = ct.prepareStatement("select username,bookname,borrowDate from borrow");
            rs=ps.executeQuery();
            while(rs.next())
            {
                //rowData可以存放多行
                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();
                if(ps!=null) ps.close();
                if(ct!=null) ct.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        //初始化JTable
        jt = new JTable(rowData,columnNames);
        jt.setBackground(new java.awt.Color(198, 226, 255));
        //初始化jsp JScrollPane
        jsp = new JScrollPane(jt);
        jp1 = new JPanel();
        jp1.add(jsp);
        add(jp1);
        setVisible(true);
        setTitle("BorrowRecord");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 500;
        int windowsHeight = 460;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
    }
}

还书记录一览表

package com.book.view;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

class show2 extends JFrame  {

    //定义一些控件
    private JTable jt;
    private JScrollPane jsp;
    private JPanel jp1;
    //定义操作数据库需要的变量
    PreparedStatement ps = null;
    Connection ct = null;
    ResultSet rs = null;
    //连接数据库
    public static final String driver = "";
    public static final String url = "";
    public static final String user = "";
    public static final String password = "";
    //rowData用来存放行数据
    //columnNames存放列名
    Vector rowData,columnNames;

    //构造函数
    public show2()
    {
        //中间
        columnNames = new Vector();
        //设置列名
        columnNames.add("用户名");
        columnNames.add("书名");
        columnNames.add("还书日期");

        rowData = new Vector();
        try {
            Class.forName( driver );
            ct = DriverManager.getConnection( url,user,password );
            ps = ct.prepareStatement("select username,bookname,returnDate from returnr");
            rs=ps.executeQuery();
            while(rs.next())
            {
                //rowData可以存放多行
                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();
                if(ps!=null) ps.close();
                if(ct!=null) ct.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        //初始化JTable
        jt = new JTable(rowData,columnNames);
        jt.setBackground(new java.awt.Color(198, 226, 255));
        //初始化jsp JScrollPane
        jsp = new JScrollPane(jt);
        jp1 = new JPanel();
        jp1.add(jsp);
        add(jp1);
        setVisible(true);
        setTitle("ReturnRecord");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 500;
        int windowsHeight = 460;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
    }
}

主函数入口

package com.book.view;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class mainPage extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainPage frame = new mainPage();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public mainPage() {
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int windowsWedth = 450;
        int windowsHeight = 300;
        setBounds((width-windowsWedth)/2,(height-windowsHeight)/2,windowsWedth,windowsHeight);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton jb1 = new JButton("\u7BA1\u7406\u5458");//管理员界面
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login_admin l1 = new login_admin();
            }
        });
        jb1.setBounds(127, 60, 165, 68);
        contentPane.add(jb1);

        JButton jb2 = new JButton("\u7528\u6237");      //用户界面
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login_user l2 = new login_user();
            }
        });
        jb2.setBounds(133, 153, 159, 68);
        contentPane.add(jb2);
    }
}

  • 运行结果
    JavaSwing图形界面编写简单的图书管理系统(jdbc)_第1张图片
    JavaSwing图形界面编写简单的图书管理系统(jdbc)_第2张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第3张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第4张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第5张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第6张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第7张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第8张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第9张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第10张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第11张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第12张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第13张图片
    JavaSwing图形界面编写简单的图书管理系统(jdbc)_第14张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第15张图片JavaSwing图形界面编写简单的图书管理系统(jdbc)_第16张图片
    JavaSwing图形界面编写简单的图书管理系统(jdbc)_第17张图片

你可能感兴趣的:(java实验)