Posted on

Java Connect to a redis database

import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class RedisConnection {

    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        //Connect to redis server
        Socket socket = new Socket("localhost", 6379);
        //Create a scanner to read the input from the user
        Scanner scanner = new Scanner(System.in);
        //Create a PrintWriter to write to the redis server
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        //Create a BufferedReader to read from the redis server
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //Prompt the user for input
        System.out.println("Enter a command: ");
        //Read the user input
        String input = scanner.nextLine();
        //Write the user input to the redis server
        //Read the response from the redis server
        String response = in.readLine();
        //Print the response from the redis server
        System.out.println("Response: " + response);
        //Close the connection to the redis server

Enter fullscreen mode Exit fullscreen mode

Top comments (0)