4.数据库的创建(黑名单)--另 单元测试

本文详细介绍了手机安全应用中数据库的设计与实现过程,包括数据库Bean类定义、数据库配置类设置、数据库创建更新类编写及数据库DAO操作类实现,并提供了单元测试示例。

1数据库的bean

package org.heima.mobilesafe01.bean;

public class BlackInfo {
	public int _id;//
	public String phone;
	public int _type;
	@Override
	public String toString() {
		return "BlackInfo [_id=" + _id + ", phone=" + phone + ", _type="
				+ _type + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + _type;
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BlackInfo other = (BlackInfo) obj;
		if (_type != other._type)
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		return true;
	}
	
	
}

2 数据库的配置类

package org.heima.mobilesafe01.db;

public interface BlackListDB {
	String BLACK_DB = "black.db";
	int VERSION = 1;

	public interface BlackList {
		String TABLE_NAME="blacklist";
		String ID = "_id";
		String PHONE = "phone";
		String TYPE = "type";
		String CREATE_TABLE_SQL = "create table "+TABLE_NAME+"(" + ID
				+ " integer primary key autoincrement," + PHONE + " text,"
				+ TYPE + " integer)";
	}
}

3 数据库的创建更新类

package org.heima.mobilesafe01.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author U
 *	黑名单数据库的帮助类
 */
public class BlackDBHelper extends SQLiteOpenHelper {

	public BlackDBHelper(Context context) {
		super(context, BlackListDB.BLACK_DB, null, BlackListDB.VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create table blacklist _id,phone,type
		db.execSQL("create table blacklist(_id integer primary key autoincrement,phone text,type integer)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}

4 数据库的Dao类

package org.heima.mobilesafe01.db;

import java.util.ArrayList;
import java.util.List;

import org.heima.mobilesafe01.bean.BlackInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BlackDao {

	private BlackDBHelper mDbHelper;

	public BlackDao(Context context) {
		mDbHelper = new BlackDBHelper(context);
	}

	// add
	public boolean addBlack(BlackInfo info) {
		return addBlack(info.phone, info._type);
	}

	public boolean addBlack(String phone, int type) {
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(BlackListDB.BlackList.PHONE, phone);
		values.put(BlackListDB.BlackList.TYPE, type);
		long insert = database.insert(BlackListDB.BlackList.TABLE_NAME, null,
				values);
		database.close();
		return insert != -1;
	}

	// delete
	public boolean delete(String phone) {
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		int delete = database.delete(BlackListDB.BlackList.TABLE_NAME,
				"phone=?", new String[] { phone });
		database.close();
		return delete != 0;
	}
	// update
	public boolean update(String phone,int type){
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		// update tableName set type=? where phone=?
		ContentValues values = new ContentValues();
		values.put(BlackListDB.BlackList.TYPE, type);
		int update=database.update(BlackListDB.BlackList.TABLE_NAME, values, "phone=?", new String[]{phone});
		database.close();
		return update != 0;
	}
	// select
	public List<BlackInfo> queryAllBlackInfos(){
		List<BlackInfo> blackInfos=new ArrayList<BlackInfo>();
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		Cursor cursor = database.rawQuery("select _id,phone,type from blacklist", null);
		if(cursor!=null){
			BlackInfo info=null;
			while(cursor.moveToNext()){
				info=new BlackInfo();
				info._id=cursor.getInt(0);
				info.phone=cursor.getString(1);
				info._type=cursor.getInt(2);
				blackInfos.add(info);
			}
			cursor.close();
		}
		database.close();
		return blackInfos;
	}
	
	public int getCount(){
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		Cursor cursor =database.rawQuery("select count(1) from blacklist",null);
		if(cursor!=null){
			if(cursor.moveToNext()){
				return cursor.getInt(0);
			}
			cursor.close();
		}
		database.close();
		return -1;
	}
	
	/**
	 *  删除所有
	 */
	public void deleteAll(){
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		database.execSQL("delete from blacklist");
		database.close();
	}
	
	public List<BlackInfo> queryPartBlackInfos(int count,int offset){
		List<BlackInfo> blackInfos=new ArrayList<BlackInfo>();
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		Cursor cursor = database.rawQuery("select _id,phone,type from blacklist limit ? offset ?", new String[]{String.valueOf(count),String.valueOf(offset)});
		if(cursor!=null){
			BlackInfo info=null;
			while(cursor.moveToNext()){
				info=new BlackInfo();
				info._id=cursor.getInt(0);
				info.phone=cursor.getString(1);
				info._type=cursor.getInt(2);
				blackInfos.add(info);
			}
			cursor.close();
		}
		database.close();
		return blackInfos;
	}
	
	
	/**
	 * @param phone
	 * @return
	 * 通过电话号码查询他的拦截类型
	 */
	public int getType(String phone){
		SQLiteDatabase database = mDbHelper.getWritableDatabase();
		Cursor cursor =database.rawQuery("select type from blacklist where phone=?",new String[]{phone});
		if(cursor!=null){
			if(cursor.moveToNext()){
				return cursor.getInt(0);
			}
			cursor.close();
		}
		database.close();
		return -1;
	}
	
	
	
	
}


5 单元测试

package org.heima.mobilesafe01.test;

import java.util.List;

import org.heima.mobilesafe01.bean.BlackInfo;
import org.heima.mobilesafe01.db.BlackDao;
import org.heima.mobilesafe01.utils.L;

import android.test.AndroidTestCase;

public class BlackDaoTest extends AndroidTestCase {
	public void testAdd() {
		BlackDao blackDao = new BlackDao(getContext());
		for (int i = 0; i < 50; i++) {
			if(i%2==0){
				blackDao.addBlack("100" + i, 1);
			}else{
				blackDao.addBlack("100" + i, 2);
			}
		}
	}
	
	public void testQuery(){
		BlackDao blackDao = new BlackDao(getContext());
		List<BlackInfo> queryAllBlackInfos = blackDao.queryAllBlackInfos();
		for (BlackInfo info:queryAllBlackInfos) {
			L.d(info.toString());
		}
	}
	
	public void testUpdate(){
		BlackDao blackDao = new BlackDao(getContext());
		blackDao.update("1001", 1);
	}
	
	public void testDelete(){
		BlackDao blackDao = new BlackDao(getContext());
		blackDao.delete("1000");
	}
	
	public void testDeleteAll(){
		BlackDao blackDao = new BlackDao(getContext());
		blackDao.deleteAll();
	}
	
	public void testQueryPart(){
		BlackDao blackDao = new BlackDao(getContext());
		List<BlackInfo> queryAllBlackInfos = blackDao.queryPartBlackInfos(5, 10);
		for (BlackInfo info:queryAllBlackInfos) {
			L.d(info.toString());
		}
	}
	
	public void testGetCount(){
		BlackDao blackDao = new BlackDao(getContext());
		int count = blackDao.getCount();
		L.d("count:"+count);
	}
	
}












-- ========== 🌐 Prosody XMPP Server 全局配置文件 ========== -- -- 目标:构建一个完整的网页客服聊天系统 -- 功能:单聊、群聊、历史消息、文件传输、多设备同步 -- 文档: https://prosody.im/doc/configure -- 检查语法: sudo prosodyctl check config -- 重启服务: sudo systemctl restart prosody -- -- 作者:Chat Assistant -- 时间:2025-04-05 -- ======================================================== ---------- 🔧 全局设置(必须在第一个 VirtualHost 之前) ---------- -- 👤 管理员账号(可选,用于接收注册通知等) admins = { } -- 📦 插件路径(如需加载第三方模块) plugin_paths = { "/var/lib/prosody/custom_plugins" } -- ⚙️ 启用的核心模块列表 modules_enabled = { -- === 基础功能 === "disco"; -- 服务发现 (XEP-0030) "roster"; -- 联系人列表 "saslauth"; -- 认证机制 "tls"; -- TLS 加密通信 "ping"; -- 心跳检测 "time"; -- 时间同步 "version"; -- 版本查询响应 -- === 安全与管理 === "admin_adhoc"; -- XMPP 内部管理命令 "admin_shell"; -- 命令行管理支持 (prosodyctl) "blocklist"; -- 黑名单屏蔽 "limits"; -- 连接速率限制 "mimicking"; -- 地址欺骗防护 "server_contact_info";-- 公开联系信息 "tombstones"; -- 防止已删除账号重新注册 "watchregistrations"; -- 新用户注册提醒 "announce"; -- 广播通知所有在线用户 -- === 用户体验增强 === "bookmarks"; -- 书签同步(自动加入房间) "carbons"; -- 多设备消息同步 (XEP-0280) "dialback"; -- 服务器间身份验证 "motd"; -- 登录欢迎语 "private"; -- 私有数据存储 "pep"; -- 个人事件协议 "smacks"; -- 流量控制与断线重连 (XEP-0198) "vcard4"; -- 新版 vCard (XEP-0292) "vcard_legacy"; -- 兼容旧客户端的 vCard -- === 消息相关(关键!)=== "mam"; -- 消息归档 (XEP-0313) ✅ 查看历史 "default_archive"; -- 自动启用归档所有聊天 "offline"; -- 存储离线消息 ✅ 不丢消息 "register"; -- 开放用户注册 ✅ web 注册入口 -- === Web 接入支持 === "bosh"; -- BOSH HTTP 绑定 (长轮询) "websocket"; -- WebSocket 支持 (现代浏览器首选) "http_files"; -- 提供静态文件服务 (部署前端) -- === 工具与监控 === "account_activity"; -- 最后活动时间 "cloud_notify"; -- 推送通知(移动端唤醒) "csi_simple"; -- 客户端节能优化 "uptime"; -- 查看运行时长 -- "http_openmetrics" -- Prometheus 指标导出(可选) } -- ❌ 可禁用的默认模块(通常不建议关闭) modules_disabled = { -- "c2s"; -- 危险!禁用客户端连接 -- "s2s"; -- 禁用服务器互联 } -- 🌐 HTTP 服务端口(BOSH + 静态文件) http_ports = { 5280 } -- 🔐 HTTPS 端口(用于 WSS 和加密资源) https_ports = { 5281 } -- ✅ 允许浏览器通过 BOSH 发送凭据(跨域登录必需) consider_bosh_secure = true -- ✅ 允许跨域请求(开发调试阶段开启) cross_domain_bosh = true cross_domain_websocket = true -- 🔐 认证方式:内部哈希密码存储 authentication = "internal_hashed" -- 💾 存储方式:使用 SQLite 数据库(推荐) storage = "sql" sql = { driver = "SQLite3", database = "/var/lib/prosody/prosody.sqlite" } -- 🕒 消息归档保留时间(默认一周) archive_expires_after = "1w" -- 📄 日志输出配置 log = { info = "/var/log/prosody/prosody.log"; -- 正常日志 error = "/var/log/prosody/prosody.err"; -- 错误日志 debug = "/var/log/prosody/prosody_debug.log"; -- 调试日志(查看消息体) } -- 🔐 SSL/TLS 证书目录(Let's Encrypt 或手动放置) certificates = "/etc/pki/prosody/" -- 🏷️ PID 文件位置(Linux 系统) pidfile = "/run/prosody/prosody.pid" -- ⚖️ 带宽速率限制(防止滥用) limits = { c2s = { rate = "10kb/s"; -- 客户端输入限速 burst = "20kb"; -- 突发流量允许 }; s2sin = { rate = "30kb/s"; }; } -- 🔐 强制服务器间安全认证 s2s_secure_auth = true -- 📊 是否收集统计信息(可选) -- statistics = "internal" ----------- 🏢 虚拟主机配置 ----------- VirtualHost "szrengjing.com" -- 主机启用状态 enabled = true -- SSL 证书(具体路径) ssl = { key = "/etc/pki/prosody/certs/szrengjing.com.key"; certificate = "/etc/pki/prosody/certs/szrengjing.com.crt"; } -- 主机级启用模块(覆盖全局) modules_enabled = { "roster"; "saslauth"; "tls"; "disco"; "ping"; "mam"; -- ✅ 消息归档 "offline"; -- ✅ 离线消息 "carbons"; -- ✅ 多端同步 "vcard4"; "bookmarks"; } ------ 🔧 组件配置(特殊功能子域名) ------ -- 📁 文件传输代理(NAT 穿透) Component "proxy.szrengjing.com" "proxy65" name = "文件传输代理" proxy65_address = "szrengjing.com" proxy65_port = 7777 -- 💬 多用户聊天室(群聊) Component "conference.szrengjing.com" "muc" name = "客服聊天室" persistent = true -- 房间持久化存在 max_occupants = 50 -- 最大人数 modules_enabled = { "muc_mam"; -- 群聊消息归档 } ---------- 🎯 模块专项配置 ---------- -- 📢 登录时显示提示语 motd_text = "欢迎登录客服系统!今日在线客服: 5人" -- 🛎️ 新用户注册时发送通知给管理员 watch_registrations = true registration_watchers = { "admin@szrengjing.com" } -- 💌 新用户注册后自动收到欢迎消息 welcome_message = "欢迎加入客服团队!请联系管理员获取培训资料。" -- 📞 公开服务器联系方式(客户端可查询) server_contact_info = { { category = "support", type = "email", value = "support@szrengjing.com" }, { category = "support", type = "xmpp", value = "support@szrengjing.com" } } ---------- 📁 包含额外配置文件 ---------- -- 自动加载 conf.d 下的所有 .cfg.lua 文件 Include "conf.d/*.cfg.lua" ---------- 🖥️ 静态文件服务配置 ---------- -- 提供网页前端访问能力(例如你的 chat.html) http_paths = { files = "/www/wwwroot/szrengjing.com/js/chat" -- ✅ 指向你的前端目录 } ---------- ✅ 配置结束 ---------- -- 请勿在此处添加任何内容! -- 更多帮助: https://prosody.im/doc/configure 根据所需要的功能 生成配置完整版源文件 以在线客服 lm 功能进行完整配置 我们以经有了用户聊天窗口 最好是能步消息
最新发布
11-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值