2 kuangshow0227 kuangshow0227 于 2016.03.30 11:05 提问

【Java】查询数据时报错,请各大神指点~(附源码) 10C

请求流程:
1、访问SearchList.jsp时请求servlet:SearchById;
2、SearchById 的doPost方法访问showInfoDao.findModeResult(sql);
3、JdbcUtils中对sql进行数据库查询,并将查询结果返回,展示在SearchList.jsp页面;

在第3步,数据库连接成功,查询的时候出现了“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;”
报错信息如下:

 doGet 请求
doPost请求
数据库连接成功!
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??personinfo' 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: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:3515)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)

    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
    at com.bdwm.dao.ShowInfoDao.findModeResult(ShowInfoDao.java:18)
    at com.bdwm.servlet.SearchById.doPost(SearchById.java:71)
    at com.bdwm.servlet.SearchById.doGet(SearchById.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
    at java.util.concurrent.ThreadPoolExecutor.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)
[]

6个回答

javaDesire
javaDesire   2016.03.30 11:18

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??personinfo' at line 1

danielinbiti
danielinbiti   Ds   Rxr 2016.03.30 11:12
 check the manual that corresponds to your MySQL server version for the right syntax to use near 'from??personinfo' at line 1
 这不提示很明显,是sql有问题吗,  from和personinfo中间两个??,看看是不是编码有问题了
 把那个sql拷贝出来,在记事本中粘贴一下,然后再从记事本中拷贝到代码中试试
kuangshow0227
kuangshow0227 我用的是select *;String sql = "select * from personinfo";
一年多之前 回复
qq_29829081
qq_29829081   2016.03.30 11:19

这是异常是因为SQL语句出错了。错误提示里面说:“from??personinfo”这个位置附近出错了。你注意两点:第一,看看SQL语句里面表的名称有没有输入错误;第二,如果是用字符串拼接的SQL语句,一定要注意两个字符串之间的
”空格“(比如sql1=“select * from student”; sql2="where id = 3"; sql = sql1 +sql2, 最终的sql语句就成了sql= "select * from studentwhere id = 3"。此时,student和where就成了一个单词,导致sql语句出错。因此,在拼接时给sql1最后,或者sql2之前加一个空格就好了,例如sql2="where id = 3"; sql = sql1 +sql2, 最终的sql语句就成了sql= "空格符select * from studentwhere id = 3")。
希望能够帮到你!

kuangshow0227
kuangshow0227 回复秦时明月_: 把代码里面的语句复制到了mysql里面,可以查询成功呢,我也看到了两个问号,但程序里面只有这一个sql语句,也不明白为何错误提示里面有两个问号?
一年多之前 回复
qq_29829081
qq_29829081 回复霸气帅洋洋: at java.lang.reflect.Constructor.newInstance(Unknown Source)。从这句提示能看出来可能是什么名称写错了,找不到资源。
一年多之前 回复
qq_29829081
qq_29829081 回复霸气帅洋洋: 你试着将你的sql语句复制到数据库客户端,SQLyog里面看看是哪里出错了?
一年多之前 回复
qq_29829081
qq_29829081 回复霸气帅洋洋:错误提示里面那两个问号是什么?
一年多之前 回复
kuangshow0227
kuangshow0227 我的查询语句没有拼接,语句为:String sql = "select * from personinfo";
一年多之前 回复
beijing20110905
beijing20110905   Rxr 2016.03.30 11:45

SQL语句写的不对。断点调试。取到SQL语句的内容,放到mysql数据库验证下语句是否能正确执行。如果能正确执行,但在代码中报错,可以试着在语句末尾加空格或去掉别名。

zhangjs712
zhangjs712   2016.03.30 13:50

把SQL语句发出来,看下啊

kuangshow0227
kuangshow0227 sql语句:String sql = "select * from personinfo";
一年多之前 回复
qq_20039385
qq_20039385   2016.03.31 22:35

错误提示很明显的说:你的sql语法错吴,把sql贴错来看看。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!