3306-学生信息的添加与查询

本文介绍了一个简单的学生信息管理系统的设计与实现,该系统能够读取学生数据并进行更新与查询操作。系统采用HashMap存储学生信息,使用TreeSet确保输出时学生ID有序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学生信息的添加与查询

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Problem Description
设计一个学生添加和查询的系统,从键盘读入学生的数据,然后再从屏幕显示出来。
Input
第一行有2个整数N和M,其中:N——学生数量,M——学生属性数量;
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)
Output
输出所有学生的属性及数据。(每行的列数据之间用‘\t’进行分隔)
Sample Input
5 4
id name birthday score
0001 Mike 1990-05-20 98.5
0002 John 1992-05-20 67
0003 Hill 1994-05-02 36.5
0004 Christ 1996-05-20 86.5
0001 Jack 1998-05-20 96
Sample Output
id:0001	name:Jack	birthday:1998_5_20	score:96.0
id:0002	name:John	birthday:1992_5_20	score:67.0
id:0003	name:Hill	birthday:1994_5_2	score:36.5
id:0004	name:Christ	birthday:1996_5_20	score:86.5
Hint

Source

zhouxq

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * Created by Dell on 2018/5/20.
 */
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        scanner.nextLine();
        scanner.nextLine();
        HashMap<String,Student> hashMap = new HashMap<String,Student>();
        for(int i=0;i<n;i++){
            Student student = new Student(scanner.next(),scanner.next(),scanner.next(),scanner.nextDouble());
            hashMap.put(student.getId(),student);
        }
        TreeSet<String> treeSet = new TreeSet<String>(hashMap.keySet());
        for(String id:treeSet){
            System.out.println(hashMap.get(id));
        }
    }
}
class Student{
    String id;
    String name;
    String birthday;
    double score;
    Student(String id, String name, String birthday,double score){
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "id:" + id + "\tname:" + name + "\tbirthday:"
                + dateChange(birthday) + "\tscore:" + score;
    }
    public static String dateChange(String birthday) {
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyy_M_d");
        String date = null;
        try {
            date = matter2.format(matter1.parse(birthday));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

好的,下面我将提供一个使用JSP实现学生信息增加和查询的示例。我们将创建两个主要功能:一个是用于添加学生信息,另一个是用于查询所有学生信息。 ### 1. 数据库准备 首先,确保你有一个数据库(例如MySQL),并且已经创建了一个名为 `students` 的表: ```sql CREATE DATABASE school; USE school; CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age INT, major VARCHAR(50) ); ``` ### 2. JSP页面代码 #### (1) 添加学生信息的HTML表单 (`addStudent.jsp`) 这个表单允许用户输入学生的姓名、年龄和专业。 ```html <!DOCTYPE html> <html> <head> <title>添加学生</title> </head> <body> <h2>添加学生信息</h2> <form action="addStudentAction.jsp" method="post"> 姓名: <input type="text" name="name"><br><br> 年龄: <input type="number" name="age"><br><br> 专业: <input type="text" name="major"><br><br> <input type="submit" value="添加"> </form> </body> </html> ``` #### (2) 处理添加请求的JSP页面 (`addStudentAction.jsp`) 该页面接收来自表单的数据,并将其插入到数据库中。 ```jsp <%@ page import="java.sql.*" %> <% String name = request.getParameter("name"); String ageStr = request.getParameter("age"); String major = request.getParameter("major"); int age = Integer.parseInt(ageStr); // 数据库连接参数 String url = "jdbc:mysql://localhost:3306/school"; String user = "root"; // 替换为你的数据库用户名 String password = "password"; // 替换为你的数据库密码 try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO students (name, age, major) VALUES (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setString(3, major); pstmt.executeUpdate(); out.println("学生信息添加成功!"); conn.close(); } catch (Exception e) { out.println("发生错误:" + e.getMessage()); } %> ``` #### (3) 查询学生信息的JSP页面 (`viewStudents.jsp`) 该页面从数据库中查询所有学生信息并显示在网页上。 ```jsp <%@ page import="java.sql.*" %> <html> <head> <title>查看学生信息</title> </head> <body> <h2>学生信息列表</h2> <table border="1"> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>专业</th> </tr> <% String url = "jdbc:mysql://localhost:3306/school"; String user = "root"; // 替换为你的数据库用户名 String password = "password"; // 替换为你的数据库密码 try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM students"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String major = rs.getString("major"); %> <tr> <td><%= id %></td> <td><%= name %></td> <td><%= age %></td> <td><%= major %></td> </tr> <% } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { out.println("发生错误:" + e.getMessage()); } %> </table> </body> </html> ``` ### 解释: - **addStudent.jsp**:这是一个简单的HTML表单,允许用户输入学生信息- **addStudentAction.jsp**:此页面处理表单提交的数据,并将其插入到数据库中。 - **viewStudents.jsp**:此页面从数据库中检索所有学生信息,并以表格形式显示。 ### 注意事项: - 确保你已经在项目中配置了MySQL JDBC驱动程序。 - 替换数据库连接中的用户名和密码为你自己的数据库凭据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值