Redis - Java Database Connection
Code for Establishing Database connection to Redis from Java - Phone Number Search Application in Java using Redis
import java.util.Scanner;
import redis.clients.jedis.Jedis;
public class redisdemo {
public static void main(String[] args) {
//this is a demo on establishing a connection with the redis data store
//we need to download and add the jedis jar file to our project
try
{
Jedis phonenumberDB = new Jedis("localhost");
System.out.println("Connection established successfully");
//we will get the username and phone number from the user
String username,phonenumber;
Scanner input = new Scanner(System.in);
for(int i=0;i<3;i++)
{
System.out.println("Enter the username");
username = input.nextLine();
System.out.println("Enter the phone number");
phonenumber = input.nextLine();
phonenumberDB.set(username, phonenumber);
}
System.out.println("Enter the username for searching");
String search = input.nextLine();
if(phonenumberDB.get(search)==null)
{
System.out.println("No such user in the database");
}else
{
System.out.println(phonenumberDB.get(search));
}
}
catch(Exception e)
{
e.getStackTrace();
}
}
}