首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >sql语法错误(使用jstl标记)

sql语法错误(使用jstl标记)
EN

Stack Overflow用户
提问于 2016-07-05 14:13:09
回答 1查看 784关注 0票数 0

我把usernamecolumn namenew entry从一个jsp中的用户更新。我正在尝试使用jstl标记更新我的SQL数据库表,并显示更新后的表。jsp页面没有打开,它在我的update sql查询中给出一条语法错误的消息。

我的jsp页面:

代码语言:javascript
复制
<%@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表中我的列名相同

代码语言:javascript
复制
        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"/>

udate sql查询-

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

代码语言:javascript
复制
        <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,并停留在这里显示一个白色空白页面。

代码语言:javascript
复制
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语句检查任何错误的条目或空值,当提交错误的条目时,它将重定向到entry.jsp。这意味着它在这里之前是工作的,但在这个之后就不工作了。

代码语言:javascript
复制
        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);
    }
}
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38196336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档