实验十
- :
题要求(必做):
(1) 已知client端,提交计算表达式请求,如:3+4
(2)在server服务器端完成运算并将结果返回给client端
(3)使用TCP/IP的套接字编程完成程序
(4)能反复运算
Server端:
package java实验10;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端已连接: " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String expression;
while ((expression = in.readLine()) != null) {
System.out.println("接收到表达式: " + expression);
String result = evaluateExpression(expression);
out.println(result);
}
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String evaluateExpression(String expression) {
try {
String[] parts = expression.split("\\+");
int a = Integer.parseInt(parts[0].trim());
int b = Integer.parseInt(parts[1].trim());
int result = a + b;
return String.valueOf(result);
} catch (Exception e) {
return "无效的表达式";
}
}
}
Client端:
package java实验10;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("已连接到服务器: " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入表达式(例如: 3+4),或输入'q'退出: ");
String expression = scanner.nextLine();
if (expression.equals("q")) {
break;
}
out.println(expression);
String result = in.readLine();
System.out.println("计算结果: " + result);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
实验十一
1、题要求:
(1) 已知学生类Student{id,name,sex}
(2) 使用ObjectStream将对象{1,张三,男},{2,李四,男}序列化后存储到文件d:\student.txt中
(3) 读取student.txt中的内容,并将内容打印到控制台
package java实验11;
import java.io.*;
class Student implements Serializable {
private int id;
private String name;
private String sex;
public Student(int id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class First {
public static void main(String[] args) {
// 创建两个Student对象
Student student1 = new Student(1, "张三", "男");
Student student2 = new Student(2, "李四", "男");
// 使用ObjectOutputStream将对象序列化后存储到文件中
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\eclipse\\workspace\\java实验\\src\\java实验11\\student.txt"))) {
oos.writeObject(student1);
oos.writeObject(student2);
System.out.println("对象已序列化并存储到文件中");
} catch (IOException e) {
e.printStackTrace();
}
// 使用ObjectInputStream读取文件中的内容,并打印到控制台
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\eclipse\\workspace\\java实验\\src\\java实验11\\student.txt"))) {
Student s1 = (Student) ois.readObject();
Student s2 = (Student) ois.readObject();
System.out.println("从文件中读取的对象:");
System.out.println(s1);
System.out.println(s2);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
实验十二
(1):
已知本机装有数据库mysql,并已有数据库名为mydb,下有一表名为student(id(int,10),name(varchar,50),cardid(varchar,50),class(varchar,30)),请使用jdbc直接访问该数据库表,并将表中记录读出,打印到控制台上。
(2):
已知有学生类student(id,name ,cardid, courses),课程类course(id,coursename,courseno),一个学生可能选有多门课程,即courses为list,现要求,输入多个学生的信息及相关课程信息,能够通过学生的cardid,查到该生的所有选课课程,请选用适当的泛型,存储数据,实现要求。
这个实验需要你自己连接电脑上的数据库,代码中的端口、用户名、密码要改成你自己的 :
package java12;
import java.sql.*;
public class First {
private static final String URL = "jdbc:mysql://localhost:3307/mydb?serverTimezone=Asia/Shanghai&useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM student")) {
// 加载MySQL JDBC驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 处理查询结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String cardId = resultSet.getString("cardid");
String className = resultSet.getString("class");
System.out.println("ID: " + id + ", Name: " + name + ", Card ID: " + cardId + ", Class: " + className);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
package java12;
import java.util.*;
class Student {
private int id;
private String name;
private String cardid;
private List<Course> courses;
public Student(int id, String name, String cardid) {
this.id = id;
this.name = name;
this.cardid = cardid;
this.courses = new ArrayList<>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getCardid() {
return cardid;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
}
class Course {
private int id;
private String coursename;
private String courseno;
public Course(int id, String coursename, String courseno) {
this.id = id;
this.coursename = coursename;
this.courseno = courseno;
}
public int getId() {
return id;
}
public String getCoursename() {
return coursename;
}
public String getCourseno() {
return courseno;
}
}
public class Second {
private Map<String, Student> students;
public Second() {
students = new HashMap<>();
}
public void addStudent(Student student) {
students.put(student.getCardid(), student);
}
public void addCourseToStudent(String cardid, Course course) {
Student student = students.get(cardid);
if (student != null) {
student.addCourse(course);
}
}
public List<Course> getStudentCourses(String cardid) {
Student student = students.get(cardid);
if (student != null) {
return student.getCourses();
}
return Collections.emptyList();
}
public static void main(String[] args) {
Second system = new Second();
// 添加学生信息
system.addStudent(new Student(1, "张三", "001"));
system.addStudent(new Student(2, "李四", "002"));
system.addStudent(new Student(3, "王五", "003"));
// 添加课程信息
system.addCourseToStudent("001", new Course(1, "数学", "MATH101"));
system.addCourseToStudent("001", new Course(2, "英语", "ENG101"));
system.addCourseToStudent("002", new Course(1, "数学", "MATH101"));
system.addCourseToStudent("003", new Course(3, "计算机科学", "CS101"));
// 查询学生选课信息
List<Course> courses = system.getStudentCourses("001");
System.out.println("学生001选修的课程:");
for (Course course : courses) {
System.out.println(course.getCoursename() + " (" + course.getCourseno() + ")");
}
}
}
都看到这里了点个赞吧🥱