我的jsp页面没有使用结果集rs = st.executeQuery();

时间:2017-08-16 09:19:53

标签: java mysql jsp

This is my file name SEFHTML1.jsp

<%@ page import ="java.sql.*" %>
<%@page import ="java.io.*" %>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<style>
/* Full-width input fields */
input[type=text], input[type=password] {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

/* Set a style for all buttons */
button {
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    cursor: pointer;
    width: 100%;
}

/* Extra styles for the cancel button */
.cancelbtn {
    padding: 14px 20px;
    background-color: #f44336;
}

/* Float cancel and signup buttons and add an equal width */
.cancelbtn,.signupbtn {
    float: left;
    width: 50%;
}

/* Add padding to container elements */
.container {
    padding: 16px;
}

/* Clear floats */
.clearfix::after {
    content: "";
    clear: both;
    display: table;
}

/* Change styles for cancel button and signup button on extra small screens */
@media screen and (max-width: 300px) {
    .cancelbtn, .signupbtn {
       width: 100%;
    }
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<h2>Student Entry Form</h2>
<form method=post action="SEFHTML.jsp"></form>
  <div class="container">
<label><b>RegistrationNumber</b></label>
    <input type="text" placeholder="Enter RegistrationNumber" name="RegistrationNumber" required>
     <label><b>Name</b></label>
    <input type="text" placeholder="Enter your Name" name="Name" required>
     <label><b>Course</b></label>
    <input type="text" placeholder="Enter your Course" name="Course" required>
    <label><b>Batch</b></label>
    <input type="text" placeholder="Enter your Batch" name="Batch" required>
          <button type="submit" class="submitbtn">Submit</button>
    <%
String RegistrationNumber=request.getParameter("RegistrationNumber");
String Name=request.getParameter("Name");
String Course=request.getParameter("Course");
String Batch=request.getParameter("Batch");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
ResultSet rs;
Statement st = con.createStatement();
>>>>>>>>>>>>>>>>>90) rs=st.executeQuery("SELECT * FROM customers1 WHERE RegistrationNumber='" + RegistrationNumber + "'Name='" + Name + "'Course='" + Course + "' and Batch='" + Batch + "'");
if (rs.next()) {
      session.setAttribute("RegistrationNumber",RegistrationNumber);
      session.setAttribute("Name",Name);
      session.setAttribute("Course",Course);
      session.setAttribute("Batch",Batch);

   out.println("Welcome " + Name);
   //out.println("<a href='logout.jsp'>Log out</a>");
    response.sendRedirect("facultyentryform.jsp");
}
else
{
    out.println("Invalid Details Please try again");
}
%>
<button type="button">Submit</button>
h1>Student Succesfully Logged in</h1>

我的问题是,当我尝试验证RegistrationNumber,Name,Course,Batch时,它在第90行显示错误(我在代码中提到了行号)。我在mysql DB中创建了一个名为customers1的表,MYSQL中的一切都很好D B。 该页面必须从SFEHTML1.jsp指向facultyentryform.jsp

facultyentryform.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import ="java.sql.*" %>
<%@page import ="java.io.*" %>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Faculty Entry Form</title>
<label><b>FacultyID</b></label>
    <input type="text" placeholder="Enter FacultyID" name="FacultyID" required>
     <label><b>FacultyName</b></label>
    <input type="text" placeholder="Enter FacultyName" name="FacultyName" required>
     <label><b>Designation</b></label>
    <input type="text" placeholder="Enter Designation" name="Designation" required>
</head>
<body>
<%
String FacultyID=request.getParameter("FacultyID");
String FacultyName=request.getParameter("FacultyName");
String Designation=request.getParameter("Designation");
Class.forName("com.mysql.jdbc.Driver");
ResultSet rs;
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
PreparedStatement st = con.prepareStatement("INSERT INTO customers2(FacultyID,FacultyName,Designation) VALUES(?,?,?)");
st.setString(1,FacultyID);
st.setString(2,FacultyName);
st.setString(3,Designation);
int i=st.executeUpdate();
if(i!=0)
{
response.sendRedirect("bookentryform.jsp"); 
out.print("Welcome");
}
else
{
    response.sendRedirect("facultyentryform.jsp");
}
%>
</body>
</html>

Tomcat服务器异常

87: Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
88: ResultSet rs;
89: Statement st = con.createStatement();
90: rs=st.executeQuery("SELECT * FROM customers1 WHERE RegistrationNumber='" + RegistrationNumber + "'Name='" + Name + "'Course='" + Course + "' and Batch='" + Batch + "'");
91: if (rs.next()) {
92:       session.setAttribute("RegistrationNumber",RegistrationNumber);
93:       session.setAttribute("Name",Name);


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'Name='null'Course='null' and Batch='null'' at line 1
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:669)
    org.apache.jsp.SEFHTML1_jsp._jspService(SEFHTML1_jsp.java:238)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'Name='null'Course='null' and Batch='null'' at line 1
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
    com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
    org.apache.jsp.SEFHTML1_jsp._jspService(SEFHTML1_jsp.java:208)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.

这是apache tomcat服务器异常。我创建了一个&#34; customers1&#34; mysql中的mysql表/视图。我在mysql表中没有问题一切正常。我正在使用jsp,mysql和java.My页面必须直接从SEFHTML.jsp页面到facultyentryform.jsp。以上代码必须保存每当我尝试给新字段时,在mysql服务器中

1 个答案:

答案 0 :(得分:2)

您错过了条件之间的AND

rs=st.executeQuery("SELECT * 
  FROM customers1 
  WHERE RegistrationNumber='" + RegistrationNumber + "' AND Name='" + Name + "' AND Course='" + Course + "' and Batch='" + Batch + "'");

正如我评论的那样:

  1. 不要在jsp中直接使用jdbc。了解三层架构。
  2. 了解准备好的语句以防止SQL注入。
  3. 注意Java命名约定。属性名称应以小写字符
  4. 开头