JAVA程序,管理学生的,数据库就不用了
发布网友
发布时间:2023-07-15 18:08
我来回答
共6个回答
热心网友
时间:2023-12-23 12:22
中午休息的时候帮你随便弄了两下,自己拿去改改,消息类自己封装,再随便配置下就能用了,反正只用一次没必要写这么精细
用list会出现无法统一保存序号和编号的问题,所以用treemap直接映*,你应该能看懂嘛
package client;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
import message.Message;
import message.MessageType;
import message.Student;
public class Client {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws UnknownHostException,
IOException, ClassNotFoundException {
TreeMap<Integer, Student> list = new TreeMap<Integer, Student>();
System.out.println("欢迎进入学生管理系统");
System.out.println("1:登录");
System.out.println("2:退出");
int num = input.nextInt();
switch (num) {
case 1:
log(list);
break;
case 2:
System.out.println("谢谢使用!");
break;
}
}
public static void log(TreeMap<Integer, Student> list)
throws UnknownHostException, IOException, ClassNotFoundException {
Socket sock = new Socket(自己配置);
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
window(ois, oos, list);
}
public static void window(ObjectInputStream ois, ObjectOutputStream oos,
TreeMap<Integer, Student> list) throws IOException,
ClassNotFoundException {
System.out.println("1:查询");
System.out.println("2:修改备注");
System.out.println("3:删除");
System.out.println("4:添加");
System.out.println("5:退出");
int num = input.nextInt();
switch (num) {
case 1:
about(oos, ois, list);
log(list);
break;
case 2:
rework(oos, ois);
log(list);
break;
case 3:
remove(oos, ois);
log(list);
break;
case 4:
add(oos, ois, list);
log(list);
break;
case 5:
cancle(ois, oos);
break;
}
}
public static void cancle(ObjectInputStream ois, ObjectOutputStream oos)
throws IOException, ClassNotFoundException {
oos.writeObject(new Message("退出",MessageType.CANCLE));
oos.flush();
oos.close();
ois.close();
}
public static void enter(ObjectOutputStream oos, ObjectInputStream ois)
throws IOException, ClassNotFoundException {
oos.writeObject(new Message(message.MessageType.ENTER));
Message mes = (Message) ois.readObject();
System.out.println(mes.getMessage());
ois.close();
oos.flush();
oos.close();
}
public static void rework(ObjectOutputStream oos, ObjectInputStream ois)
throws IOException, ClassNotFoundException {
System.out.println("输入要修改备注的学生编号");
int num = input.nextInt();
System.out.println("输入新的备注语");
String lag = input.next();
oos.writeObject(new Message(lag, MessageType.REWORK, num));
Message mes = (Message) ois.readObject();
System.out.println(mes.getMessage());
oos.flush();
oos.close();
ois.close();
}
public static void about(ObjectOutputStream oos, ObjectInputStream ois,
TreeMap<Integer, Student> list) throws IOException,
ClassNotFoundException {
oos.writeObject(new Message(null, MessageType.ABOUT));
list = (TreeMap<Integer, Student>) ois.readObject();
Iterator it = list.keySet().iterator();
while (it.hasNext()) {
Object mapKey = it.next();
System.out.println(list.get(mapKey).getNumber() + "\t"
+ list.get(mapKey).getName() + "\t"
+ list.get(mapKey).getAge() + "\t"
+ list.get(mapKey).getSex() + "\t"
+ list.get(mapKey).getLag());
}
ois.close();
oos.flush();
oos.close();
}
public static void remove(ObjectOutputStream oos, ObjectInputStream ois)
throws IOException, ClassNotFoundException {
System.out.println("输入要删除的学生编号");
int num = input.nextInt();
oos.writeObject(new Message(MessageType.REMOVE, num));
Message mes = (Message) ois.readObject();
System.out.println(mes.getMessage());
oos.flush();
oos.close();
ois.close();
}
public static void add(ObjectOutputStream oos, ObjectInputStream ois,
TreeMap<Integer, Student> list) throws IOException,
ClassNotFoundException {
System.out.println("请输入编号");
int num = input.nextInt();
System.out.println("请输入姓名");
String name = input.next();
System.out.println("请输入年龄");
int age = input.nextInt();
System.out.println("请输入性别");
String sex = input.next();
System.out.println("请输入备注");
String lag = input.next();
oos.writeObject(new Message(MessageType.ADD, num, name, age, sex, lag));
Message mes = (Message) ois.readObject();
System.out.println(mes.getMessage());
oos.flush();
oos.close();
ois.close();
}
}
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.TreeMap;
import message.Message;
import message.Student;
public class Server {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ServerSocket ssk = new ServerSocket(4000);
System.out.println("服务器启动");
TreeMap<Integer, Student> list = new TreeMap<Integer, Student>();
list.put(1, new Student("小米", 1, 11, "女", "备注一"));
list.put(2, new Student("小白", 2, 12, "女", "备注二"));
list.put(3, new Student("小宝", 3, 13, "男", "备注三"));
while (true) {
Socket sock = ssk.accept();
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(sock
.getOutputStream());
Message mes = (Message) ois.readObject();
switch (mes.getMesstype()) {
case ADD:
add(oos, list, mes);
break;
case ABOUT:
about(oos, list);
break;
case CANCLE:
System.out.println("客户已退出");
break;
case ENTER:
enter(oos);
break;
case REMOVE:
remove(oos, mes, list);
break;
case REWORK:
rework(oos, mes, list);
break;
case PASSWORD:
break;
}
}
}
public static void cancle(ObjectOutputStream oos,Socket sock, ObjectInputStream ois) throws IOException {
}
public static void add(ObjectOutputStream oos,
TreeMap<Integer, Student> list, Message mes) throws IOException {
int num = mes.getNum();
int age = mes.getAge();
String lag = mes.getLag();
String sex = mes.getSex();
String name = mes.getName();
list.put(num, new Student(name, num, age, sex, lag));
mes.setMessage("添加成功");
oos.writeObject(mes);
oos.flush();
oos.close();
}
public static void enter(ObjectOutputStream oos) throws IOException {
oos.writeObject(new Message("登录成功"));
oos.flush();
oos.close();
}
public static void about(ObjectOutputStream oos,
TreeMap<Integer, Student> list) throws IOException {
oos.writeObject(list);
oos.flush();
oos.close();
}
public static void rework(ObjectOutputStream oos, Message mes,
TreeMap<Integer, Student> list) throws IOException {
int num = mes.getNum();
String lag = (String) mes.getMessage();
list.get(num).setLag(lag);
mes.setMessage("修改成功!");
oos.writeObject(new Message(mes));
oos.flush();
oos.close();
}
public static void remove(ObjectOutputStream oos, Message mes,
TreeMap<Integer, Student> list) throws IOException {
int num = mes.getNum();
list.remove(num);
oos.writeObject(new Message("删除成功!"));
oos.flush();
oos.close();
}
// public void password(){
//
// }
}
热心网友
时间:2023-12-23 12:22
不用数据库麻烦啊.....追问只用1天的,用了就丢了,要弄数据库什么的肯定收费高
追答丢给楼下....
热心网友
时间:2023-12-23 12:23
200块可以搞定追问收费倒是比较合理,但是校方不出钱,我只有自己出,还是算了
热心网友
时间:2023-12-23 12:23
可以使用微型数据库acess
热心网友
时间:2023-12-23 12:24
牛X,居然中午休息那么点时间就搞定了
热心网友
时间:2023-12-23 12:25
确实简单,不过还是建议用B/S模式的吧,C/S的话每个教员的机器都要装客户端,不嫌麻烦吗?