我正在使用follwing查询进行更新。 我的更新函数和连接字符串在jar文件中。 我正在使用JSP useBean从jar文件中调用函数。 我能够在数据库上运行select查询。 只有在更新时遇到问题。 为什么我现在收到这个错误,它工作正常。
查询
String sql="INSERT INTO tm_visitor(VDT,VID,VISITOR_TEXT,COMPANY_TEXT,CONTACT,PERSON_TO_MEET,DEPARTMENT_TEXT,FLOOR)SELECT REPLACE(LEFT(NOW(), 10), '-', ''), LPAD(COALESCE(MAX(VID) + 1, 000001),6,'0'),?,?,?,?,?,? FROM tm_visitor";
QUERY2
String sql2="INSERT INTO tm_belonging(BID,BELONGING_TYPE,BELONGING_TEXT)VALUES(LAST_INSERT_ID(),?,?)";
错误:
Error in DBConnect com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Y
ou have an error in your SQL syntax; check the manual that corresponds to your M
ySQL server version for the right syntax to use near '?,?,?,?,?,? FROM tm_visito
r' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '?,?,?,?,?,? FROM tm_visitor' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3536)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3468)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1957)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2107)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1634)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1553)
at com.vms.util.MyDBConnect.exeUpdate(Unknown Source)
at org.apache.jsp.applicationservices.fileshare.vm.apps.admin.visitorSub
mit_jsp._jspService(visitorSubmit_jsp.java:148)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
13)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:48
9)
at java.lang.Thread.run(Unknown Source)
Error in DBConnect com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Y
ou have an error in your SQL syntax; check the manual that corresponds to your M
ySQL server version for the right syntax to use near '?,?)' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '?,?)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3536)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3468)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1957)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2107)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1634)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1553)
at com.vms.util.MyDBConnect.exeUpdate(Unknown Source)
at org.apache.jsp.applicationservices.fileshare.vm.apps.admin.visitorSub
mit_jsp._jspService(visitorSubmit_jsp.java:151)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
13)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:48
9)
at java.lang.Thread.run(Unknown Source)
修改
JSP CODE:
<jsp:useBean id="mdc" class="com.vms.util.MyDBConnect"/>
boolean status=false;
String VisitorName="",Company="",Contact="",WhomeToMeet="",Department="",DepartmentFloor="";
String Belonging="",BType="",Belonging2="",BType2="",Belonging3="",BType3="";
String Btype="",Belong="";
try
{
VisitorName=(String)request.getParameter("VisitorName");
out.println(","+VisitorName);
Company=(String)request.getParameter("Company");
out.println(","+Company);
Contact=(String)request.getParameter("Contact");
out.println(","+Contact);
WhomeToMeet=(String)request.getParameter("WhomeToMeet");
out.println(","+WhomeToMeet);
Department=(String)request.getParameter("Department");
out.println(","+Department);
DepartmentFloor=(String)request.getParameter("DepartmentFloor");
out.println(","+DepartmentFloor);
BType=(String)request.getParameter("BType0");
out.println(","+BType);
Belonging=(String)request.getParameter("Belonging0");
out.println(","+Belonging);
String sql="INSERT INTO tm_visitor(VDT,VID,VISITOR_TEXT,COMPANY_TEXT,CONTACT,PERSON_TO_MEET,DEPARTMENT_TEXT,FLOOR)SELECT REPLACE(LEFT(NOW(), 10), '-', ''), LPAD(COALESCE(MAX(VID) + 1, 000001),6,'0'),?,?,?,?,?,? FROM tm_visitor";
//System.out.println(sql);
String sql2="INSERT INTO tm_belonging(BID,BELONGING_TYPE,BELONGING_TEXT)VALUES(LAST_INSERT_ID(),?,?)";
//System.out.println(sql2);
mdc.exeUpdate(sql);
mdc.exeUpdate(sql2);
}
catch(Exception e)
{
e.printStackTrace();
//System.out.println("eRROR>>\n"+e);
}
答案 0 :(得分:0)
每当你使用一个?在你的陈述中,意味着它将被一个参数“替换”。
执行查询时必须绑定这些参数。
使用标准JDBC语法,它将类似于:
PreparedStatement prepStmt = connection.prepareStatement(sql);
prepStmt.setString(1, paramValue1);
prepStmt.setInt(2, paramValue2);
....
prepStmt.setInt(n, paramValueN);
prepStmt.execute();
希望有所帮助