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

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现通讯录管理系统,可以使用 JavaAWT(Abstract Window Toolkit)和 Swing 库来设计用户界面。以下是一个简单的示例: 1. 首先,创建一个 JFrame 窗口,设置其大小和布局: ``` import javax.swing.*; import java.awt.*; public class AddressBook extends JFrame { public AddressBook() { setTitle("Address Book"); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); } } ``` 2. 在 JFrame 窗口中添加一个 JPanel,用于放置通讯录条目。使用 JScrollPane 包装 JPanel,以便在需要时可以滚动: ``` import javax.swing.*; import java.awt.*; public class AddressBook extends JFrame { public AddressBook() { setTitle("Address Book"); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); JScrollPane scrollPane = new JScrollPane(contentPane); add(scrollPane, BorderLayout.CENTER); } } ``` 3. 创建一个通讯录条目的类 AddressEntry,包括姓名、电话和电子邮件等信息: ``` public class AddressEntry { private String name; private String phoneNumber; private String email; public AddressEntry(String name, String phoneNumber, String email) { this.name = name; this.phoneNumber = phoneNumber; this.email = email; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } public String getEmail() { return email; } } ``` 4. 在 JPanel 中添加一个按钮,用于添加新的通讯录条目。创建一个对话框,让用户输入新条目的信息: ``` import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AddressBook extends JFrame { private JPanel contentPane; public AddressBook() { setTitle("Address Book"); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); JScrollPane scrollPane = new JScrollPane(contentPane); add(scrollPane, BorderLayout.CENTER); JButton addButton = new JButton("Add"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Create a dialog to get the new entry information JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); JTextField nameField = new JTextField(); JTextField phoneField = new JTextField(); JTextField emailField = new JTextField(); panel.add(new JLabel("Name:")); panel.add(nameField); panel.add(new JLabel("Phone:")); panel.add(phoneField); panel.add(new JLabel("Email:")); panel.add(emailField); int result = JOptionPane.showConfirmDialog(null, panel, "Add New Entry", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { // Add the new entry to the content pane AddressEntry entry = new AddressEntry(nameField.getText(), phoneField.getText(), emailField.getText()); addEntry(entry); } } }); add(addButton, BorderLayout.SOUTH); } private void addEntry(AddressEntry entry) { JLabel label = new JLabel(entry.getName() + " " + entry.getPhoneNumber() + " " + entry.getEmail()); contentPane.add(label); contentPane.revalidate(); contentPane.repaint(); } } ``` 5. 启动应用程序,显示通讯录管理系统窗口: ``` public class Main { public static void main(String[] args) { AddressBook addressBook = new AddressBook(); addressBook.setVisible(true); } } ``` 这是一个基本的通讯录管理系统,可以通过添加新的通讯录条目来扩展功能。当然,这只是一个简单的示例,真正的通讯录管理系统可能需要更复杂的功能和更多的界面元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值