2 a7558456 a7558456 于 2016.05.04 17:28 提问

JDBC关于数据更新的错误 1C

错误信息是: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("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
while (rs.next()) {
out.println("");
number = rs.getString(1);
out.println("");
xingming = rs.getString(2);
out.println("");
math = rs.getInt(3);
out.println("");
english = rs.getInt(4);
out.println("");
pe = rs.getInt(5);
out.println("");
out.println("");
}
out.println("
" + "学号" + "" + "姓名" + "" + "数学成绩" + "" + "英语成绩" + "" + "体育成绩" + "
" + number + "" + xingming + "" + math + "" + english + "" + pe + "
con.close();
} catch (SQLException e2) {
System.out.println(e2.getMessage());
}
%>


4个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.05.04 17:34
 粗看
 + "WHERE
 这where前面没有空格
z1150309744
z1150309744   2016.05.04 17:31

你一共有三个更新语句,因此,测试下,到底是哪个出现问题,再来解决

CSDNXIAON
CSDNXIAON   2016.05.04 17:32

jdbc批量更新数据
mysql jdbc 更新数据
批量更新数据引起 DataGrid 的绘制错误及解决方法
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

qq_30408111
qq_30408111   2016.05.04 17:39

sql.executeUpdate(condition1);
sql.executeUpdate(condition2);
sql.executeUpdate(condition3);
我刚刚也遇到了问题,不知道和你的是不是一样,原因就是你连着更新三条语句,在第一天执行完之后,还没提交,就执行第二个语句,这其中涉及到一个事务的回滚,你的那三条语句就相当是一个事务,如果有一天语句出错事务就会出现问题,所以会出错,解决方法时在每条执行语句执行完之后,先提交了(加一条提交语句)然后在执行下一条,你试试吧

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