[英]error in your SQL syntax near
I need help. 我需要帮助。 I'm developing app for uploading text files.
我正在开发用于上传文本文件的应用程序。 For this moment I encountered error which I cannot clearly understand.
目前,我遇到了我无法清楚理解的错误。 Here is my code:
这是我的代码:
Index.jsp 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);
}
}
}
it seems ok but I have such error stacktrace 似乎还可以,但我有这样的错误stacktrace
browseTxt 1189 text/plain com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
BrowseTxt 1189 text / plain com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误。 check the manual that corresponds to your MySQL server version for the right syntax to use near '?le_Name, file_size, ?leCreationDate, text_file) values ('?le_Name_test', 'file_' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at 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 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545) at com.mysql.jdbc.PreparedStatement.executeInter
检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在sun.reflect.NativeConstructorAccessorImpl.newInstance0(在第1行使用接近的'?le_Name,file_size,?leCreationDate,text_file)值('?le_Name_test','file_' com.mysql.jdbc.Util处java.lang.reflect.Constructor.newInstance(Unknown Source)处sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)处sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)处的本机方法。 com.mysql.jdbc.Util.getInstance(Util.java:383)的com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)的com.mysql.jdbc.MysqlIO的handleNewInstance(Util.java:400) com.mysql.jdbc上的.checkErrorPacket(MysqlIO.java:3847)。com.mysql.jdbc上的com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)上的.checkErrorPacket(MysqlIO.java:3783)。 com.mysql.jdbc.ConnectionImpl.execSQL上的MysqlIO.sqlQueryDirect(MysqlIO.java:2594)com.mysql.jdbc.PreparedStatement.executeInter上的MySQL(ConnectionImpl.java:2545) nal(PreparedStatement.java:1901) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034) at com.controller.TextProccessing.doPost(TextProccessing.java:68) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperVal
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)的nal(PreparedStatement.java:1901)com.mysql.jdbc.PreparedStatement的com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)的nal。 javax.servlet.http.HttpServlet.service(HttpServlet.java:648)上com.controller.TextProccessing.doPost(TextProccessing.java:68)上的.executeUpdate(PreparedStatement.java:2034)在javax.servlet.http.HttpServlet上。 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)的服务(HttpServlet.java:729)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)的服务位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)的.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain .java:206),位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperVal ve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482) at java.util.concurrent.Thr
ve.java:219)在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)在org.apache.catalina org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)上的.core.StandardHostValve.invoke(StandardHostValve.java:142)在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) )在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)在org.apache.coyote.http11.AbstractHttp11Processor。在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractHttptoProcess.java:673)处的进程(AbstractHttp11Processor.java:1091)在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1526)处的进程(AbstractProtocol.java:673)在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1482)在java.util.concurrent.Thr eadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)
eadPoolExecutor.runWorker(来源不明),位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(来源不明),位于org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61),位于java.lang .Thread.run(未知来源)
And my DB structure 而我的数据库结构
create database TextFilesDB; 创建数据库TextFilesDB;
use TextFilesDB; 使用TextFilesDB;
CREATE TABLE
files
(id
int(11) NOT NULL AUTO_INCREMENT,创建表
files
(id
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=utf16file_Name
varchar(45)非空,file_size
varchar(45)默认为空,fileCreationDate
varchar(45)默认为空,text_file
mediumblob,主键(id
))ENGINE = InnoDB DEFAULT CHARSET = utf16
It seems that you have some format problem with the columns names. 似乎您的列名称存在格式问题。 Can you try to replace
你能替换吗
INSERT INTO files(file_Name, file_size, fileCreationDate, text_file) values (?, ?, ?, ?)
by 通过
INSERT INTO files(file_Name, file_size, fileCreationDate, text_file) values (?, ?, ?, ?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.