Skip to main content

Verifying username and Password Using JDBC

Verifying username and Password Using JDBC

We have already created table Password_info
password_table
JDBC program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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