zoukankan      html  css  js  c++  java
  • 学生数据库管理系统Model2

    test

    package test2;
    
    /**
     * model2模式
     */
    import javax.swing.*;
    import java.util.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.Event;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Test1 extends JFrame implements ActionListener {
        // rowData用来存放行数据
        // columnNames用来存放列名
    
        JTable jt = null;
        JScrollPane jsp = null;
    
        JPanel jp1, jp2;
        JLabel jl1;
        JButton jb1, jb2, jb3, jb4;
        JTextField jtf;
        stuModel sm;// 防止内存泄漏
    
        public static void main(String[] args) {
            Test1 test = new Test1();
        }
    
        public void hello() {
            sm = new stuModel();
            jt.setModel(sm);
        }
    
        public Test1() {
    
            // jt=new JTable(rowData,columnNames);
            // 初始化
            jp1 = new JPanel();
            jtf = new JTextField(10);
            jb1 = new JButton("查询");
            jb1.addActionListener(this);
            jl1 = new JLabel("请输入名字");
            jp1.add(jl1);
            jp1.add(jtf);
            jp1.add(jb1);
            // 初始化下面的
            jp2 = new JPanel();
            jb2 = new JButton("添加");
            jb2.addActionListener(this);
            jb3 = new JButton("修改");
            jb3.addActionListener(this);
            jb4 = new JButton("删除");
            jb4.addActionListener(this);
            jp2.add(jb2);
            jp2.add(jb3);
            jp2.add(jb4);
            // 创建一个数据模型对象
            // 初始化
            jt = new JTable(sm);
            jsp = new JScrollPane(jt);
            // 把jsp放入到JFrame
            this.add(jsp);
            this.add(jp1, "North");
            this.add(jp2, "South");
    
            this.setSize(400, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getSource() == jb1) {
                System.out.println("用户希望被查询");
                String name = this.jtf.getText().trim();
                String sql = "select* from stu where stuName='" + name + "'";
                if (name.equals(""))
                    sql = "";
                sm = new stuModel(sql);
                jt.setModel(sm);
            }
            // 当用户点添加
            else if (e.getSource() == jb2) {
                stuAdd sa = new stuAdd(this, "添加学生", true);// 父窗口 注意true与false区别
                System.out.println("添加成功");
                sm = new stuModel();
                jt.setModel(sm);
                
            } else if (e.getSource() == jb4) {
                // 返回用户点上的行,要是没选择返回-1
                int rowNum = this.jt.getSelectedRow();
                if (rowNum == -1) {
                    // 提示
                    JOptionPane.showMessageDialog(this, "请选择一行");
                    return;
                }
                // 得到学生编号
                String stuId = (String) sm.getValueAt(rowNum, 0);
                // 创建一个sql语句
                String sql = "delete from stu where stuId=?";
                String[] paras = { stuId };
                stuModel temp = new stuModel();
                temp.updatestu(sql, paras);
                sm = new stuModel();
                jt.setModel(sm);
            } else if (e.getSource() == jb3) {//修改
                // 用户希望修改
                int rowNum = this.jt.getSelectedRow();
                if (rowNum == -1) {
                    // 提示
                    JOptionPane.showMessageDialog(this, "请选择一行");
                    return;
                }
                // 显示修改对话框
                new stuUp(this, "修改", true, sm, rowNum);
                System.out.println("修改成功");
                sm = new stuModel();
                jt.setModel(sm);
            }
        }
    }
    View Code

    stuModel

    package test2;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
    
    import javax.swing.table.*;
    
    public class stuModel extends AbstractTableModel {
        Vector rowData, columnNames;
        Connection ct = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        String user = "root";
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8";
    
        // 添加学生(增删改都可以)
        public boolean updatestu(String sql, String[] paras) {
    
            // 根据用户输入的sql语句完成添加任务
            boolean b = true;
            try {
                // 1.加载驱动
                Class.forName(driver);
                // 2.得到连接
                ct = DriverManager.getConnection(url, "root", "123456");
                // 3.创建
                ps = ct.prepareStatement(sql);
                // 3.给参数赋值
                for (int i = 0; i < paras.length; i++) {
                    ps.setString(i + 1, paras[i]);
                }
                if (ps.executeUpdate() != 1) {
                    b = false;
                }
            } catch (Exception e2) {
                b = false;
                e2.printStackTrace();
            } finally {
                try {
                    if (rs != null)
                        rs.close();
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
    
            }
            return b;
        }
    
        public void init(String sql) {
            if (sql.equals("")) {
                sql = "select* from stu";
            }
    
            System.out.println(sql);
            columnNames = new Vector();
            columnNames.add("学号");
            columnNames.add("名字");
            columnNames.add("性别");
            columnNames.add("年龄");
            columnNames.add("籍贯");
            columnNames.add("系别");
    
            rowData = new Vector();// 可以存放多行
            try {// 1.加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 2.、
                ct = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456");
    
                ps = ct.prepareStatement(sql);
                rs = ps.executeQuery();
                while (rs.next()) {
                    Vector hang = new Vector();
                    hang.add(rs.getString(1));
                    hang.add(rs.getString(2));
                    hang.add(rs.getString(3));
                    hang.add(rs.getInt(4));
                    hang.add(rs.getString(5));
                    hang.add(rs.getString(6));
                    // 加入到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 e2) {
                    e2.printStackTrace();
                }
            }
        }
    
        public stuModel() {
            this.init("");
        }
    
        public stuModel(String sql) {
            this.init(sql);
        }
    
        @Override
        public int getRowCount() {// 多少行
            return this.rowData.size();
        }
    
        @Override
        public int getColumnCount() {// 多少列
            // TODO Auto-generated method stub
            return this.columnNames.size();
        }
    
        @Override
        public Object getValueAt(int row, int column) {
            return ((Vector) this.rowData.get(row)).get(column);
        }
    }
    View Code

    stuAdd

    package test2;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    
    import org.apache.commons.dbcp.DriverManagerConnectionFactory;
    
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class stuAdd extends JDialog implements ActionListener {
        // 定义需要的swing组件
        JLabel jl1, jl2, jl3, jl4, jl5, jl6;
        JButton jb1, jb2, jb3;
        JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
        JPanel jp1, jp2, jp3;
    
        // 他的父窗口owner 窗口名字 模式窗口or非模式()
        public stuAdd(Frame owner, String title, boolean model) {
            super(owner, title, model);
            jl1 = new JLabel("stuId");
            jl2 = new JLabel("stuName");
            jl3 = new JLabel("stuSex");
            jl4 = new JLabel("stuAge");
            jl5 = new JLabel("stuJg");
            jl6 = new JLabel("stuDept");
    
            jtf1 = new JTextField();
            jtf2 = new JTextField();
            jtf3 = new JTextField();
            jtf4 = new JTextField();
            jtf5 = new JTextField();
            jtf6 = new JTextField();
    
            jb1 = new JButton("添加");
            jb1.addActionListener(this);
            jb2 = new JButton("取消");
    
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            // 设置布局
            jp1.setLayout(new GridLayout(6, 1));
            jp2.setLayout(new GridLayout(6, 1));
            // 添加组件
            jp1.add(jl1);
            jp1.add(jl2);
            jp1.add(jl3);
            jp1.add(jl4);
            jp1.add(jl5);
            jp1.add(jl6);
    
            jp2.add(jtf1);
            jp2.add(jtf2);
            jp2.add(jtf3);
            jp2.add(jtf4);
            jp2.add(jtf5);
            jp2.add(jtf6);
    
            jp3.add(jb1);
            jp3.add(jb2);
            this.add(jp1, BorderLayout.WEST);
            this.add(jp2, BorderLayout.CENTER);
            this.add(jp3, BorderLayout.SOUTH);
            // 展现
            this.setSize(350, 300);
            this.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("你好啊");
            if (e.getSource() == jb1) {
                // 希望修改
                String sql = "insert into stu values(?,?,?,?,?,?)";
                String[] paras = { jtf1.getText(), jtf2.getText(), jtf3.getText(), jtf4.getText(), jtf5.getText(),
                        jtf6.getText(), };
                stuModel temp = new stuModel();
                temp.updatestu(sql, paras);
                this.dispose();
            }
        }
    }
    View Code

    stuUp

    package test2;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    
    import org.apache.commons.dbcp.DriverManagerConnectionFactory;
    
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class stuUp extends JDialog implements ActionListener {
        // 定义需要的swing组件
        JLabel jl1, jl2, jl3, jl4, jl5, jl6;
        JButton jb1, jb2, jb3;
        JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
        JPanel jp1, jp2, jp3;
    
        // 他的父窗口owner 窗口名字 模式窗口or非模式()
        public stuUp(Frame owner, String title, boolean model, stuModel sm, int rowNum) {
            super(owner, title, model);
            jl1 = new JLabel("stuId");
            jl2 = new JLabel("stuName");
            jl3 = new JLabel("stuSex");
            jl4 = new JLabel("stuAge");
            jl5 = new JLabel("stuJg");
            jl6 = new JLabel("stuDept");
    
            jtf1 = new JTextField();
            // 初始化数据
            jtf1.setText((String) sm.getValueAt(rowNum, 0));
            jtf2 = new JTextField();
            // 让jtf1不能被修改
            jtf1.setEnabled(false);
            jtf2.setText((String) sm.getValueAt(rowNum, 1));
            jtf3 = new JTextField();
            jtf3.setText((String) sm.getValueAt(rowNum, 2));
            jtf4 = new JTextField();
            jtf4.setText((String) sm.getValueAt(rowNum, 3).toString());
            jtf5 = new JTextField();
            jtf5.setText((String) sm.getValueAt(rowNum, 4));
            jtf6 = new JTextField();
            jtf6.setText((String) sm.getValueAt(rowNum, 5));
    
            jb1 = new JButton("修改");
            jb1.addActionListener(this);
            jb2 = new JButton("取消");
    
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            // 设置布局
            jp1.setLayout(new GridLayout(6, 1));
            jp2.setLayout(new GridLayout(6, 1));
            // 添加组件
            jp1.add(jl1);
            jp1.add(jl2);
            jp1.add(jl3);
            jp1.add(jl4);
            jp1.add(jl5);
            jp1.add(jl6);
    
            jp2.add(jtf1);
            jp2.add(jtf2);
            jp2.add(jtf3);
            jp2.add(jtf4);
            jp2.add(jtf5);
            jp2.add(jtf6);
    
            jp3.add(jb1);
            jp3.add(jb2);
            this.add(jp1, BorderLayout.WEST);
            this.add(jp2, BorderLayout.CENTER);
            this.add(jp3, BorderLayout.SOUTH);
            // 展现
            this.setSize(350, 300);
            this.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("你好啊");
            if (e.getSource() == jb1) {
                // 做一个sql
                String sql = "update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?";
                String[] paras = { jtf2.getText(), jtf3.getText(), jtf4.getText(), jtf5.getText(), jtf6.getText(),
                        jtf1.getText() };
                stuModel temp = new stuModel();
                temp.updatestu(sql, paras);
                this.dispose();
            }
        }
    }
    View Code

     

     后台与界面分离

  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10826004.html
Copyright © 2011-2022 走看看