如何将CSV文件上传到JSP页面并将其中的数据发送到MySQL数据库?

huangapple go评论112阅读模式
英文:

How to upload a csv to a JSP page and send the data in it to a MySQL database?

问题

  1. 我有一个 JSP 页面,用户可以使用 `<input type="file">` 上传 CSV 文件。我希望该 CSV 文件的内容能够存储到数据库中。我花了好几个小时搜索,只找到了关于在 PHP 中处理的方法,但我对 PHP 没有任何经验,因此非常感谢提供 Java 方面的任何帮助。
  2. 我的 JSP 页面:
  3. <span>上传 CSV 文件:<input type="file"></span>
  4. <table style="margin-left: 20px">
  5. <tr>
  6. <th class="table-custom">员工姓名</th>
  7. <th class="table-custom">上班时间</th>
  8. <th class="table-custom">下班时间</th>
  9. </tr>
  10. </table>
英文:

I have a JSP where a user can upload a csv using input type=&quot;file&quot;. I want the content in that csv to go to a database. I searched for hours and only found methods in php for this, but I have zero experience in php, so any help in java is appreciated.

My JSP:

  1. &lt;span&gt;Upload CSV : &lt;input type=&quot;file&quot;&gt;&lt;/span&gt;
  2. &lt;table style=&quot;margin-left: 20px&quot;&gt;
  3. &lt;tr&gt;
  4. &lt;th class=&quot;table-custom&quot;&gt;Employee Name&lt;/th&gt;
  5. &lt;th class=&quot;table-custom&quot;&gt;In Time&lt;/th&gt;
  6. &lt;th class=&quot;table-custom&quot;&gt;Out Time&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/table&gt;

答案1

得分: 1

尝试这段代码:

  1. <!-- 开始代码段: js 隐藏: false 控制台: true babel: false -->
  2. <!-- 语言: lang-js -->
  3. document.getElementById("upload").addEventListener("change", upload, false);
  4. var out = "";
  5. function upload(e) {
  6. document.getElementById('csvForm').innerHTML = "";
  7. var data = null;
  8. var file = e.target.files[0];
  9. var reader = new FileReader();
  10. reader.readAsText(file);
  11. reader.onload = function(event) {
  12. var csvData = event.target.result;
  13. var parsedCSV = d3.csv.parseRows(csvData);
  14. parsedCSV.forEach(function(d, i) {
  15. if (i == 0) return true; // 跳过标题
  16. if (d.constructor === Array) {
  17. createForm(d);
  18. }
  19. });
  20. }
  21. }
  22. function createForm(csv) {
  23. out += '<input value="' + csv[0] + '" name="date" type="hidden">';
  24. out += '<input value="' + csv[1] + '" name="name" type="hidden">';
  25. out += '<input value="' + csv[2] + '" name="in-time" type="hidden">';
  26. out += '<input value="' + csv[3] + '" name="out-time" type="hidden">';
  27. document.getElementById('csvForm').innerHTML = out;
  28. document.getElementById('csvForm').setAttribute("formmethod", "post");
  29. document.getElementById('csvForm').setAttribute("formaction", "testBrain.jsp");
  30. out += '<br>';
  31. }
  32. <!-- 语言: lang-html -->
  33. <input id="upload" type="file">
  34. <form id="csvForm"></form>
  35. <input type="submit" value="submit" form="csvForm" formmethod="post" formaction="testBrain.jsp">
  36. <script src="https://d3js.org/d3.v3.js"></script>
  37. <!-- 结束代码段 -->

testBrain.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@page import="java.sql.*,java.util.*"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Refresh" content="5;url=test.jsp">
  6. </head>
  7. </html>
  8. <%
  9. String[] date=request.getParameterValues("date");
  10. String[] name=request.getParameterValues("name");
  11. String[] inTime=request.getParameterValues("in-time");
  12. String[] outTime=request.getParameterValues("out-time");
  13. try
  14. {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDb", "root", "1234");
  17. Statement st=conn.createStatement();
  18. for(int x=0; x<name.length;x++) {
  19. int i = st.executeUpdate("insert into csv(date,name,`in`,`out`)values('"
  20. + date[x] + "','" + name[x] + "','" + inTime[x] + "','" + outTime[x] + "')");
  21. }
  22. out.println("Data is successfully inserted! Redirecting.. Please wait");
  23. // String redirectURL = "test.jsp";
  24. // response.sendRedirect(redirectURL);
  25. }
  26. catch(Exception e)
  27. {
  28. System.out.print(e);
  29. e.printStackTrace();
  30. out.println("Error");
  31. }
  32. %>

我使用了这个问题并做了一些修改使其能够工作。您可以通过在 createForm JS 中移除 &quot;type = hidden&quot; 来在提交前查看内容。

英文:

Try this code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. document.getElementById(&quot;upload&quot;).addEventListener(&quot;change&quot;, upload, false);
  2. var out = &quot;&quot;;
  3. function upload(e) {
  4. document.getElementById(&#39;csvForm&#39;).innerHTML = &quot;&quot;;
  5. var data = null;
  6. var file = e.target.files[0];
  7. var reader = new FileReader();
  8. reader.readAsText(file);
  9. reader.onload = function(event) {
  10. var csvData = event.target.result;
  11. var parsedCSV = d3.csv.parseRows(csvData);
  12. parsedCSV.forEach(function(d, i) {
  13. if (i == 0) return true; // skip the header
  14. if (d.constructor === Array) {
  15. createForm(d);
  16. }
  17. });
  18. }
  19. }
  20. function createForm(csv) {
  21. out += &#39;&lt;input value=&quot;&#39; + csv[0] + &#39;&quot; name=&quot;date&quot; type=&quot;hidden&quot;&gt;&#39;;
  22. out += &#39;&lt;input value=&quot;&#39; + csv[1] + &#39;&quot; name=&quot;name&quot; type=&quot;hidden&quot;&gt;&#39;;
  23. out += &#39;&lt;input value=&quot;&#39; + csv[2] + &#39;&quot; name=&quot;in-time&quot; type=&quot;hidden&quot;&gt;&#39;;
  24. out += &#39;&lt;input value=&quot;&#39; + csv[3] + &#39;&quot; name=&quot;out-time&quot; type=&quot;hidden&quot;&gt;&#39;;
  25. document.getElementById(&#39;csvForm&#39;).innerHTML = out;
  26. document.getElementById(&#39;csvForm&#39;).setAttribute(&quot;formmethod&quot;, &quot;post&quot;);
  27. document.getElementById(&#39;csvForm&#39;).setAttribute(&quot;formaction&quot;, &quot;testBrain.jsp&quot;);
  28. out += &#39;&lt;br&gt;&#39;;
  29. }

<!-- language: lang-html -->

  1. &lt;input id=&quot;upload&quot; type=&quot;file&quot;&gt;
  2. &lt;form id=&quot;csvForm&quot;&gt;&lt;/form&gt;
  3. &lt;input type=&quot;submit&quot; value=&quot;submit&quot; form=&quot;csvForm&quot; formmethod=&quot;post&quot; formaction=&quot;testBrain.jsp&quot;&gt;
  4. &lt;script src=&quot;https://d3js.org/d3.v3.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

testBrain.jsp

  1. &lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
  2. &lt;%@page import=&quot;java.sql.*,java.util.*&quot;%&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;meta http-equiv=&quot;Refresh&quot; content=&quot;5;url=test.jsp&quot;&gt;
  6. &lt;/head&gt;
  7. &lt;/html&gt;
  8. &lt;%
  9. String[] date=request.getParameterValues(&quot;date&quot;);
  10. String[] name=request.getParameterValues(&quot;name&quot;);
  11. String[] inTime=request.getParameterValues(&quot;in-time&quot;);
  12. String[] outTime=request.getParameterValues(&quot;out-time&quot;);
  13. try
  14. {
  15. Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
  16. Connection conn = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/testDb&quot;, &quot;root&quot;, &quot;1234&quot;);
  17. Statement st=conn.createStatement();
  18. for(int x=0; x&lt;name.length;x++) {
  19. int i = st.executeUpdate(&quot;insert into csv(date,name,`in`,`out`)values(&#39;&quot; + date[x] + &quot;&#39;,&#39;&quot; + name[x] + &quot;&#39;,&#39;&quot; + inTime[x] + &quot;&#39;,&#39;&quot; + outTime[x] + &quot;&#39;)&quot;);
  20. }
  21. out.println(&quot;Data is successfully inserted! Redirecting.. Please wait&quot;);
  22. // String redirectURL = &quot;test.jsp&quot;;
  23. // response.sendRedirect(redirectURL);
  24. }
  25. catch(Exception e)
  26. {
  27. System.out.print(e);
  28. e.printStackTrace();
  29. out.println(&quot;Error&quot;);
  30. }
  31. %&gt;

I used this question and changed some stuff for it to work.
You can view the content before submitting it by removing the &quot;type = hidden&quot; in the createForm JS.

huangapple
  • 本文由 发表于 2020年5月5日 20:35:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定