Verifying username and Password Using JDBC

Verifying username and Password Using JDBC

We have already created table Password_info
password_table
JDBC program
package jdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
 
import com.mysql.jdbc.Driver;
 
public class Login 
{
public static void main(String[] args)
{
 
	Connection con=null;
	PreparedStatement p =null;
	ResultSet rs=null;
 
 
	String username="bhavesh";
	String password="12345";
 
	/*
	 * load the driver
	 */
 
	try 
	{
 
 
		Driver driverref=new Driver();
		DriverManager.registerDriver(driverref);
 
 
 
		String dburl="jdbc:mysql://bhavesh-pc:3306/test?user=j2ee&password=j2ee";
		con=DriverManager.getConnection(dburl);
 
		/*
		 * issue sql query via driver
		 */
 
		String q = " select *from password_info where user=? and current_password=? ";
 
		 p= con.prepareStatement(q);
 
		 p.setString(1,username);
		 p.setString(2,password);
 
	    rs=p.executeQuery();
 
		if(rs.next())
		{
			System.out.println("Login successful");
		}
		else
		{
 
			System.out.println("wrong credential, Try again");
		}
		}
	catch (Exception e)
    {
 
		e.printStackTrace();
	}
		/*
		 * close all jdbc object
		 */
 
		finally
		{
 
			try {
				if(con!=null)
				{
					con.close();
				}
				if(p!=null)
				{
					p.close();
				}
				if(rs!=null)
				{
					rs.close();
				}
			} 
			catch (SQLException e) 
			{
 
				e.printStackTrace();
			}
 
		}
 
   }
 
}
 
output:
Login successful

Leave a Reply