数据库原理与实践课设(宾馆管理系统),java+jdbc+sqlserver2017

第一次用java写这么多东西,发现开发方面确实比C++高很多,哈哈哈当然要记录下来

~

ps:课设中涉及的所有数据都是存在数据库中的~~~

1、首先当然是连接数据库啦,我这了采用的编程语言是java,所以通过java的jdbc连接SQL server2017

代码:

import java.sql.*;
public class connectDatabase {
    static Connection conn;
    public static Connection getConnect()
        {
            String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=宾馆管理";
            String userName="sa";
            String userPwd="123456";

            try
            {
                Class.forName(driverName);
                conn=DriverManager.getConnection(dbURL,userName,userPwd);
                System.out.println("连接数据库成功");
            }
            catch(Exception e)
            {
                e.printStackTrace();
                System.out.print("连接失败");
            }
            return conn;
        }

}

其中的一些电脑的端口配置我就不多讲啦~

2、首先是登陆界面,这里我在sqlserver里面有一张管理员表,表中存着管理员ID和密码,所以实现了简单的权限管理:

public class login extends JFrame implements ActionListener
{
    private JFrame frame;
    private JTextField Tname;
    private JPasswordField Tpwd;
    private JButton Jexit;
    private JButton Jlogin;
    private JLabel labelBack;
    private ImageIcon backImg = new ImageIcon("image//login.jpg");
    private Image icon = Toolkit.getDefaultToolkit().getImage("image//logo.jpg");
    private JButton changePwd;
    private JButton addAdmin;
    Connection con;
    Statement st;
    public login()
    {
        initData();
    }
    public void initData()
    {

        try{ UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e) {
            System.out.println(e.toString());
        }
        frame = new JFrame("登录");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setIconImage(icon);
        frame.setSize(600, 440);
        frame.setResizable(false);
        frame.setLayout(null);
        Container cp = frame.getContentPane();
        cp.setLayout(null);
        ((JPanel)cp).setOpaque(false);
        labelBack = new JLabel(backImg);
        Tname = new JTextField(15);
        Tpwd = new JPasswordField(15);
        Jexit = new JButton("退出");
        Jlogin = new JButton("登陆");
        changePwd = new JButton("更改密码");
        addAdmin = new JButton("新增管理");
        changePwd.setBounds(478,297,90,20);
        addAdmin.setBounds(476,240,90,20);
        labelBack.setBounds(0,0,600,400);
        Tname.setBounds(new Rectangle(253,228,205,39));
        Tpwd.setBounds(new Rectangle(253,288,205,39));
        Jlogin.setBounds(new Rectangle(252,343,79,33));
        Jexit.setBounds(new Rectangle(379,343,79,33));
        Jlogin.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tpwd.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Jexit.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tname.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        changePwd.setForeground(Color.BLUE);
        addAdmin.setForeground(Color.BLUE);
        changePwd.setContentAreaFilled(false);
        addAdmin.setContentAreaFilled(false);
        frame.getLayeredPane().add(labelBack,new Integer(Integer.MIN_VALUE));
        cp.add(changePwd);
        cp.add(addAdmin);
        cp.add(Tpwd);
        cp.add(Tname);
        cp.add(Jexit);
        cp.add(Jlogin);
        changePwd.addActionListener(this);
        addAdmin.addActionListener(this);
        Jexit.addActionListener(this);
        Jlogin.addActionListener(this);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
        Dimension frameSize = frame.getSize();//获取主界面尺寸
        if(frameSize.height>screenSize.height)frameSize.height = screenSize.height;
        if(frameSize.width>screenSize.width)frameSize.width = screenSize.width;//主界面居中
        frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == Jlogin) {
            String Sname = Tname.getText();
            char[] Spwd = Tpwd.getPassword();
            String s = new String(Spwd);
            System.out.println(s);
            boolean isCorrrct = false;
            if (Sname.isEmpty() || s.isEmpty() ) {
                JOptionPane.showMessageDialog(frame, "不允许有空!");
            } else {
                try {
                    con = connectDatabase.getConnect();
                    String  sql="select * from 管理员表 where 管理员ID='"+Sname+"'"+"and 密码="+"'"+s+"'";
                    st = con.createStatement();
                    ResultSet res = st.executeQuery(sql);
                    if (res.next()) isCorrrct = true;
                    else isCorrrct = false;
                    res.close();
                    st.close();
                    con.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
                if (isCorrrct)
                {
                    JOptionPane.showMessageDialog(frame, "登陆成功!");
                    frame.setVisible(false);
                    functionUI f = new functionUI();
                    f.setVisible(true);
                }
                else
                {
                    JOptionPane.showMessageDialog(frame, "用户名或者密码错误!");
                    Tpwd.setText("");
                }

            }
        }
        else if(e.getSource() == Jexit)
        {
            try {
                frame.setVisible(false);
                MainFrame m = new MainFrame();
                m.setVisible(true);
            }catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        else if(e.getSource() == changePwd){
            change_pwd cc = new change_pwd();
            frame.setVisible(false);
        }
        else if(e.getSource() == addAdmin)
        {
            frame.setVisible(false);
            confirm_addmin cc = new confirm_addmin();
        }
    }

    public static void main(String[] args)
    {
        login L = new login();
    }

}
里面的图片当然是自己设置的,运行界面最后我会放一些;

3、然后是更改密码,更改密码在登陆界面是有接口的:

public class change_pwd extends JFrame implements ActionListener
{
    private JFrame frame;
    private JTextField tname;
    private JTextField toldpwd;
    private JPasswordField tnewPwd;
    private JPasswordField tconfirmPwd;
    private JButton Jexit;
    private JButton Jconfirm;
    private JLabel labelBack;
    private ImageIcon backImg = new ImageIcon("image//change_pwd.jpg");
    private Image icon = Toolkit.getDefaultToolkit().getImage("image//logo.jpg");
    Connection con;
    PreparedStatement st;
    public change_pwd()
    {
        initData();
    }
    public void initData()
    {

        try{ UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e) {
            System.out.println(e.toString());
        }
        frame = new JFrame("新增管理员");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setIconImage(icon);
        frame.setSize(600, 440);
        frame.setResizable(false);
        frame.setLayout(null);
        Container cp = frame.getContentPane();
        cp.setLayout(null);
        ((JPanel)cp).setOpaque(false);
        labelBack = new JLabel(backImg);
        Jexit = new JButton("返回");
        Jconfirm = new JButton("确认");
        labelBack = new JLabel(backImg);
        tconfirmPwd = new JPasswordField();
        tnewPwd = new JPasswordField();
        toldpwd = new JTextField();
        tname = new JTextField();
        labelBack.setBounds(0,0,600,400);
        Jexit.setBounds(495,341,79,33);
        Jconfirm.setBounds(495,291,79,33);
        tconfirmPwd.setBounds(237,348,205,26);
        tnewPwd.setBounds(237,305,205,26);
        toldpwd.setBounds(237,262,205,26);
        tname.setBounds(237,219,205,26);
        frame.getLayeredPane().add(labelBack,new Integer(Integer.MIN_VALUE));
        cp.add(Jexit);
        cp.add(Jconfirm);
        cp.add(tconfirmPwd);
        cp.add(tname);
        cp.add(tnewPwd);
        cp.add(toldpwd);
        Jexit.addActionListener(this);
        Jconfirm.addActionListener(this);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
        Dimension frameSize = frame.getSize();//获取主界面尺寸
        if(frameSize.height>screenSize.height)frameSize.height = screenSize.height;
        if(frameSize.width>screenSize.width)frameSize.width = screenSize.width;//主界面居中
        frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == Jconfirm)
        {
            con = connectDatabase.getConnect();
            try {
                String s1 = tname.getText();
                s1 = s1.trim();
                String s2 = toldpwd.getText();
                s2 = s2.trim();
                String s3 = tnewPwd.getText();
                s3 = s3.trim();
                String s4 = tconfirmPwd.getText();
                s4 = s4.trim();
                if (s1.isEmpty() || s2.isEmpty() || s3.isEmpty() || s4.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "不允许有空");
                } else {
                    String sql1 = "select * from 管理员表 where 管理员ID=? and 密码=?";
                    st = con.prepareStatement(sql1);
                    st.setString(1,s1);
                    st.setString(2,s2);
                    ResultSet rs = st.executeQuery();
                    if(rs.next())
                    {
                        if(s3.equals(s4))
                        {
                            try {
                                String sql2 = "update 管理员表 set 密码 = ? where 管理员ID=?";
                                st = con.prepareStatement(sql2);
                                st.setString(1, s3);
                                st.setString(2, s1);
                                st.executeUpdate();
                                JOptionPane.showMessageDialog(frame,"修改成功!");
                                toldpwd.setText("");
                                tnewPwd.setText("");
                                tconfirmPwd.setText("");
                                tname.setText("");
                            }catch (SQLException ex)
                            {
                                JOptionPane.showMessageDialog(frame,"修改失败!");
                                toldpwd.setText("");
                                tnewPwd.setText("");
                                tconfirmPwd.setText("");
                                ex.printStackTrace();
                            }
                        }
                    }else {
                        JOptionPane.showMessageDialog(frame,"账号密码不匹配!");
                        toldpwd.setText("");
                        tnewPwd.setText("");
                        tconfirmPwd.setText("");
                    }
                }
            }catch (SQLException ex)
            {
                ex.printStackTrace();
            }
        }else if(e.getSource() == Jexit)
        {
            try {
                login LL = new login();
                frame.setVisible(false);
            }catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }

    }

    public static void main(String[] args)
    {
        change_pwd L = new change_pwd();
    }

}
4、接着是新增管理员,新增管理员这里需要验证管理员,所以多了个验证管理员的界面(其实我觉得这里有点冗余了~):

(1)、验证管理员:

import java.awt.*;
import java.awt.event.*;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class confirm_addmin extends JFrame implements ActionListener
{
    private JFrame frame;
    private JTextField Tname;
    private JPasswordField Tpwd;
    private JButton Jexit;
    private JButton Jlogin;
    private JLabel labelBack;
    private ImageIcon backImg = new ImageIcon("image//confirm_changepwd.jpg");
    private Image icon = Toolkit.getDefaultToolkit().getImage("image//logo.jpg");
    Connection con;
    Statement st;
    public confirm_addmin()
    {
        initData();
    }
    public void initData()
    {

        try{ UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e) {
            System.out.println(e.toString());
        }
        frame = new JFrame("管理员账户确认");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setIconImage(icon);
        frame.setSize(600, 440);
        frame.setResizable(false);
        frame.setLayout(null);
        Container cp = frame.getContentPane();
        cp.setLayout(null);
        ((JPanel)cp).setOpaque(false);
        labelBack = new JLabel(backImg);
        Tname = new JTextField(15);
        Tpwd = new JPasswordField(15);
        Jexit = new JButton("返回");
        Jlogin = new JButton("确认");
        labelBack.setBounds(0,0,600,400);
        Tname.setBounds(new Rectangle(234,228,205,39));
        Tpwd.setBounds(new Rectangle(234,288,205,39));
        Jlogin.setBounds(new Rectangle(233,343,79,33));
        Jexit.setBounds(new Rectangle(360,343,79,33));
        Jlogin.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tpwd.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Jexit.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tname.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        frame.getLayeredPane().add(labelBack,new Integer(Integer.MIN_VALUE));
        cp.add(Tpwd);
        cp.add(Tname);
        cp.add(Jexit);
        cp.add(Jlogin);
        Jexit.addActionListener(this);
        Jlogin.addActionListener(this);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
        Dimension frameSize = frame.getSize();//获取主界面尺寸
        if(frameSize.height>screenSize.height)frameSize.height = screenSize.height;
        if(frameSize.width>screenSize.width)frameSize.width = screenSize.width;//主界面居中
        frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == Jlogin) {
            String Sname = Tname.getText();
            char[] Spwd = Tpwd.getPassword();
            String s = new String(Spwd);
            System.out.println(s);
            boolean isCorrrct = false;
            if (Sname.isEmpty() || s.isEmpty() ) {
                JOptionPane.showMessageDialog(frame, "不允许有空!");
            } else {
                try {
                    con = connectDatabase.getConnect();
                    String  sql="select * from 管理员表 where 管理员ID='"+Sname+"'"+"and 密码="+"'"+s+"'";
                    st = con.createStatement();
                    ResultSet res = st.executeQuery(sql);
                    if (res.next()) isCorrrct = true;
                    else isCorrrct = false;
                    res.close();
                    st.close();
                    con.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
                if (isCorrrct)
                {
                    JOptionPane.showMessageDialog(frame, "管理员确认成功!");
                    frame.setVisible(false);
                    add_admin f = new add_admin();
                }
                else
                {
                    JOptionPane.showMessageDialog(frame, "用户名或者密码错误!");
                    Tpwd.setText("");
                }

            }
        }
        else if(e.getSource() == Jexit)
        {
            frame.setVisible(false);
            login m = new login() ;
        }
    }

    public static void main(String[] args)
    {
        confirm_addmin L = new confirm_addmin();
    }

}
(2)、新增管理员:
public class add_admin extends JFrame implements ActionListener
{
    private JFrame frame;
    private JTextField Tnewname;
    private JPasswordField Tpwd;
    private JPasswordField TconfirmPwd;
    private JButton Jexit;
    private JButton Jconfirm;
    private JLabel labelBack;
    private ImageIcon backImg = new ImageIcon("image//add_admin.jpg");
    private Image icon = Toolkit.getDefaultToolkit().getImage("image//logo.jpg");
    Connection con;
    PreparedStatement st;
    public add_admin()
    {
        initData();
    }
    public void initData()
    {

        try{ UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e) {
            System.out.println(e.toString());
        }
        frame = new JFrame("新增管理员");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setIconImage(icon);
        frame.setSize(600, 440);
        frame.setResizable(false);
        frame.setLayout(null);
        Container cp = frame.getContentPane();
        cp.setLayout(null);
        ((JPanel)cp).setOpaque(false);
        labelBack = new JLabel(backImg);
        Tnewname = new JTextField(15);
        Tpwd = new JPasswordField(15);
        Jexit = new JButton("返回");
        Jconfirm = new JButton("确认");
        TconfirmPwd = new JPasswordField(15);
        TconfirmPwd.setBounds(new Rectangle(237,306,205,26));
        labelBack.setBounds(0,0,600,400);
        Tnewname.setBounds(new Rectangle(237,217,205,26));
        Tpwd.setBounds(new Rectangle(237,262,205,26));
        Jconfirm.setBounds(new Rectangle(237,351,79,33));
        Jexit.setBounds(new Rectangle(363,351,79,33));
        Jconfirm.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tpwd.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Jexit.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        Tnewname.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        TconfirmPwd.setFont(new java.awt.Font("宋体",Font.PLAIN,18));
        frame.getLayeredPane().add(labelBack,new Integer(Integer.MIN_VALUE));
        cp.add(TconfirmPwd);
        cp.add(Tpwd);
        cp.add(Tnewname);
        cp.add(Jexit);
        cp.add(Jconfirm);
        Jexit.addActionListener(this);
        Jconfirm.addActionListener(this);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
        Dimension frameSize = frame.getSize();//获取主界面尺寸
        if(frameSize.height>screenSize.height)frameSize.height = screenSize.height;
        if(frameSize.width>screenSize.width)frameSize.width = screenSize.width;//主界面居中
        frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        con = connectDatabase.getConnect();
        if(e.getSource() == Jconfirm) {
            try{
                String s1 = Tnewname.getText();
                char[] c1 = Tpwd.getPassword();
                String s2 = String.valueOf(c1);
                char[] c2 = TconfirmPwd.getPassword();
                String s3 = String.valueOf(c2);
                s1 = s1.trim();
                s2 = s2.trim();
                s3 = s3.trim();
                if(s3.isEmpty()||s2.isEmpty()||s1.isEmpty())
                {
                    JOptionPane.showMessageDialog(frame,"不允许有空");
                }else {
                    if(!s3.equals(s2))
                    {
                        JOptionPane.showMessageDialog(frame,"密码不一致");
                    }
                    else{
                        String sql = "insert 管理员表 values(?,?)";
                        st = con.prepareStatement(sql);
                        st.setString(1,s1);
                        st.setString(2,s2);
                        st.executeUpdate();
                        JOptionPane.showMessageDialog(frame,"添加成功");
                        TconfirmPwd.setText("");
                        Tnewname.setText("");
                        Tpwd.setText("");
                        st.close();
                    }
                }
                con.close();
            }catch (Exception ex)
            {
                JOptionPane.showMessageDialog(frame,"添加失败");
                ex.printStackTrace();
            }
        }
        else if(e.getSource() == Jexit)
        {
            frame.setVisible(false);
            login m = new login() ;
        }
    }

    public static void main(String[] args)
    {
        add_admin L = new add_admin();
    }

}
5、登陆之后就是功能界面了:
public class functionUI extends JFrame implements ActionListener
{
    private JButton emptyRoom;
    private JButton handleRoom;
    private JButton historyRoom;
    private JButton manageVip;
    private JButton exitRoom;
    private JButton setting;
    private JButton visitor;
    private JButton exit;
    private Container mainPanel;
    private JPanel jpane1;
    private JLabel labe1;
    private ImageIcon img1 = new ImageIcon("image//function.jpg");
    private Image icon = Toolkit.getDefaultToolkit().getImage("image//logo.jpg");
    public functionUI()
    {
        try{ UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e) {
            System.out.println(e.toString());
        }
        mainPanel = this.getContentPane();
        setSize(1024,795);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setIconImage(icon);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
        Dimension frameSize = this.getSize();//获取主界面尺寸
        if(frameSize.height>screenSize.height)frameSize.height = screenSize.height;
        if(frameSize.width>screenSize.width)frameSize.width = screenSize.width;//主界面居中
        this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        setLayout(null);
        setVisible(true);
        setTitle("管理员界面");
        jpane1 = new JPanel();
        jpane1.setSize(1024,768);
        jpane1.setBounds(0,0,1024,800);
        jpane1.setLayout(null);
        emptyRoom = new JButton(new ImageIcon("image//Button//FJCX.png",""));
        handleRoom = new JButton(new ImageIcon("image//Button//KFCL.jpg",""));
        historyRoom = new JButton(new ImageIcon("image//Button//KFLS.jpg",""));
        manageVip = new JButton(new ImageIcon("image//Button//GBGL.png",""));
        exitRoom = new JButton(new ImageIcon("image//Button//TF.jpg",""));
        setting = new JButton(new ImageIcon("im
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值