Change Password

to change the password

Page 1 of 1

6 Replies - 14328 Views - Last Post: 15 April 2010 - 01:02 AM Rate Topic: -----

#1 Rykker  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 27
  • Joined: 03-October 09

Change Password

Posted 05 April 2010 - 09:54 PM

hello friends i have made the code to change the password but its not working....please help..thanks in advance..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.io.IOException;

public class ChangePassword extends JFrame implements ActionListener
{
 
  JFrame jf;
  JButton btnUpdate,btnCancel,btnBack;
  JLabel lbl1,lblPass,lblNewPass,lblConfirmPass,lbluser;
  JPasswordField txtPass,txtNewPass,txtConfirmPass;
  JTextField txtuser;
  Statement stmt;
  PreparedStatement ps;
  Connection conn;
  ResultSet rs; 
  JPanel pan2;
 
  public ChangePassword()
  {
   	Container cp = getContentPane();
   	    setSize(450,450);
		setLayout(null);
		setLayout(null);
		setTitle("LOGIN TO LIBRARY");
		cp.setLayout(null);
		JLabel jl = new JLabel("",new ImageIcon(""),JLabel.LEFT);
		jl.setSize(450,450);
		jl.setLocation(0,0);
		cp.add(jl);
		
		lbl1=new JLabel("Change Password");
		lbl1.setLocation(150,40);						
		lbl1.setSize(300,50);
		lbl1.setFont(new Font("Times New Roman", Font.BOLD, 20 ));
		jl.add(lbl1);
		
		lbluser  = new JLabel("Username");
		lbluser.setSize(100,50);
		lbluser.setLocation(40,100);
		lbluser.setFont(new Font("Times New Roman", Font.BOLD, 16 ));
		jl.add(lbluser);
	
		txtuser = new JTextField(20);
		txtuser.setSize(200,25);
		txtuser.setLocation(220,110);
		jl.add(txtuser);	

	   lblPass = new JLabel("Password");
	   lblPass.setSize(200,50);
 	   lblPass.setLocation(40,160);
 	   lblPass.setFont(new Font("Times New Roman", Font.BOLD, 16 ));
 	   jl.add(lblPass);

 	   txtPass = new JPasswordField(20);
  	   txtPass.setSize(200,25);
  	   txtPass.setLocation(220,170);
  	   jl.add(txtPass);

  	   lblNewPass = new JLabel("New Password");
   	   lblNewPass.setSize(150,50);
 	   lblNewPass.setLocation(40,220);
 	   lblNewPass.setFont(new Font("Times New Roman", Font.BOLD, 16 ));
	   jl.add(lblNewPass);
     
 	   txtNewPass = new JPasswordField(20);
	   txtNewPass.setSize(200,25);
 	   txtNewPass.setLocation(220,230);
	   jl.add(txtNewPass);
  
 	  lblConfirmPass = new JLabel("Re-Type Password");
 	  lblConfirmPass.setSize(250,50);
 	  lblConfirmPass.setLocation(40,280);
 	  lblConfirmPass.setFont(new Font("Times New Roman", Font.BOLD, 16 ));
 	  jl.add(lblConfirmPass);
     
 	  txtConfirmPass = new JPasswordField(20);
 	  txtConfirmPass.setSize(200,25);
 	  txtConfirmPass.setLocation(220,290);
 	  jl.add(txtConfirmPass);
  
 	  btnUpdate = new JButton("Upadate");
 	  btnUpdate.setSize(85,25);
 	  btnUpdate.setLocation(80,350);
 	 
 	 btnUpdate.addActionListener(this);
 	  jl.add(btnUpdate);
   
 	  btnBack = new JButton("Back");
 	  btnBack.setSize(80,25);
  	  btnBack.setLocation(173,350);
  	
  	  btnBack.addActionListener(this);
  	  jl.add(btnBack);
   
   
 	  btnCancel = new JButton("Clear");
      btnCancel.setSize(80,25);
 	  btnCancel.setLocation(260,350);
	  
  	  btnCancel.addActionListener(this);
 	  jl.add(btnCancel);
   
  try
      {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn=DriverManager.getConnection("Jdbc:Odbc:Library","","");
       stmt=conn.createStatement();
       }
     catch(Exception e)
     {}

    setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
   
 public void actionPerformed(ActionEvent ae)
 {
 if(ae.getSource()== btnUpdate)
 {
 	try
 	{
 		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn=DriverManager.getConnection("Jdbc:Odbc:Library","","");
 		String p1 = "";
 		String pk="";
 	//	String p = txtuser.getText();
 		String p2 = txtPass.getText();
 		String p3 = txtNewPass.getText();
 		String p4 = txtConfirmPass.getText();
 		rs=stmt.executeQuery("select * from Password where username ="+txtuser.getText());
 		
 		while(rs.next())
 		{
 			
 			 p1 = rs.getString(2);
 			 if(p2.equals(p1))
 			 {
 			 	if(p3.equals(p4))
 			 	{
 			 		ps=conn.prepareStatement("Update Password SET password = ? where username ="+txtuser.getText());
                    ps.setString(2,p4);
                    ps.setString(1,p);
                    ps.executeUpdate();
                    JOptionPane.showMessageDialog(null,"Password Changed");
                    	
                    
 			 	}
 			 	else
 					{
 			JOptionPane.showMessageDialog(null,"Please check your Password n type Again");
 			txtPass.setText("");
 			txtNewPass.setText("");
 			txtuser.setText("");
 			txtConfirmPass.setText("");
 					}
 			 }
 			            
 	 
 					
 			
 		}
 		
 		       
 			}
 			catch(Exception e)
 			{
 				
 			}
 		
 	}
 if(ae.getSource()== btnCancel)
    {
            txtPass.setText("");
            txtuser.setText("");
 			txtNewPass.setText("");
 			txtConfirmPass.setText("");	
    	}
    	if(ae.getSource() == btnBack)
    	{
    		this.dispose();
    	
    	}
 }
 public static void main(String args[])
 {
 				 JFrame frm = new ChangePassword();
     			 frm.setSize(450,450);
       			 frm.setLocation(100,90);
       			 BufferedImage image = null;
				 try {
            			image = ImageIO.read(frm.getClass().getResource("Usericon.png"));
        			} catch (IOException e) {
         				   e.printStackTrace();
       				 }
                 frm.setIconImage(image);
       			 frm.setVisible(true);
       			 frm.show();
 }
}
   
   
  


This post has been edited by Rykker: 05 April 2010 - 10:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change Password

#2 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Re: Change Password

Posted 06 April 2010 - 04:55 AM

View PostRykker, on 05 April 2010 - 08:54 PM, said:

its not working....please help...


Um, what's not working? We aren't mind readers. Tell us what it is supposed to do and what it is doing. Only with that knowledge can we look at the code to analyze what is wrong with it.
Was This Post Helpful? 1
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Change Password

Posted 06 April 2010 - 05:10 AM

kingfeanor is right, without explaining the specific error, neither him or any of us will be able to assist you. Saying it dosent work isnt specific enough, is it a runtime or compiled error? What type of error is it?
Was This Post Helpful? 0
  • +
  • -

#4 Rykker  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 27
  • Joined: 03-October 09

Re: Change Password

Posted 06 April 2010 - 09:06 AM

View Postm-e-g-a-z, on 06 April 2010 - 04:10 AM, said:

kingfeanor is right, without explaining the specific error, neither him or any of us will be able to assist you. Saying it dosent work isnt specific enough, is it a runtime or compiled error? What type of error is it?


my code is working but its is not updating the password.i am trying to change the password as the user wants. It is taking the values but not updating..
Was This Post Helpful? 0
  • +
  • -

#5 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Re: Change Password

Posted 06 April 2010 - 05:16 PM

Rykker,
Can you debug the code? I copied the code into a file and can't compile it. Is says:
ChangePassword.java:150: cannot find symbol
symbol  : variable p
location: class ChangePassword
                    ps.setString(1,p);
                                   ^



I think I can gather from your responses that the UI opens and prompts for the password but that when you enter the password it doesn't get updated in the database. Is this correct?

Can you tell us each thing you enter, each button you push, and what you expect to have happen? What is the database schema? Can you tell us your ODBC settings? What Db are you connecting to via ODBC? How are you checking that the password was set?
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Change Password

Posted 06 April 2010 - 05:38 PM

And can i advise you neaten up your code. Mixing JDBC with swing etc is no good. I will try and give you some advise. Create a factory class to help you with your JDBC methods, like so
import java.sql.Connection;  //the importing of required libraries
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**This class contains all methods which relate to database connection, closing and exception handling*/
public class DatabaseUtils
{   
    /**method which returns a connection*/
	public static Connection connect(String driver, String url) throws ClassNotFoundException, SQLException //exception handling
	{
		Class.forName(driver);  //passing the driver to Class.forName
		return DriverManager.getConnection(url);  //passing the reamining variables and being returned a connection
	}
	
	/**Method which closes a connectio*/
	public static void close(Connection connection)  
	{
		try  //try block (try doing this)
		{
			if (connection != null) //if connection is not equal to null (if there is a connection)
			{
				connection.close();  //close it
			}
		
		}
		catch (SQLException e)  //catch block to handle an exception
		{
			e.printStackTrace(); //print exception to the command line
		}
		
	}
	
	/**Method which closes a ResultSet*/
	public static void close(ResultSet resultSet)  
	{
		try  //try
		{
			if (resultSet != null)  //if resultset not equal to null
			{
				resultSet.close(); //close it
			}
		}
		catch (SQLException e)  //catch
		{
			e.printStackTrace();
		}
	}
	
	/**Method which closes a Statement*/
	public static void close(Statement statement)  
	{
		try
		{
			if (statement != null)
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	/**Method which rolls back the connection*/
	public static void rollback(Connection connection) 
	{
		try
		{
			if (connection != null)
			{
				connection.rollback();
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}	
}




With this, you will make your life so much easier. Really, you gui should pass to another class before you use the above, but if you want to use it in your gui, just declare some variables
private Connection con = null;  //creating a Connection object which is empty	
public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";  //hardcoding the driver used
public static final String URL = "jdbc:odbc:register";  //table name


Then in you method, do the sql. Here is an example which takes a Person Object, and registers them as a user
/**Method which performs a query to register the user by INSERT INTO*/
	public void regPerson(PersonInfo person)  
	{
		PreparedStatement ps = null;  //initialising a blank PreparedStatement
		
		try  //try block
		{
			con = DatabaseUtils.connect(DRIVER, URL);  //assigning our empty connection "con" with the returned connection
																		//provided after sending the 4 variables to the connect method in the DatabaseUtils class			
			String sql = "INSERT INTO register(FullName, UserName, PassWord) VALUES (?, ?, ?)";//creating a preparedStatment which
			//inserts into our register table, into the above columns, the values .....
			ps = con.prepareStatement(sql);  //assigning our prepared statement with a connection and the sql command
			ps.setString(1, person.getRegName());  //setting the first value of our sql command by getting the register name from the PersonInfo object
			ps.setString(2, person.getRegUser()); //setting the second value
			ps.setString(3, person.getRegPassword());  //setting the third value
			
			ps.executeUpdate();  //executing the query
				
		}
		catch(Exception e) //catch block
		{
			System.out.println(e);  //print the exception to the command line
			DatabaseUtils.rollback(con);  //rollback the connection (to make it healthy again)
			e.printStackTrace();	//print the stacktrace to the command line
		}
		finally  //finally block
		{
			DatabaseUtils.close(ps);  //close the prepared statements using the close method from the DatabaseUtils class
			DatabaseUtils.close(con);  //close the connection
		}
	}



Coding like this will make things so much easier to handle.

But also as has been said, it is important to mention things like what database provider you use, because different providers are set up in different ways.

This post has been edited by nick2price: 06 April 2010 - 05:40 PM

Was This Post Helpful? 1
  • +
  • -

#7 Rykker  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 27
  • Joined: 03-October 09

Re: Change Password

Posted 15 April 2010 - 01:02 AM

View Postkingfeanor, on 06 April 2010 - 04:16 PM, said:

Rykker,
Can you debug the code? I copied the code into a file and can't compile it. Is says:
ChangePassword.java:150: cannot find symbol
symbol  : variable p
location: class ChangePassword
                    ps.setString(1,p);
                                   ^



I think I can gather from your responses that the UI opens and prompts for the password but that when you enter the password it doesn't get updated in the database. Is this correct?

Can you tell us each thing you enter, each button you push, and what you expect to have happen? What is the database schema? Can you tell us your ODBC settings? What Db are you connecting to via ODBC? How are you checking that the password was set?


Quote

i enter the user name as Admin and password is also admin OK.now when i push the update buton the value in the confirm password text field should get updated in the database.and tht should get replaced with the password in the database(i mean the admin).My table name is Password it contains two fields the first is the username and the other is the password.My ODBC name is Library n database name is also Library.

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1