java代碼怎么正則刪除redis的數(shù)據(jù),即批量刪除符合一定條件的redis數(shù)據(jù),現(xiàn)在介紹批量刪除已某些字符開頭的redis數(shù)據(jù):
創(chuàng)新互聯(lián)建站是專業(yè)的東川網(wǎng)站建設(shè)公司,東川接單;提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行東川網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
在Java中連接Redis,并進行操作,首先得加載以JAR包形式存在的Java中的Redis Client,我們這里選擇Jedis。以下是使用Jedis的具體步驟:
在Maven項目中,在pom.xml中增加如下語句(即加載Jedis jar包):
dependency
? ? ?groupIdredis.clients/groupId
artifactIdjedis/artifactId
? ? version2.7.2/version
typejar/type
/dependency
如不是Maven工程,就自行下載Jedis jar包引用即可。
在加載Jedis JAR包之后,可以直接使用新建一個Jedis實例的方法,來建立一個到Redis的連接,并進行操作。不過跟Mysql一樣,每次操作的時候,都建立連接,很耗費性能。解決方法就是從一個連接池中取出連接對象,用完還回去。使用連接池的方案還能解決很多同步性問題。
在Jedis中,管理Redis連接的類是JedisPool
package com.atzy
import?redis.clients.jedis.Jedis; ?
import?redis.clients.jedis.JedisPool;
public?class?RedisHelper?{
public?static?void?main(String[]?args)?{
JedisPool?jedisPool?=?new?JedisPool("localhost",?6379);
?Jedis?jedis?=?null;
try?{
jedis?=?jedisPool.getResource();
? ? ?String?pre_str="ab";?
? ? ??SetString?set?=?jedis.keys(pre_str?+"*");
? ? ? IteratorString?it?=?set.iterator();
? ? ???while(it.hasNext()){
? ? ? ? ??String?keyStr?=?it.next();
? ? ? ? ? System.out.println(keyStr);
? ? ? ? ??jedis.del(keyStr);
? ? ? ? }
}?catch?(Exception?e)?{
e.printStackTrace();
}?finally?{
if?(jedis?!=?null)
jedis.close();
}
jedisPool.destroy();
}
}
以上代碼則是批量刪除以某字符串前綴的key 。
簡單實現(xiàn)代碼如下:
EmployeeDao.java
//刪除數(shù)據(jù)
public boolean deleteEmployeeById(int id){
boolean result = false;
try{
con = DBCon.getConn();
String sql = "delete from tb_employee where id=?";
pstmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setInt(1, id);
int i = pstmt.executeUpdate();
if(i == 1)
result = true;
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(pstmt != null){
pstmt.close();
}
}catch(Exception e){
e.printStackTrace();
}
try{
if(con != null){
con.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
TestSql2.java
package com.sql.test;
import com.sql.dao.EmployeeDao;
public class TestSql02 {
public static void main(String[] args){
boolean result = EmployeeDao.getInstance().deleteEmployeeById(1);
if(result == true){
System.out.println("刪除成功!");
}else{
System.out.println("刪除失敗!");
}
}
}
有兩種方法:
1.
使用循環(huán),在循環(huán)里面調(diào)用remove(下標)來循環(huán)刪除數(shù)組中的每一個數(shù)據(jù)
2.
將你的數(shù)組重新new一下,這樣就將原來的數(shù)組覆蓋清空了
文章標題:java清除數(shù)據(jù)代碼 java代碼里面刪除表數(shù)據(jù)
網(wǎng)頁鏈接:http://www.muchs.cn/article20/doeejjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站維護、網(wǎng)站收錄、搜索引擎優(yōu)化、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)