java - sql语法错误(使用jSTL标签)

标签 java mysql jsp servlets

我正在jsp中从用户那里更新用户名列名新条目。 我正在尝试使用 jSTL 标签更新我的 SQL 数据库表并显示更新后的表。 jsp 页面未打开,它在我的更新 sql 查询中给出语法错误消息。

我的jsp页面:-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Found page</title>
    <style>
        a:link, a:visited {
            background-color:darkolivegreen;
            color: white;
            padding: 10px 25px;
            text-align: center;
            width:100px; 
            text-decoration: none;
            display: inline-block;
            border-radius:20px; 
        }


        a:hover, a:active {
            background-color: lightgreen;
        }
        header {
            background-color:teal;
            color:white;
            text-align:center;
            padding:5px;
        }
         #file {
            font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;

        }

        #file td, #file th {
            border: 2px solid black;
            text-align: left;
            padding: 8px;                
        }            

        #file tr:hover {background-color: #ddd;} 

        #file th {
            padding-top: 12px;
            padding-bottom: 12px;
            background-color: lightslategray;
            color: white;
        }
        section {
            height:270px; 
            width:1050px;
            float:right;
            padding:87px;
        }
        footer {
            background-color:black;
            float:bottom;
            color:white;
            clear:both;
            text-align:center;
            padding:5px;
        }
    </style>
</head>
<body style="background-color:lightsteelblue;">
    <%
        String userName = null;
        String sessionID = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("user")) {
                    userName = cookie.getValue();
                }
            }
        }
    %>
    <header>
        <h1>File Tracking System</h1>
        <div><span style="float:right">Hi <%=userName%></span></div> 
        <br>
        <form  style="float:right" action=" LogoutServlet" method="post">
            <input type="submit" value="Logout" >
        </form>
        <br>
    </header>
    <br>
    <a href="file.jsp">1.Insert New File</a>        
    <a href="fileStatus.jsp">2.Change File Status</a>
    <a href="found.jsp">3.Search your File</a>
    <a href="checkstatus.jsp">4.Check File Status</a> 
    <br>
    <br>
    <form method="POST">
        User ID:<br><input type="text" name="user" value="" size="20" />

下拉列表中的这些选项的拼写与我在 userdetails 表中的列名称相同

        Alter:<br><select name="use">
            <option>userName</option>
            <option>password</option>
            <option>role</option>
            <option>firstname</option>
            <option>lastname</option>
            <option>departmentname</option>
            <option>email</option>
            <option>mobile</option>
        </select>
        New Entry:<br><input type="text" name="enter" value="" size="20" />
        <input type="submit" value="submit" name="submit" />
    </form>
    <br>
    <section>
        <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
                           url="jdbc:mysql://localhost/login"
                           user="root"  password="root"/>

更新sql查询-

其格式为 ( update table_name set table_column="xyz"where column_name= "abc"; )

        <sql:update dataSource="${snapshot}" var="result">
            update usertable set "${param.use}"="${param.enter}" where username="${param.user}";
        </sql:update>
        <sql:query dataSource="${snapshot}" var="result">
            select * from usertable where username="${param.user}";
        </sql:query>

        <table id="file">
            <tr>  
                <th>UserName</th>
                <th>Password</th>
                <th>Role</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Department Name</th>
                <th>Email</th>
                <th>Mobile Number</th>
            </tr>
            <c:forEach var="row" items="${result.rows}">
                <tr>
                    <td><c:out value="${row.username}"/></td>
                    <td><c:out value="${row.password}"/></td>
                    <td><c:out value="${row.role}"/></td>
                    <td><c:out value="${row.firstname}"/></td>
                    <td><c:out value="${row.lasttname}"/></td>
                    <td><c:out value="${row.departmentname}"/></td>
                    <td><c:out value="${row.email}"/></td>
                    <td><c:out value="${row.mobile}"/></td>
                </tr>
            </c:forEach>
        </table>
    </section>
    <footer>
        Copyright 2016 NSIC. All right reserved.                             
    </footer>
</body>
</html>

正确的sql语法应该是什么?我还有其他方法可以做到这一点吗? 我使用了另一种方法,在 alter.jsp 中提交值并将它们放入 servlet changeServlet.java 中,但它没有显示任何错误,也没有被重定向到任何地方只是打开http://localhost:8080/test9/changeServlet并卡在此处,显示白色的空白页。

package bean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class changeServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //request.getRequestDispatcher("link.html").include(request, response);
    Cookie[] cookies = request.getCookies();
    if(cookies != null){
    for(Cookie cookie : cookies){
        if(cookie.getName().equals("JSESSIONID")){
            System.out.println("JSESSIONID="+cookie.getValue());
            break;
        }
    }
    }       
    HttpSession session = request.getSession(false);
    System.out.println("admin="+session.getAttribute("admin"));
   if(session!=null && session.getAttribute("admin") != null){
               String admin=(String)session.getAttribute("admin"); 
                boolean status=false;
    try{
        String user=request.getParameter("user");
        String column=request.getParameter("column");
        String data=request.getParameter("data");            
        boolean checkc=checkchar.value(column);
        boolean checkp=checkchar.value(data);

if 语句正在检查是否有任何错误条目或 null 值,当提交错误条目时,它会重定向到 entry.jsp。这意味着它在此之前一直有效,但在此之后就不起作用了。

        if(checkc==true&&checkp==true) {
        Connection con=ConnectionProvider.getCon();
        String sql="update usertable set '"+column+"'='"+data+"' where username='"+user+"'";
        PreparedStatement pstmt =con.prepareStatement(sql);

        int rs=pstmt.executeUpdate();
        if(rs>0){status=true;}

            if(status){
                  PrintWriter out= response.getWriter();
                  out.print("data updated,"+admin);
                  response.sendRedirect("updated.jsp");
                        }
              else 
              {   
                  PrintWriter out= response.getWriter();
                  out.print("failed to update");
                  response.sendRedirect("notinsert.jsp");
              }
            }
        else{response.sendRedirect("entry.jsp");}
            }catch(SQLException e){}  
              }else{
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.html");
    PrintWriter out= response.getWriter();
    out.println("<font color=red>Either user name or password is wrong.</font>");
    rd.include(request, response);
    }
}
}

最佳答案

在修改这些内容之前,您的查询将不起作用:

  1. 您的选项标签

<option value="userName">userName</option>
<option value="password">password</option>
<option value="role">role</option>
<option value="firstname">firstname</option>
<option value="lastname">lastname</option>
<option value="department">departmentname</option>
<option value="email">email</option>
<option value="mobile">mobile</option>

  1. 您直接在同一页面上触发查询,即您显示了该框,紧接着您的查询就在那里等待更新。

这会导致问题,因为 JSTL 总是首先在服务器端运行,所以无论你在哪里编写它,它都会首先执行,现在在执行该查询时,jSTL 发现参数为空,所以现在查询变成了一些东西像这样:-

UPDATE TABLE `table-name` SET `null`=null where `user-name`=null;`

您可以在 null=null 中看到,column-name 本身就是 null,可以考虑使用 null 值,但您的列名称也是 null,并且数据库或表中不存在。

所以请把它分成两个jsp页面,一个将收集信息并将它们作为参数传递给另一个jsp页面,现在这个页面必须包含查询,现在它可以正确获取查询中的参数值。

关于java - sql语法错误(使用jSTL标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196336/

相关文章:

mysql - 如何在多列索引中对列进行排序以获得 Mysql 中的最佳性能

mysql - 如何从一系列行中随机提取 N 条记录并不断迭代?

eclipse - 使用 jsp 和 session 创建一个简单的登录页面

JSP 在/WEB-INF 返回 "HTTP Status 404 The requested resource is not available"

java - 遍历列表后得到一个总和

c# - MySQL连接器通用SELECT多表查询方法

java - 通过 Rest 调用在我的 Java 类中获取 JSON 结果

java - Tomcat 8.0中运行的JSP如何加载非Config文件

java - 在 JAVA 中将数据从文本文件传输到 MySQL 表时出现 OutOfMemory 错误

java - 来自 RCC(8) 规范或类似规范的维恩图生成软件