Eclipse writes a java program to connect to the redis database tutorial

Redis is a very good key-value pair type database. The reading and writing speed is super fast. Let me introduce a java program written in eclipse under the download window to connect to the redis database, save a data, and read a data. (The default is that the redis database has been installed locally.)

Step 1:
Download, install and configure the redis database on Windows. I won't give an overview here. Download jedis-2.4.2.jar, of course, it is best to download the latest version of the jar package. This one came out under Baidu search. After downloading, put it under a folder, you will need it later.

Step 2:

Open eclipse and create a new java project.





Step 3:

In the Java project of Test, we create a new folder, name it lib, and put the jedis-2.4.2.jar package just downloaded under our newly created lib package, as shown in the following figure:





Step 4:
In eclipse, select the jar package under the build path. Then we create a new class in the Test project, and the class name is TestConnect.
// Connecting to Redis server on localhost
//Instantiate a client
Jedis jedis = new Jedis("localhost");
//=================================================
// check whether server is running or not
//ping and see if it works
System.out.println("Server is running: " + jedis.ping());
// save a
jedis.set("leiTest", "localhost Connection  sucessfully");
// get a
System.out.println("Get value by key: " + jedis.get("leiTest"));

In the class, we enter the following content: Step 5: Run the class just now, run the ctrl+f11 shortcut key, as shown in the figure below: Step 6: Run the class just now, ctrl+f11 shortcut key Under operation, as shown in the following figure:



















Step 7: To

further verify whether we have saved data on redis and can take it out, we go to the directory of the redis installation package, as shown in the figure below, and open redis-cli.exe in the red box , After opening, we enter the interface of the second picture below. Step 8: We enter the command get leiTest on the redis client interface. leiTest is a String type key that we stored in the redis database in eclipse just now. As shown in the figure below, it proves that we have indeed succeeded, and you can try it too.














Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327042256&siteId=291194637