-1
b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                user=tf1.getText();
                pass=new String(tf2.getPassword());
                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jashan","noor1032"); 
                    PreparedStatement stmt=con.prepareStatement("select * from jashan.student where 'name1'=? and 'password'=?");
                        stmt.setString(1, user);
                        stmt.setString(2, pass);
                    ResultSet Rs=stmt.executeQuery();
                    if(Rs.next())
                        {
                        JOptionPane.showMessageDialog(f, "success!!");
                        }
                    else
                        {
                            JOptionPane.showMessageDialog(null, "incorrect username/password","warning",JOptionPane.WARNING_MESSAGE);
                        }
                   }    
                    catch(Exception f)
                        {
                            System.out.println(f);
                        }
            }

actually, i want that the code matches the username and password, but it is not doing so...whenever i execute, it shows invalid password/username....i don't know why....i have a table in oracle 11g which has columns student_id, name1,gender,address, email_id, phone_number,and password in the same order as defined. can anyone tell me what is the problem??

Picture of my table

Dave
  • 5,108
  • 16
  • 30
  • 40
  • I'm not sure you need to put the column names in your prepared statement in single quotes. – KevinO Mar 06 '19 at 16:50
  • See: [What is the difference between single and double quotes in SQL?](https://stackoverflow.com/q/1992314/5221149) – Andreas Mar 06 '19 at 17:11
  • 1
    *"whenever i execute, it shows invalid password/username"* That's because you enter the wrong name/password. Enter `name1` for name and `password` for password, and you'll get "success!!". That is of course not what you intended, but it *is* what you wrote the code to do, by comparing `user` to **text literal** `'name1'` and comparing `pass` to text literal `'password'`. – Andreas Mar 06 '19 at 17:22
  • @Andreas i am entering the right username and password, but still it shows the same error – Jashandeep Singh Mar 07 '19 at 07:01
  • Debug your Query in an SQL Tool. E.g. PL/SQL-Developer. Then copy it into the java source. – keuleJ Mar 07 '19 at 12:27

2 Answers2

1

You have given single qoute around your column name that is causing the issue. Remove that and it should work

Change 'name1' to name1 and 'password' to password in your query

Thanthu
  • 4,399
  • 34
  • 43
1

You have managed to create a table with lowercase column names. As you have experienced, it causes trouble as Oracle converts all names (for tables, columns etc.) to upper-case by default.

You have two options:

  1. Drop the table and recreate it with uppercase names.

  2. Use double quotes around the columns names to prevent Oracle from converting them to uppercase:

select * from jashan.student where "name1"=? and "password"=?

Or when you put it in Java:

PreparedStatement stmt = con.prepareStatement("select * from jashan.student where \"name1\"=? and \"password\"=?");

In any case, single quotes around column names is incorrect.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • actually this solution looks fine, but adding double quotes around the name1 and password fields, it creates a problem in query, because double quotes are used to define strings in java.....when i put double quotes, both the fields technically come out of the query. – Jashandeep Singh Mar 07 '19 at 16:01
  • 1
    thank you very much, i renamed all the attributes into uppercase and it worked – Jashandeep Singh Mar 07 '19 at 16:08
  • Yes, in Java code you need to esacpe the double quotes. See my update. – Codo Mar 07 '19 at 16:09
  • can you also tell me how to retrieve all the values from the oracle database and display it in jtable....I did it in MySQL and it worked and now i applied the same code fro oracle but it didn't....to see my code please refer to my one more question which i asked 4 days ago – Jashandeep Singh Mar 07 '19 at 16:12