Java data redis in connection check, add, change, delete operations in a method

com.lml.redis Package;


Import the java.util.HashMap;
Import the java.util.Iterator;
Import a java.util.Map;
Import java.util.Map.Entry;
Import java.util.Set;


Import redis.clients.jedis .Jedis;


public class RedisDBHelper {
Jedis JS = null;
Private Host String = "127.0.0.1";
Private int Port = 6379;


/ **
* link redis database, initialize
*
* @param hosturl: ip / name of the linked server
* @param hostPort: host port number
* @return: returns whether a successful link initialization
* /
public Boolean the init (String host, int port) {
iF (JS == null) {
JS = new new Jedis (host, port);
}
IF (JS = null!) {
System.out.println ( "initialized successfully");
return to true;
The else {}
return false;
}
}


/ **
* Link redis database, initialize
*
* @param Host: ip / name of the linked server
* @return: whether to return a successful link initialization
* /
public boolean the init (String Host) {
IF (JS == null) {
JS = new new Jedis (Host);
}
IF (JS = null!) {
System.out.println ( "initialized successfully");
return to true;
} the else {
return to false;
}
}


/ **
* new data
*
* @param key: the new key data
* @param value: the value of new data
* @return: returns a boolean value that indicates whether the data is successfully add
* /
public boolean the SET (key String, String value ) {


IF (js.exists (Key)) {
System.out.println (Key + "exists");
System.out.println ( "To modify data using the update () method");
return to false;
} the else {
js.set (Key, value);
IF (js.exists (Key)) {
IF (value.equals (js.get (Key))) {
System.out.println ( "to increase the data successfully");
return to true;
} the else {
System.out.println ( "data failed to increase");
return to false;
}
} {the else
the System .out.println ( "data failed to increase");
return to false;
}
}
}


/ **
* Add a plurality of data
*
* @param Map: placing a plurality of data value pairs set
* @return: returns a boolean value indicating whether all the new data has been successfully
* /
public Boolean sets (the Map <String, String> Map) {
Boolean = sets to true;
for (the Entry <String, String> E: EnumMap.entrySet ()) {
String Key = E.getKey();
E.getValue value = String ();
; = Boolean SET SET (Key, value)
(! SET) {IF
sets = to false;
}
}
return sets;
}


/ **
* delete data
*
* @param Key: To delete data the Key
* @return: returns a boolean value that indicates whether to delete success
* /
public boolean the delete (String Key) {


iF (js.exists (Key)) {
iF (js.del (Key) == 1) {
System.out .println ( "delete data successfully");
return to true;
} the else {
System.out.println ( "delete data failed");
return to false;
}
} the else {
System.out.println (Key + "does not exist");
to false return;
}
}


/ **
* delete a set of data
*
* @param keys: key data set to be deleted
* @Return: returns a boolean value that indicates whether to delete all the success
* /
public boolean Deletes (String [] Keys) {
boolean Deletes = to true;
for (String Key: Keys) {
boolean = the Delete the Delete (Key);
IF (the Delete! ) {
Deletes = to false;
}
}
return Deletes;
}


/ **
* modify data
*
* @param Key: to modify the Key
* @param value: the value of the data to be modified
* @return: returns a boolean value indicating whether the modified success
* /
public Boolean Update (String Key, String value) {
IF (js.exists (Key)) {
js.set (Key, value);
IF (value.equals (js.get (Key))) {
the System. out.println ( "modifying data successfully");
return to true;
} the else {
System.out.println ( "modify data failed");
return to false;
}
} The else {
System.out.println (Key + "does not exist");
System.out.println ( "To use the new data set () method");
return to false;
}
}


/ **
* Get table data
*
* @param key: key to query
* @return: return the found data; if the key does not exist, returns null;
* /
public String GET (String key) {
IF (js.exists (key)) {
System.out.println (js.get (Key));
return js.get (Key);
} the else {
System.out.println (Key + "does not exist");
return null;
}
}
/ **
* Get multi Article of key value
* @param keys: a collection of a plurality of key
* @return
* /
public the Map <String, String> the gets (String [] Keys) {
the Map <String, String> = new new the HashMap Map <String, String> ( );
for (String key : keys) {
if (js.exists(key)) {
String value = js.get(key);
map.put(key, value);
System.out.println(key + "-" + value);
} else {
System.out.println(key + "不存在");
}
}
return map;
}


/**
* 获得所有数据的键
*/
public void getKeys() {
Set<String> set = js.keys("*");
if (set.size() != 0) {
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key);
}
} else {
System.out.println("数据库暂时没有数据");public void the getAll () {* /* Search data/ **}
}







Set<String> set = js.keys("*");
if (set.size() != 0) {
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
String value = js.get(key);
System.out.println(key + "-" + value);
}
} else {
System.out.println("数据库暂时没有数据");
}
}


/**
* 关闭链接
*/
public void unInit() {
if (js != null) {
js.close();
js = null;
}
}
}

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12080584.html