菜鸟的博客

纵有疾风起,人生不言弃。

导航

小学期第三次博客--实现代码如下

package com.example.hadoop;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HiveJdbcUtils {
    private static final String URL = "jdbc:hive2://192.168.26.2:10000/testforhadoop";
    private static final String USER = "hadoop";
    private static final String PASSWORD = "123456";


    static {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>添加项目查新信息</title>
    <script type="text/javascript">
        function toggleSelects() {
            var immediate = document.getElementById('immediate_select');
            var achievement = document.getElementById('achievement_select');
            var other = document.getElementById('other_select');
            immediate.style.display = 'none';
            achievement.style.display = 'none';
            other.style.display = 'none';

            if (document.getElementById('immediate_radio').checked) {
                immediate.style.display = 'block';
            } else if (document.getElementById('achievement_radio').checked) {
                achievement.style.display = 'block';
            } else if (document.getElementById('other_radio').checked) {
                other.style.display = 'block';
            }
        }

        function generateInputFields() {
            var numberOfPoints = document.getElementById('number_of_novelty_points').value;
            var container = document.getElementById('novelty_points_container');
            container.innerHTML = '';
            for (var i = 0; i < numberOfPoints; i++) {
                var input = document.createElement('input');
                input.type = 'text';
                input.name = 'novelty_point_' + (i + 1);
                input.placeholder = '查新点 ' + (i + 1);
                container.appendChild(input);
                container.appendChild(document.createElement('br'));
            }
        }

        function collectNoveltyPoints() {
            var numberOfPoints = document.getElementById('number_of_novelty_points').value;
            var allPoints = [];
            for (var i = 0; i < numberOfPoints; i++) {
                var point = document.getElementsByName('novelty_point_' + (i + 1))[0].value;
                allPoints.push(point);
            }
            document.getElementById('column_name').value = allPoints.join(';');
        }
    </script>
</head>
<body>
<h1>添加项目查新信息</h1>
<form action="AddProjectServlet" method="post" onsubmit="collectNoveltyPoints()">
    <label for="id">编号:</label>
    <input type="text" id="id" name="id"><br><br>

    <label for="project_name">项目名称:</label>
    <input type="text" id="project_name" name="project_name"><br><br>

    <label for="novelty_scope">查新范围:</label>
    <input type="checkbox" id="domestic" name="novelty_scope" value="国内"> 国内
    <input type="checkbox" id="international" name="novelty_scope" value="国外"> 国外<br><br>

    <label for="novelty_purpose">查新目的:</label><br>
    <input type="radio" id="immediate_radio" name="novelty_purpose" value="immediate" onclick="toggleSelects()"> 立即查新
    <input type="radio" id="achievement_radio" name="novelty_purpose" value="achievement" onclick="toggleSelects()"> 成果查新
    <input type="radio" id="other_radio" name="novelty_purpose" value="other" onclick="toggleSelects()"> 其他查新<br><br>

    <div id="immediate_select" style="display:none;">
        <label for="novelty_purpose_immediate">立即查新目的:</label>
        <select id="immediate" name="novelty_purpose_immediate">
            <option value="项目申报(国家级、省部级、学协会、其他)">项目申报(国家级、省部级、学协会、其他)</option>
            <option value="中小企业创新基金(研发阶段、小试-中试、市场推广)">中小企业创新基金(研发阶段、小试-中试、市场推广)</option>
            <option value="新产品">新产品</option>
            <option value="技术引进">技术引进</option>
            <option value="技术吸收与创新">技术吸收与创新</option>
            <option value="其他">其他</option>
        </select><br><br>
    </div>

    <div id="achievement_select" style="display:none;">
        <label for="novelty_purpose_achievement">成果查新目的:</label>
        <select id="achievement" name="novelty_purpose_achievement">
            <option value="成果鉴定">成果鉴定</option>
            <option value="高新技术成果转化">高新技术成果转化</option>
            <option value="申报奖励(国家级、省部级、学协会、其他奖励)">申报奖励(国家级、省部级、学协会、其他奖励)</option>
            <option value="高新技术企业认定">高新技术企业认定</option>
            <option value="其他">其他</option>
        </select><br><br>
    </div>

    <div id="other_select" style="display:none;">
        <label for="novelty_purpose_other">其他查新目的:</label>
        <select id="other" name="novelty_purpose_other">
            <option value="博士论文开题">博士论文开题</option>
            <option value="申报专利">申报专利</option>
            <option value="其他">其他</option>
        </select><br><br>
    </div>

    <label for="technical_points">技术要点:</label><br>
    <textarea id="technical_points" name="technical_points" rows="4" cols="50"></textarea><br><br>

    <label for="number_of_novelty_points">查新点个数:</label>
    <input type="number" id="number_of_novelty_points" name="number_of_novelty_points" onchange="generateInputFields()"><br><br>

    <div id="novelty_points_container"></div>

    <input type="hidden" id="column_name" name="column_name">

    <label for="search_terms">检索词:</label>
    <input type="text" id="search_terms" name="search_terms"><br><br>

    <label for="completion_date">完成时间:</label>
    <input type="date" id="completion_date" name="completion_date"><br><br>

    <label for="discipline_classification">学科分类:</label>
    <select id="discipline_classification" name="discipline_classification">
        <option value="信息科学与系统科学">信息科学与系统科学</option>
        <option value="生物学">生物学</option>
        <option value="力学">力学</option>
        <option value="物理学">物理学</option>
        <option value="化学">化学</option>
        <option value="数学">数学</option>
        <option value="天文学">天文学</option>
        <option value="地球科学">地球科学</option>
        <option value="其他">其他</option>
    </select><br><br>

    <label for="industry_classification">产业分类:</label>
    <select id="industry_classification" name="industry_classification">
        <option value="节能环保产业">节能环保产业</option>
        <option value="生物产业">生物产业</option>
        <option value="新一代信息技术产业">新一代信息技术产业</option>
        <option value="高端装备制造产业">高端装备制造产业</option>
        <option value="新能源产业">新能源产业</option>
        <option value="新材料产业">新材料产业</option>
        <option value="新能源汽车">新能源汽车</option>
        <option value="国民经济其他产业">国民经济其他产业</option>
    </select><br><br>

    <label for="remarks">备注:</label><br>
    <textarea id="remarks" name="remarks" rows="4" cols="50"></textarea><br><br>

    <input type="submit" value="提交">
</form>
</body>
</html>
package com.example.hadoop;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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("/AddProjectServlet")
public class AddProjectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String id = request.getParameter("id");
        String projectName = request.getParameter("project_name");
        String noveltyScope = request.getParameter("novelty_scope");
        String noveltyPurpose = request.getParameter("novelty_purpose_immediate");
        if (noveltyPurpose == null || noveltyPurpose.isEmpty()) {
            noveltyPurpose = request.getParameter("novelty_purpose_achievement");
        }
        if (noveltyPurpose == null || noveltyPurpose.isEmpty()) {
            noveltyPurpose = request.getParameter("novelty_purpose_other");
        }
        String technicalPoints = request.getParameter("technical_points");
        int numberOfNoveltyPoints = Integer.parseInt(request.getParameter("number_of_novelty_points"));
        String columnName = request.getParameter("column_name");
        String searchTerms = request.getParameter("search_terms");
        String completionDate = request.getParameter("completion_date");
        String disciplineClassification = request.getParameter("discipline_classification");
        String industryClassification = request.getParameter("industry_classification");
        String remarks = request.getParameter("remarks");

        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = HiveJdbcUtils.getConnection();

            String sql = "INSERT INTO ProjectNoveltyCheck (id, project_name, novelty_scope, novelty_purpose, technical_points, number_of_novelty_points, search_terms, completion_date, discipline_classification, industry_classification, remarks, column_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            stmt.setString(2, projectName);
            stmt.setString(3, noveltyScope);
            stmt.setString(4, noveltyPurpose);
            stmt.setString(5, technicalPoints);
            stmt.setInt(6, numberOfNoveltyPoints);
            stmt.setString(7, searchTerms);
            stmt.setString(8, completionDate);
            stmt.setString(9, disciplineClassification);
            stmt.setString(10, industryClassification);
            stmt.setString(11, remarks);
            stmt.setString(12, columnName);  // 设置 column_name 字段

            stmt.executeUpdate();

            response.getWriter().println("数据已成功插入!");

        } catch (SQLException e) {
            e.printStackTrace();
            response.getWriter().println("数据插入失败: " + e.getMessage());
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

posted on 2024-07-20 20:14  hhmzd233  阅读(3)  评论(0编辑  收藏  举报