Login in console using jdbc

Introduction 

 
In this blog, we will know to validate user name and password by using a table named as login having columns as uname and pass using jdbc. If valid credentials are provided then it will show the welcome: username otherwise Invalid user name and password.
 
Here we use Type-1 driver (JDBC-ODBC bridge)
 

Creation of dsn(database source name) for Oracle

 
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up a data source (for Oracle- Oracle in XE)-select it and click finish-give any name in the data source name textbox-then click ok button.
 
Note: - Here Username=system, Password=pintu and Dsn name= dsnlogin
 

Table Creation with values

  1. create table login(uname varchar(50), pass varchar(50))   
  2. insert into login values('raj''raj123')  
  3. insert into login values('ravi''ravi123')  
  4. insert into login values('rahul''rahul123'    
  5. import java.sql.*;   
  6. import java.util.*;  
  7. public class login {  
  8.  public static void main(String args[]) throws Exception  
  9.  {  
  10.   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  11.   Connection con = DriverManager.getConnection("jdbc:odbc:dsnlogin""system""pintu");  
  12.   Statement stmt = con.createStatement();  
  13.   Scanner sc = new Scanner(System.in);  
  14.   System.out.print("Enter the user id : ");  
  15.   String str1 = sc.next();  
  16.   System.out.print("Enter the password : ");  
  17.   String str2 = sc.next();  
  18.   ResultSet rs = stmt.executeQuery("select * from login where uname='" + str1 + "' and pass='" + str2 + "'");  
  19.   if (rs.next())  
  20.    System.out.println("Welcome::: " + str1);  
  21.   else  
  22.    System.out.println("Invalid user name and password");  
  23.   con.close();  
  24.  }   
  25. }  
Compile
 
Javac login.java
 
Java login 
Next Recommended Reading Display records from database using JDBC