1222carnivore  

今日代码:
1.由于不熟悉util连接数据库的代码,自己写的时候出了问题,于是在这里重新记录一下util怎么写的
com.Util.util

package com.Util;

import java.sql.*;

public class util {

    //数据库连接信息
    private static final String URL = "jdbc:mysql://localhost:3306/db1";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    //获取数据库连接
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    //确保在完成对数据库的操作后,及时关闭ResultSet和PreparedStatement
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭Connection
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

2.写完了精神状态评估表结果的计算和录入

<%@ page import="java.sql.SQLException" %>
<%@ page import="com.Bean.bean" %>
<%@ page import="com.Dao.dao" %>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>评估结果</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f9;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .container {
      background-color: #fff;
      padding: 2rem;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      width: 600px;
      text-align: center;
    }
    .result-title {
      font-size: 1.5rem;
      margin-bottom: 1rem;
    }
    .result-description {
      font-size: 1.2rem;
      color: #333;
    }
    .back-button {
      margin-top: 2rem;
      padding: 0.7rem 1.5rem;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 1rem;
    }
    .back-button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>

<div class="container">
  <div class="result-title">评估结果</div>
  <%
    // 获取表单数据
    String cognitiveFunctionStr = request.getParameter("cognitiveFunction");
    String aggressiveBehaviorStr = request.getParameter("aggressiveBehavior");
    String depressionSymptomsStr = request.getParameter("depressionSymptoms");

    int cognitiveFunction = cognitiveFunctionStr != null ? Integer.parseInt(cognitiveFunctionStr) : 0;
    int aggressiveBehavior = aggressiveBehaviorStr != null ? Integer.parseInt(aggressiveBehaviorStr) : 0;
    int depressionSymptoms = depressionSymptomsStr != null ? Integer.parseInt(depressionSymptomsStr) : 0;

    // 计算总分
    int totalScore = cognitiveFunction + aggressiveBehavior + depressionSymptoms;

    // 根据总分确定评估结果
    String FeelResult = "";
    switch (totalScore) {
      case 0:
        FeelResult = "能力完好";
        break;
      case 1:
        FeelResult = "轻度受损";
        break;
      case 2:
      case 3:
        FeelResult = "中度受损";
        break;
      default:
        FeelResult = "重度受损";
        break;
    }

    // 创建 bean 对象并设置属性
    bean bean = new bean();
    String Username = (String) session.getAttribute("Username");
    bean.setCognitiveFunction(cognitiveFunction);
    bean.setAggressiveBehavior(aggressiveBehavior);
    bean.setDepressionSymptoms(depressionSymptoms);
    bean.setTotalScore(totalScore);
    bean.setFeelResult(FeelResult);

    // 添加用户到数据库
    dao dao = new dao();
    dao.AddUser_Spirit(cognitiveFunction,aggressiveBehavior,depressionSymptoms,FeelResult,Username);
    out.print("<script type='text/javascript'>alert('录入成功!');</script>");
  %>
  <div class="result-description">您的评估结果为 <%= FeelResult %>!总分为 <%= totalScore %>。</div>
  <button class="back-button" onclick="window.location.href='/Menu.jsp'">返回主菜单</button>
</div>

</body>
</html>
posted on 2025-02-20 17:06  作业-----  阅读(13)  评论(0)    收藏  举报