2016-07-05 84 views
0

我正在采取usernamecolumn namenew entry从用户在jsp更新。 我想用jstl标签更新我的SQL数据库表,并且还显示更新后的表。 jsp页面未打开,它在我的更新sql查询中给出语法错误消息。sql语法错误(使用jstl标签)

我的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"/> 

uDate公司的SQL查询 -

它是形式(更新TABLE_NAME组table_column = “XYZ”,其中列名= “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语句检查任何错误的项目或空值时提交错误输入重定向到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); 
    } 
} 
} 
+0

添加单引号arround char值 – Jens

+0

你的意思是这样的------更新usertable设置'$ {param.use}'='{param.enter}'其中username ='$ {参数。用户}'; –

+0

它不工作 –

回答

2

您的查询将不会工作,直到这些东西被修改:

  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>

  2. 您是直接同一页面上发射的查询,即你已经显示了该框,并且紧随其后,您的查询在那里进行更新。

这会导致问题,因为JSTL总是第一个运行在服务器端,因此不要紧,你写它,它会首先执行,现在在执行该查询,JSTL开创的参数为空,所以现在查询变得像this.:-

Update table-name set null=null where user-name=null;

您可以在null=null列名看到的是自己为空,可以考虑null值,但你的列名也就是null,而且不存在于数据库或表中。

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

+0

在上面的代码中,我已经给出了从jsp传递值并在servlet中使用然后在servlet中有一些错误,你可以检查它的替代方法。 –

+0

是的,你的代码有错误。你提交的表单没有设置任何参数来检测servlet的去向,并且如果我没有弄错,那么你可能会得到任何参数值为空或空指针异常(当在查询中使用这个空参数时)。错误(其实不是错误,而是错误)在

标记上,设置操作以指示要去哪里以及要做什么。 –