1.0版本生成四则运算并存入数据库
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/RandomArithmetic")
public class RandomArithmeticServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 调用ArithmeticServlet获取结果
ArithmeticServlet arithmeticServlet = new ArithmeticServlet();
try {
arithmeticServlet.doGet(request, response);
} catch (IOException e) {
e.printStackTrace();
}
// 将结果传递给result.jsp页面
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ArithmeticServlet")
public class ArithmeticServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 随机生成两个数字
int num1 = new Random().nextInt(100);
int num2 = new Random().nextInt(100);
// 随机选择一个四则运算操作符
String[] operators = {"+", "-", "*", "/"};
String operator = operators[new Random().nextInt(operators.length)];
// 计算结果
double result = calculate(num1, num2, operator);
// 保存运算结果到数据库
saveToDatabase(num1, num2, operator, result);
// 输出结果到客户端
response.setContentType("text/html");
response.getWriter().println("<html><head><title>Arithmetic Operation</title></head><body>");
response.getWriter().println("<h1>Arithmetic Operation</h1>");
response.getWriter().println("<p>Number 1: " + num1 + "</p>");
response.getWriter().println("<p>Number 2: " + num2 + "</p>");
response.getWriter().println("<p>Operator: " + operator + "</p>");
response.getWriter().println("<p>Result: " + result + "</p>");
response.getWriter().println("</body></html>");
}
// 执行四则运算
public double calculate(int num1, int num2, String operator) {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return (double) num1 / num2;
default:
return 0.0;
}
}
// 保存运算结果到数据库
public void saveToDatabase(int num1, int num2, String operator, double result) {
Connection connection = null;
try {
// 建立数据库连接
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "123456");
// 插入数据
String insertQuery = "INSERT INTO arithmetic_results (num1, num2, operator, result) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setInt(1, num1);
preparedStatement.setInt(2, num2);
preparedStatement.setString(3, operator);
preparedStatement.setDouble(4, result);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet({"/ArithmeticServlet"})
public class ArithmeticServlet extends HttpServlet {
public ArithmeticServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num1 = (new Random()).nextInt(100);
int num2 = (new Random()).nextInt(100);
String[] operators = new String[]{"+", "-", "*", "/"};
String operator = operators[(new Random()).nextInt(operators.length)];
double result = this.calculate(num1, num2, operator);
this.saveToDatabase(num1, num2, operator, result);
response.setContentType("text/html");
response.getWriter().println("<html><head><title>Arithmetic Operation</title></head><body>");
response.getWriter().println("<h1>Arithmetic Operation</h1>");
response.getWriter().println("<p>Number 1: " + num1 + "</p>");
response.getWriter().println("<p>Number 2: " + num2 + "</p>");
response.getWriter().println("<p>Operator: " + operator + "</p>");
response.getWriter().println("<p>Result: " + result + "</p>");
response.getWriter().println("</body></html>");
}
public double calculate(int num1, int num2, String operator) {
byte var5 = -1;
switch(operator.hashCode()) {
case 42:
if (operator.equals("*")) {
var5 = 2;
}
break;
case 43:
if (operator.equals("+")) {
var5 = 0;
}
case 44:
case 46:
default:
break;
case 45:
if (operator.equals("-")) {
var5 = 1;
}
break;
case 47:
if (operator.equals("/")) {
var5 = 3;
}
}
switch(var5) {
case 0:
return (double)(num1 + num2);
case 1:
return (double)(num1 - num2);
case 2:
return (double)(num1 * num2);
case 3:
return (double)num1 / (double)num2;
default:
return 0.0D;
}
}
public void saveToDatabase(int num1, int num2, String operator, double result) {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "123456");
String insertQuery = "INSERT INTO arithmetic_results (num1, num2, operator, result) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setInt(1, num1);
preparedStatement.setInt(2, num2);
preparedStatement.setString(3, operator);
preparedStatement.setDouble(4, result);
preparedStatement.executeUpdate();
} catch (SQLException | ClassNotFoundException var17) {
var17.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException var16) {
var16.printStackTrace();
}
}
}
}
<%--
Created by IntelliJ IDEA.
User: 郑天羽
Date: 2023/10/16
Time: 15:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Result</title>
</head>
<body>
<h1>Arithmetic Result</h1>
<p>Result: ${result}</p>
</body>
</html>
标签:num1,num2,19,2023.10,operator,import,servlet,response From: https://www.cnblogs.com/ztydebeishanglaojia/p/17781363.html