检查以下语法错误:<body> <a href="javascript:history.go(-1);">返回 </a> <a href="Main.jsp">主页</a> <br> <br> 编写人姓名: 胡莲 学号:20212310930228 <br> <% request.setCharacterEncoding("UTF-8"); String sxh = request.getParameter("xh").trim(); String sname = request.getParameter("name").trim(); String sclassname = request.getParameter("classname").trim(); int sage = Integer.parseInt(request.getParameter("age")); String saddress = request.getParameter("address").trim(); String sphone = request.getParameter("phone").trim(); String scity = request.getParameter("city").trim(); String snotes = request.getParameter("notes").trim(); String sql = "update student set name='" + sname + "'" + ",classname='" + sclassname + "'" + ",age='" + sage + "'"+ "'" + ",address='" + saddress + "'"+ "'" + ",phone='" + sphone + "'"+ "'" + ",city='" + scity + "'"+ "'" + ",notes='" + snotes + "'"+ "where xh='" + sxh + "'"; try { java.sql.Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动程序 conn = java.sql.DriverManager .getConnection( "jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8", "root", "123456"); //URL指向数据库student PreparedStatement pstatement = conn.prepareStatement(sql); //建立SQL语句 pstatement.execute(); //执行SQL语句 out.print("学生(" + sxh + "," + sname + "," + sclassname + "," + sage + ")");//打印sql out.println("信息更新成功!"); pstatement.close(); conn.close(); } catch (SQLException e1) { System.out.print("sql语句失败!"); e1.printStackTrace(); } catch (Exception e2) { System.out.print("sql语句失败!"); e2.printStackTrace(); } %> </body>
时间: 2023-07-08 14:11:54 浏览: 158
php-check:检查您的代码是否存在语法错误和错误
代码存在以下语法错误:
1. 第8行缺少一个空格,应该是"返回 "而不是"返回";
2. 第14行多了一个单引号,应该是",age='" + sage + "'"而不是",age='" + sage + "''";
3. 第15行多了一个单引号,应该是",address='" + saddress + "'"而不是",address='" + saddress + "''";
4. 第16行多了一个单引号,应该是",phone='" + sphone + "'"而不是",phone='" + sphone + "''";
5. 第17行多了一个单引号,应该是",city='" + scity + "'"而不是",city='" + scity + "''";
6. 第18行多了一个单引号,应该是",notes='" + snotes + "'"而不是",notes='" + snotes + "''";
7. 第25行应该是out.print而不是out.println,因为后面没有换行符。
正确的代码如下所示:
```html
<body>
<a href="javascript:history.go(-1);">返回 </a>
<a href="Main.jsp">主页</a>
<br>
<br>
<%
request.setCharacterEncoding("UTF-8");
String sxh = request.getParameter("xh").trim();
String sname = request.getParameter("name").trim();
String sclassname = request.getParameter("classname").trim();
int sage = Integer.parseInt(request.getParameter("age"));
String saddress = request.getParameter("address").trim();
String sphone = request.getParameter("phone").trim();
String scity = request.getParameter("city").trim();
String snotes = request.getParameter("notes").trim();
String sql = "update student set name='" + sname + "'"
+ ",classname='" + sclassname + "'"
+ ",age='" + sage + "'"
+ ",address='" + saddress + "'"
+ ",phone='" + sphone + "'"
+ ",city='" + scity + "'"
+ ",notes='" + snotes + "'"
+ "where xh='" + sxh + "'";
try {
java.sql.Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动程序
conn = java.sql.DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8",
"root", "123456"); //URL指向数据库student
PreparedStatement pstatement = conn.prepareStatement(sql); //建立SQL语句
pstatement.execute(); //执行SQL语句
out.print("学生(" + sxh + "," + sname + "," + sclassname + "," + sage
+ ")");//打印sql
out.print("信息更新成功!");
pstatement.close();
conn.close();
} catch (SQLException e1) {
System.out.print("sql语句失败!");
e1.printStackTrace();
} catch (Exception e2) {
System.out.print("sql语句失败!");
e2.printStackTrace();
}
%>
</body>
```
阅读全文