注:以下代码与信息均贴的片段,并非完整代码。
1
错误信息:check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=5' at line 1check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=5' at line 1
检查“where id=5”附近的代码:
//Test
Person p = new Person(5,"李莲英","男",20,"110");
//Function
String sql = "update users set name=?, sex=?, age=?, phone=?, where id=? ";
观察到在“where”前多了个“,”,去掉即可:
String sql = "update users set name=?, sex=?, age=?, phone=? where id=? ";
2
错误信息: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
检查“?”附近的代码:
//Test
public void test7(){
Person p = dao.findByIdPerson(10);
System.out.println(p.toString());
}
//Function
String sql = "select * from users where id=? ";
//4.为占位符传值
pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery(sql);
修改为:
String sql = "select * from users where id= "+id;
//4.为占位符传值
// pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery(sql);
或者:
//Function
String sql = "select * from users where id=? ";
//4.为占位符传值
pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery();
错误原因:若用占位符方式传值,executeQuery();要无参。
添加字符串可有参,也可无参,最好是所有的都写成无参。
3、功能:点击“查询客户”按钮,实现显示全部客户列表。
错误信息:Servlet.service() for servlet [CustomerServlet] in context
with path [/JavaWebTest] threw exception java.lang.NullPointerException
相关代码:
<!-- top.jsp -->
<a href="CustomerServlet">查询客户</a>
//CustomerServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if(method.equals("add")){
this.doAdd(request, response);
}else if(method.equals("del")){
this.doDel(request, response);
}else if(method.equals("findid")){
this.doFindId(request, response);
}else if(method.equals("update")){
this.doUpdate(request, response);
}else{
this.doFind(request, response);
}
<a href="CustomerServlet?method=a">查询客户</a>
错误原因:空指针。没有给method传值过去,使得其为空。
正确代码:
<!-- top.jsp -->
<a href="CustomerServlet?method=a">查询客户</a>
4、编写完doSuperFind方法后,代码无报错,运行时报错。
错误信息:The method doSuperFind(HttpServletRequest, HttpServletResponse) is undefined
错误原因:重新添加了方法,服务器需重启。
5、xml文件报错
错误信息:Content is not allowed in trailing section.
<?xml version="1.0"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 默认与default的数据库建立连接 -->
<environments default="cs">
<!-- id为要连接的数据库,可同时配置多个 -->
<environment id="cs">
<!-- 事务管理,连接方式:JDBC/MANAGED -->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源:POOLED/UNPOOLED -->
<dataSource type="POOLED">
<!-- 在缓冲池中配置与数据库连接的参数 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/oa" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 配置映射信息 -->
<mappers></mappers>
</configuration>>
最后一行报错,多了个“>”。