Search code examples
javaeclipsejdbcojdbc

Cannot connect wit the database table


I wrote a login page program to read username and password which will check if the username and password matches with the ones available in the Table.

If It matches It shows the message Login successful with the help of .showMessageDialog() method, else it should display Wrong input. I also created a separate Connection Class for database connection. Then I created a table with Columns for Username and Password and inserted rows accordingly. So Logically when I enter correct values it should say Login Successful but instead it always says Wrong Input.

Note: Im Using Java version 1.8 on Eclipse Sdk and Oracle 10g for database and used the jar file ojdbc14 also tried ojdbc14_g separately.Added the ojdbc in a folder inside my project folder and created a reference library by linking it with the ojdbc14 in the folder inside my project folder.

here is the code for Login Page window


import demo.ConnectionClass;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;


import javax.swing.*;

import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;

public class LoginPage extends JFrame implements ActionListener
{
    
    
    private static final long serialVersionUID = 1L;
    
    
    JTextField t1;
    JPasswordField t2;
    JLabel l1,l2;
    JButton b;
    
    Connection conn= null;
    OraclePreparedStatement pst= null;
    OracleResultSet rs= null;
    
    LoginPage()
    {   
        
        l1=new JLabel("UserName :");
        l1.setBounds(50,50,150,20);
        add(l1);

        t1=new JTextField();
        t1.setBounds(180,50,100,20);
        add(t1);
        

        l2=new JLabel("Password :");
        l2.setBounds(50,80,150,20);
        add(l2);        

        t2=new JPasswordField();
        t2.setBounds(180,80,100,20);
        add(t2);

        b=new JButton("Login");
        b.setBounds(100,120,100,20);
        b.addActionListener(this);
        add(b);

        setBounds(400,200,300,250);
        setLayout(null);
        setVisible(true);
        
    }
    
    public void actionPerformed(ActionEvent ae) 
    {
            conn= ConnectionClass.dbconnect();
            try
            {
            
                String str = "select * from hmsProject where Username = ? and Password = ?";
                pst=(OraclePreparedStatement)conn.prepareStatement(str);
                pst.setString(1, t1.getText());
                pst.setString(2, t2.getPassword().toString());
                rs=(OracleResultSet) pst.executeQuery();
                
                if(rs.next())
                {
                    JOptionPane.showMessageDialog(null, "Login Succesfull");
                    this.setVisible(false);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Wrong input");
                    this.setVisible(false);
                    
                }
                
                rs.close();
                pst.close();
                
            }
            
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        
        
    }
    
    public static void main(String args[])
    {
        new LoginPage();
    }

}

And Here is the Connection class code

package demo;
import java.sql.*;

import javax.swing.JOptionPane;

public class ConnectionClass 
{
    
    

    public static Connection dbconnect()
    {
        try
        {       
            
            Class.forName("oracle.jdbc.OracleDriver");
            Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","hr");
            
            return conn;
            
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }

    
    public static void main(String[] args) 
    {
        

    }

}

Solution

  • I reckon the problem is here:

                    pst.setString(2, t2.getPassword().toString());
    

    t2 is a JPasswordField, and t2.getPassword() returns a char[]. However, calling .toString() on a char array will not convert the array to the string you would expect it to. Instead, it will give you something like [C@48f954eb, as Java arrays don't override .toString() and this is what the the .toString() method inherited from Object will return.

    To convert a char[] array to a string, write new String(yourCharArray) instead.

    Try replacing the line above with the following:

                    pst.setString(2, new String(t2.getPassword()));
    

    Finally (and I know people will pick me up on this if I don't mention it), but I'm guessing this is just for training/learning purposes. This isn't for a real system that will go into production use by other people, is it? Handling passwords in plain-text is OK if you're just building a login system for your own learning purposes, but in real systems, passwords will (or at least should!) be hashed and salted and the resulting hash will be what is stored in the database and compared, not the user's actual password.