为什么我收到< java.sql.SQL异常:ORA-00904:“密码”:无效的标识符>

mspsb9vt  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(124)

这是我的MVC应用程序的模型的一部分,它应该与数据库(Oracle 10g XE)通信。以下是我编写的代码,

package com.cid_org.model;

import java.sql.*;

public class LoginModelPOJO {

private String username;
private String password;
private Connection connection;

public LoginModelPOJO(String username, String password, Connection connection){
    this.username = username;
    this.password = password;
    this.connection = connection;
    validate();
}

private void validate(){
    try {
        String query = "SELECT * FROM CRIME_SOLVING_OFFICIAL where OFFICIAL_USERNAME=? and OFFICIAL_PASSWORD=?";
        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
             // String name=rs.getString(3);
              System.out.println("success");
        } else {
            System.out.println("access denied");
        }
    } catch (Exception e) {
        System.out.println("Connection " + e);
      }
}
}

但在执行时,它会生成以下错误:

Connection java.sql.SQLException: ORA-00904: "PASSWORD": invalid identifier

以下是我在Oracle中创建的表,

这是图像

顺便说一句,我知道这个问题会被贴上重复问题的标签,确实是这样,但仅供参考我已经浏览了所有与这个问题相关的帖子(你可以在图片中看到所有在Firefox中打开的标签),但没有找到任何答案。

编辑:我已经使所有的‘password’字符串消失,以突出正在谈论的‘password’标识符异常。我还将FORMANCE_PASSWORD的列名更改为FORMANCED_PWD,只是为了确定一些事情。

下面是login.html表单,

<!DOCTYPE html>

<html>
    <head>
    <meta charset="utf-8">
    <title>Criminal Investigation Department-Home</title>

<link rel="stylesheet" href="css/login_page.css">
</head>

<body>
<img src="css/images/logo/CID_Logo_1.png" alt="CID Logo">
<nav id="navigation">
    <a id="link1" class="header_links" href="most_wanted.html">Most Wanted</a>
    <a id="link2" class="header_links" href="hotnews.html">Hot News</a>
    <a id="link3" class="header_links" href="report_crime.html">Report Crime</a>
    <a id="link4" class="header_links" href="login.html">Login</a>
    <a id="link5" class="header_links" href="about.html">About Us</a>
    <a id="link6" class="header_links" href="contact.html">Contact Us</a>
    <a id="link7" class="header_links" href="safety_measures.html">Safety Measures</a>
</nav>
<div id="login_page_border">
    <form action="LoginScript.do" method="POST">
        <div id="form_border">
        <span id="login_label">Login</span><br>
        <div id="login_contents">
        <span class="login_field">Username:</span> <input name="username" type="text"><br><br>
        <span class="login_field">Password:</span> <input name="pass" type="password">
        <input id="login_button" type="submit" value=" ">
        </div>
    </div>
    </form>
</div>
</body>

下面是控制器Servlet,

package com.cid_org.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cid_org.model.*;

import java.sql.*;

/**
 * Servlet implementation class LoginControllerServlet
 */

public class LoginControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public LoginControllerServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    /*Take the data submitted by the user though the login 
     * form(Remember the HTTP Post request ->HttpServletRequest request object*/
    String username = request.getParameter("username");
    String pwd = request.getParameter("pass");
    System.out.println(username + "aaa");
    System.out.println(pwd);
    Connection connection = (Connection)getServletContext().getAttribute("connection_context_param");

    LoginModelPOJO lmpojo = new LoginModelPOJO(username, pwd, connection);
    boolean isValidFlag = lmpojo.isValid();

    if(isValidFlag){
        RequestDispatcher view =request.getRequestDispatcher("view_profile.jsp");
        view.forward(request, response);
    }
    else{
        response.sendRedirect("/CrimeReportingSystem/static/login_access_denied.html");
    }

}

}

这是模型部件的略微修改版本,

package com.cid_org.model;

import java.sql.*;

public class LoginModelPOJO {

private String username;
private String pwd;
private Connection connection;
private boolean isValidFlag;

public LoginModelPOJO(String username, String pwd, Connection connection){
    this.username = username;
    this.pwd = pwd;
    this.connection = connection;
    isValidFlag=false;
    validate();
}

private void validate(){
    try {

        String query = "SELECT * FROM CRIME_SOLVING_OFFICIAL where OFFICIAL_USERNAME=? and OFFICIAL_PWD=?";
        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, username);
        ps.setString(2, pwd);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
             // String name=rs.getString(3);
              System.out.println("success");
              isValidFlag = true;
        } else {
            System.out.println("access denied");
        }
    } catch (Exception e) {
        System.out.println("Connection " + e);
      }
}

public boolean isValid(){
    return isValidFlag;
}
}

最后是ConextListener,

package com.cid_org.listener;

import java.sql.*;

import javax.servlet.*;

/*This listener will initialize a connection and set the context
 * attribute reference with a string at the time of application deployment time or
 * when the ServletContext will be initialized*/
public class DatabaseServletContextListener implements ServletContextListener {

Connection connection = null;
public void contextInitialized(ServletContextEvent event) {
    ServletContext sc = event.getServletContext();
    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "java");
        System.out.println("la la la ...");
        //Set the Attribute for the connection
        sc.setAttribute("connection_context_param", connection);
    }
    catch(Exception e){
        //To be decided Later- I dislike "checked" exceptions
        System.out.println("conn...bzzz "+e);
    }

}

public void contextDestroyed(ServletContextEvent event) {
    try {
        /*Connection will be closed at the time of undeployment of the application or
         * when the context is destroyed*/
        connection.close();
    } catch (Exception e) {
        System.out.println("connection pika fucked " + e);
    }
}

}

顺便说一句:错误仍然是一样的。

svmlkihl

svmlkihl1#

我建议您首先通过以下方式查看您的用户名和密码字符串:

System.out.println(username);
System.out.println(password);

只需确保您不会得到空值,然后直接在Oracle中运行查询,并查看上面的代码是否给出错误,即:

SELECT * FROM CRIME_SOLVING_OFFICIAL where OFFICIAL_USERNAME= *Your username and OFFICIAL_PASSWORD=*YourPassword

如果出现错误,请查看是否出现错误。您的SQL语法是问题所在。

相关问题