Key-value database redis: II. Preliminary use

Source: Internet
Author: User

After installing redis, enter the command./redis-server in the terminal to start the database. After the database is started successfully, it regularly records the connection read/write status.

After starting the database, open another terminal and directly enter the command,

$./Redis-cli set mykey myvalue1

This command adds a record to the database. Its key value is mykey and its value is myvalue1.

After adding the record, obtain the added record. command:

$./Redis-cli get mykey

The above command only adds a value to the key, that is, a record. In real business, this situation is not very common. It is often necessary to add multiple value values to a key in the database, you can run the following command:

$./Redis-cli lpush list1 value1

$./Redis-cli lpush list1 value2

$./Redis-cli lpush list1 value3

$./Redis-cli lpush list1 value4

After the addition is successful, "OK" is displayed"

After the addition is complete, obtain the data by running the following command:

 

./Redis-cli lrange list1 0 - 1

All values in the list are displayed,

 

The above is a basic command operation, the followingCodeFor Java operations,

 

To use Java for database operations, you must first obtain the JDBC driver. The JDBC driver of redis can be obtained from the following URL:

Http://code.google.com/p/jdbc-redis/

Its usage is similar to that of other relational databases, such as MySQL, Oracle, and MSSQL,

The following is the Java code:

Code Package Com. redis. Demo;

ImportJava. SQL. statement;
ImportJava. SQL. connection;
ImportJava. SQL. drivermanager;
ImportJava. SQL. resultset;

ImportJava. SQL. sqlexception;

Public ClassRedisdemo {

Private   Static Connection Conn =   Null ;
Private   Static Statement STM =   Null ;
Private   Static Resultset rs =   Null ;

/**
* @ Param ARGs
* @ Throws Sqlexception
*/
Public   Static   Void Main (string [] ARGs) Throws Sqlexception {
Init ();
Onekey ();
Manykey ();
}

/*
* Initialization
*/
Private   Static   Void Init (){

Try {
// Load the redis JDBC driver
Class. forname ( " Br.com. svvs. JDBC. redis. redisdriver " );
// Connection
Conn = Drivermanager. getconnection ( " JDBC: redis: // 192.168.1.117 " );
STM = Conn. createstatement ();

}Catch(Classnotfoundexception e ){

System. Out. println (E. tostring ());
}Catch(Sqlexception e ){
System. Out. println (E. tostring ());
}
}

/*
* Close STM, Conn
*/
Private   Static   Void Close (){
Try {
// Rs. Close ();
STM. Close ();
Conn. Close ();
} Catch (Sqlexception e ){

System. Out. println (E. tostring ());
}
}

/*
* One key value operation
*
*/
private static void onekey () throws sqlexception {

String SQL= "Set my_first_key myfirstvalue";
Cmd.exe cute (SQL );
Cmd.exe cute ("Get my_first_key");

Resultset rs=STM. getresultset ();

While(Rs. Next ()){
System. Out. println (Rs. getstring ("My_first_key"));
}

Close ();

}

/*
* One key operates on multiple values
*/
Private   Static   Void Manykey () Throws Sqlexception {
Cmd.exe cute ( " Lpush mylist value1 " );
Cmd.exe cute ( " Lpush mylist value2 " );
Cmd.exe cute ( " Lpush mylist value3 " );

Cmd.exe cute ( " Lrange mylist 0-1 " );

Resultset rs = STM. getresultset ();

While (Rs. Next ()){
System. Out. println (Rs. getstring ( " Mylist " ));
}
}

}

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.