时间:2015-11-15 20:22:14

标签: java mysql jdbc servlet-3.0

我需要帮助。我正在开发用于上传文本文件的应用程序。这一刻我遇到了一些我无法理解的错误。这是我的代码:

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Servlet based API for querying text file</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
</head>
<body>
    <h3>Please choose file</h3>
    <div class="container">
    <form action="TextProccessing" method="post"
        enctype="multipart/form-data">

        <div class="form-group">
            <label for="browseTxt">Upload file:</label> 
            <input type="file" name="browseTxt" id="browseTxt" value="select text file">
            <p class="help-block">
                <b>Note:</b> Please choose .txt file only
            </p>
        </div>

        <BUTTON class="btn btn-default" type="submit">Upload</BUTTON>

    </form>
    </div>

    <h2>Place for text</h2>
    <div class="container"></div>
</body>
</html>

TextProccessing

package com.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig(maxFileSize = 16177215)
public class TextProccessing extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final static String RESULT_PAGE = "/result.jsp";
    private final String dbURL = "jdbc:mysql://127.0.0.1:3306/textfilesdb";
    private final String dbUser = "root";
    private final String dbPassword = "root";
    // for this moment
    private String file_Name_test = "file_Name_test";
    private String file_size_test = "file_size_test";
    private String fileCreationDate_test = "fileCreationDate_test";

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        InputStream inputStream = null;

        Part filePart = request.getPart("browseTxt");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }

        Connection conn = null; // connection to the database
        String message = null; // message will be sent back to client

        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);

            // constructs SQL statement
            String sql = "INSERT INTO files(file_Name, file_size, fileCreationDate, text_file) values (?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, file_Name_test);
            statement.setString(2, file_size_test);
            statement.setString(3, fileCreationDate_test);

            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(4, inputStream);
            }

            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved into database";
            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }

            // sets the message in request scope
            request.setAttribute("Message", message);
            getServletContext().getRequestDispatcher(RESULT_PAGE).forward(request, response);
        }

    }
}

似乎没问题,但我有这样的错误堆栈跟踪

  

browseTxt 1189 text / plain   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,在'?le_Name附近使用正确的语法,   file_size,?leCreationDate,text_file)值('?le_Name_test',   第1行的'file_'   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(未知   来源)at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知   来自java.lang.reflect.Constructor.newInstance(未知来源)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:400)at   com.mysql.jdbc.Util.getInstance(Util.java:383)at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)at at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)at at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)at at   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)at at   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)at at   com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)at   com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)     在   com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)     在   com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)     在   com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)     在com.controller.TextProccessing.doPost(TextProccessing.java:68)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:648)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1526)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1482)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)

我的数据库结构

创建数据库TextFilesDB;

使用TextFilesDB;

  

CREATE TABLE filesid int(11)NOT NULL AUTO_INCREMENT,
  file_Name varchar(45)NOT NULL,file_size varchar(45)DEFAULT   NULL,fileCreationDate varchar(45)DEFAULT NULL,text_file   mediumblob,PRIMARY KEY(id))ENGINE = InnoDB DEFAULT CHARSET = utf16

1 个答案:

答案 0 :(得分:2)

您的列名称似乎有一些格式问题。你能尝试替换

吗?
INSERT INTO files(file_Name, file_size, fileCreationDate, text_file) values (?, ?, ?, ?)

通过

INSERT INTO files(file_Name, file_size, fileCreationDate, text_file) values (?, ?, ?, ?)