1

This is model part of my MVC app and it is supposed to communicate with the Database(Oracle 10g XE).Here is the code that I have written,

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);
      }
}
}

but as it gets executed, it generates the following error:

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

Here is the table that I have created in Oracle,

Here is the image enter image description here

Btw, I know this question will be branded as duplicate question and indeed it is but FYI I have gone through all the available posts related to this question(You can see all the tabs opened in firefox in the picture) but couldn't find any answer.

Edit: I have made all the 'password' strings disappear to corner out the 'PASSWORD' identifier Exception is talking about.And I have also altered the column name of the OFFICIAL_PASSWORD to OFFICIAL_PWD just to be sure about things.

Here is the login.html form,

<!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>

Here is the controller 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");
    }

}

}

And here is a slightly modified version of the Model part,

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;
}
}

Finally ,here is the ContextListener,

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);
    }
}

}

Btw: The error is still the same.

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • 1
    Password is a keyword - ask Google how to escape resrved keywords – Germann Arlington Jun 24 '14 at 15:28
  • @pulp_fiction That's strange. Is the value for password identical to the string "PASSWORD" ? – Frank Schmitt Jun 24 '14 at 15:33
  • 2
    Are you sure that's the code that's raising the error? There is no `PASSWORD` identifier in the query you've shown. – Mat Jun 24 '14 at 15:37
  • @pulp_fiction :-Your name has forced me to comment,it's probably because of not entering all values of the columns--your table shows you have kept 4 non-nullable columns!(BTW which part of pulp_fiction---butch,vincent,jules,wallace...) – Am_I_Helpful Jun 24 '14 at 15:39
  • @pulp_fiction Another thing you can do is accumulate points, then raise a bounty on one of the earlier questions. – La-comadreja Jun 24 '14 at 15:51
  • If you would use some column named PASSWORD I would suggest escaping it with double quotes like "PASSWORD" - see http://stackoverflow.com/a/1162391/510711 . Or some trigger or some downstream call might cause this... ? – flob Jun 24 '14 at 15:51
  • Try the query in SQL first. Maybe you illegally defined an index with "PASSWORD". In every case close `ps` and `rs`. Maybe using try-with-resources. **First and for all, do `e.printStackTrace(System.out)` to determine the right spot.** It might be that the error stems from another spot (opening the connection with PASSWORD). (@Mat) – Joop Eggen Jun 24 '14 at 16:08
  • @Mat: Yeah I am sure and I myself can't stop wondering "which" PASSWORD identifier is it talking about? – Manish Kumar Sharma Jun 24 '14 at 18:07
  • @shekharsuman: But this is a "select" query and as far as I know there shouldn't be a problem with selecting row with even one column, nullable means I can't leave those columns null when I insert new row in the database itself but that doesn't seem to be the case here. And I like butch... – Manish Kumar Sharma Jun 24 '14 at 18:14
  • @pulp_fiction-Oh sorry,I didn't take a closer look!It's fine then.SORRY!Also,I prefer jules more! – Am_I_Helpful Jun 24 '14 at 18:16
  • @pulp_fiction-Better if you remove the form part and the controller servlet as they are of no use to solve this error.I'm working on your code and will definitely try to solve your case! – Am_I_Helpful Jun 24 '14 at 20:43

1 Answers1

0

I'd recommend seeing what your username and password strings are first via:

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

Just to make sure that you're not getting nulls then run the query directly in Oracle and see if it gives an error with the code you have above i.e:

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

See if this gives an error if it does your SQL Syntax is the issue.

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
Paulf0502
  • 1
  • 1
  • Unfortunately, I can't give you +1 because of my reputation but thanks for the advice, but it didn't solve my problem.I tied putting the println statements and it doesn't print anything on the console and that's a weird behaviour but anyway I am gonna post the complete thing to understand what's going on. – Manish Kumar Sharma Jun 24 '14 at 19:10
  • Try using brackets it might help stop if password is a reserved word e.g. Replace: SELECT * FROM CRIME_SOLVING_OFFICIAL where OFFICIAL_USERNAME=? and OFFICIAL_PWD=? WITH: SELECT * FROM [CRIME_SOLVING_OFFICIAL] where [OFFICIAL_USERNAME]=? and [OFFICIAL_PWD]=? I am developing an application in c# I find the following code very helpful to see values help yourself: Response.Write(@""); In your example it would be: Response.Write(@""); – Paulf0502 Jun 25 '14 at 11:44