java.swing和Java.awt实现学生信息管理系统

Java代码:

package com.edu.imau;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
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.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;

public class StudentInfoManager extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel panel;
    private JButton button1, button2, button3;
    private JTextArea text1, text2, text3;
    private String[] top={"id","name","age"};
    private static String[][] data=new String[20][3];
    JTable table = new JTable(data, top); 
    JScrollPane scrollPane  = new JScrollPane(table);
    //table.setFillsViewportHeight(true); 


    public StudentInfoManager() throws BadLocationException, SQLException {
        super("学生信息");
        this.setSize(500, 340);
        findInfo();

        this.add(scrollPane, BorderLayout.CENTER);
        this.add(getJPanel(), BorderLayout.SOUTH);
        this.setResizable(true);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private JPanel getJPanel() {
        if (panel == null) {
            panel = new JPanel();
            panel.setLayout(new GridLayout(2, 3));
            text1 = new JTextArea();
            text2 = new JTextArea();
            text3 = new JTextArea();
            button1 = new JButton("add");
            button2 = new JButton("delete");
            button3 = new JButton("update");
            button1.addActionListener(new insert());
            button2.addActionListener(new delete());
            button3.addActionListener(new update());
            text1.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
            text2.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
            text3.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
            text1.setFont(new Font("宋体", Font.BOLD, 16));
            text2.setFont(new Font("宋体", Font.BOLD, 16));
            text3.setFont(new Font("宋体", Font.BOLD, 16));
            text1.setText("id");
            text2.setText("name");
            text3.setText("age");
            panel.add(text1);
            panel.add(text2);
            panel.add(text3);
            panel.add(button1);
            panel.add(button2);
            panel.add(button3);

        }
        return panel;

    }
    class insert implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            if(text1.getText().equals(" ")||text2.getText().equals(" ")||text3.getText().equals(" "))
            {
                JOptionPane.showMessageDialog(StudentInfoManager.this, "输入数据不完整!");
            }
            else{

                Connection con =null;
                Statement stmt=null;

                int rs=0;
                try{

                    con=getCurrentConnection();
                    stmt=con.createStatement();
                    rs=stmt.executeUpdate("INSERT INTO stu(id,name,age)" + "VALUES('" +Integer.parseInt(text1.getText()) + "','" +text2.getText() + "','"
                            + Integer.parseInt(text3.getText()) + "')");
                    if(rs != 0)
                    {
                        JOptionPane.showMessageDialog(StudentInfoManager.this, "插入了"+rs+"条数据!");
                    }
                    //rs.close();
                    stmt.close();
                    con.close();
                }catch(Exception e1){
                    System.out.println(e1.toString());
                }

                }
            }
        }

    class delete implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            if(text1.getText().equals(" ")||text2.getText().equals(" ")||text3.getText().equals(" "))
                {
                    JOptionPane.showMessageDialog(StudentInfoManager.this, "请输入要删除的id");
                }
                else{

                    Connection con =null;
                    Statement stmt=null;
                    int rs=0;
                    try{
                        con=getCurrentConnection();
                        stmt=con.createStatement();
                        rs=stmt.executeUpdate("DELETE FROM stu WHERE id = '" +text1.getText() + "'");
                        if(rs != 0)
                        {
                            JOptionPane.showMessageDialog(StudentInfoManager.this, "删除了"+rs+"条数据!");
                        }
                        //rs.close();
                        stmt.close();
                        con.close();
                    }catch(Exception e1){
                        System.out.println(e1.toString());
                    }

                    }
                }

        }
    class update implements ActionListener{

        public void actionPerformed(ActionEvent e) {
                if(text1.getText().equals(" ") || text2.getText().equals(" ") || text3.getText().equals(" "))
                {
                    JOptionPane.showMessageDialog(StudentInfoManager.this, "输入数据不完整");
                }
                else{
                    //System.out.print(text1.getText());

                    Connection con =null;
                    Statement stmt=null;
                    int rs=0;
                    try{
                        con=getCurrentConnection();
                        stmt=con.createStatement();

                        rs=stmt.executeUpdate("UPDATE stu SET name='" + text2.getText()+ "',age= '" + text3.getText()+ "'WHERE id = '" + text1.getText() + "'");
                        if(rs != 0)
                        {
                            JOptionPane.showMessageDialog(StudentInfoManager.this, "更新了"+rs+"条数据!");
                        }
                        //rs.close();
                        stmt.close();
                        con.close();
                    }catch(Exception e1){
                        System.out.println(e1.toString());
                    }

                    }
                }

        }       


    public static Connection getCurrentConnection(){
        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/student";
        String user="root";
        String password="123456";
        Connection con =null;
        try {
            Class.forName(driver);
            con=DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return con;
    }
    public static void findInfo(){

        Connection con =null;
        Statement stmt=null;
        ResultSet rs=null;
        try{

            con=getCurrentConnection();
            stmt=con.createStatement();
            rs=stmt.executeQuery("select * from stu");
            int i=0;
            while(rs.next()){
                data[i][0]=rs.getString(1);
                data[i][1]=rs.getString(2);
                data[i][2]=rs.getString(3);
                i++;
            }
            rs.close();
            stmt.close();
            con.close();
        }catch(Exception e){
            System.out.println(e.toString());
        }

    }

    public static void main(String[] args) throws BadLocationException, SQLException {
        new StudentInfoManager().setVisible(true);
}}

数据库文件:


DROP TABLE IF EXISTS `stu`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `stu` ( `id` int(11) NOT NULL auto_increment, `name` varchar(40) default NULL, `age` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

LOCK TABLES `stu` WRITE;
/*!40000 ALTER TABLE `stu` DISABLE KEYS */;
INSERT INTO `stu` VALUES (1,'zhangsan',25),(2,'zhangsan',25),(3,'wangwu ',56),(4,'ll',24),(5,'zhangsi',30),(6,'ggh',12),(7,'ko',26);
-- Dump completed on 2017-04-07 3:40:07

完事。

附上源代码

http://download.csdn.net/detail/broccoli2/9816754

全部评论

相关推荐

11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务