Blog

Login in console using jdbc

Posted by Satyapriya Nayak Blogs | Java Sep 24, 2011
In this blog, we will learn how to validate a user name and password by using a table named login having columns as uname and pass using JDBC.
Download Files: login.rar

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 data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

 

Note: - Here Username=system, Password=pintu and Dsn name= dsnlogin

 

Table Creation with values

create table login(uname varchar(50),pass varchar(50))

 

insert into login values('raj','raj123')

insert into login values('ravi','ravi123')

insert into login values('rahul','rahul123')

 

import java.sql.*;

import java.util.*;

public class login{

public static void main(String args[]) throws Exception

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:dsnlogin","system","pintu");

Statement stmt=con.createStatement();

Scanner sc=new Scanner(System.in);

System.out.print("Enter the user id : ");

String str1=sc.next();

System.out.print("Enter the password : ");

String str2=sc.next();

ResultSet rs=stmt.executeQuery("select * from login where uname='"+str1+"' and pass='"+str2+"'");

if(rs.next())

System.out.println("Welcome::: "+str1);

else

System.out.println("Invalid user name and password");

con.close();

}

}

 

Compile

Javac login.java

Java login

 

COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.