错误信息是: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
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
String newmath = request.getParameter("mathGrade");
if (newmath == null) {
newmath = "-100";
}
String newenglish = request.getParameter("englishGrade");
if (newenglish == null) {
newenglish = "100";
}
String newpe = request.getParameter("peGrade");
if (newpe == null) {
newpe = "-100";
}
Connection con = null;
Statement sql = null;
ResultSet rs = null;
String xingming, number;
int math, english, pe;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驱动程序加载错误");
}
try {
con = DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/grade_db?useUnicode=true&characterEncoding=utf-8&useSSL=false",
"root", "123456");
sql = con.createStatement();
String condition1 = "UPDATE students SET englishGrade="
+ newenglish + "WHERE name=" + "'" + name + "'";
String condition2 = "UPDATE students SET mathGrade=" + newmath
+ "WHERE name=" + "'" + name + "'";
String condition3 = "UPDATE students SET peGrade=" + newpe
+ "WHERE name=" + "'" + name + "'";
sql.executeUpdate(condition1);
sql.executeUpdate(condition2);
sql.executeUpdate(condition3);
%>
数据修改后的表的记录
" + "学号" + " | " + "姓名" + " | " + "数学成绩" + " | " + "英语成绩" + " | " + "体育成绩" + " |
---|---|---|---|---|
" + number + " | " + xingming + " | " + math + " | " + english + " | " + pe + " |
你一共有三个更新语句,因此,测试下,到底是哪个出现问题,再来解决
粗看
+ "WHERE
这where前面没有空格
sql.executeUpdate(condition1);
sql.executeUpdate(condition2);
sql.executeUpdate(condition3);
我刚刚也遇到了问题,不知道和你的是不是一样,原因就是你连着更新三条语句,在第一天执行完之后,还没提交,就执行第二个语句,这其中涉及到一个事务的回滚,你的那三条语句就相当是一个事务,如果有一天语句出错事务就会出现问题,所以会出错,解决方法时在每条执行语句执行完之后,先提交了(加一条提交语句)然后在执行下一条,你试试吧