错误信息是: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">
Insert title here
<%
String name = request.getParameter("name");
name = name.trim();
if (name == null) {
name = "";
}
byte b[] = name.getBytes("ISO-8859-1");
name = new String(b);
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);
%>
数据修改后的表的记录
<%
rs = sql.executeQuery("SELECT * FROM students");
out.println("
" + "学号" + " | ");" + "姓名" + " | ");" + "数学成绩" + " | ");" + "英语成绩" + " | ");" + "体育成绩" + " | ");
---|---|---|---|---|
" + number + " | ");" + xingming + " | ");" + math + " | ");" + english + " | ");" + pe + " | ");
} catch (SQLException e2) {
System.out.println(e2.getMessage());
}
%>