使用OrmLite数据库实现本地的账号登录,注册功能

2024-01-20 21:38

本文主要是介绍使用OrmLite数据库实现本地的账号登录,注册功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

OrmLite是基于SQLite的封装的。实现方法是通过对java bean类进行注解的方式,操作简单许多。
首先想在as中使用这个OrmLite类,需要下载两个jar包,至于jar包的用途,可以看看官方文档,这里我将jar包粘上去了,需要使用的可以进行下载。
ormlite架包,点击下载

操作步骤
1、将两个架包拷贝到libs包中,最简单的引用进as中就是点击:

如图所示的按钮
即可。

#

2、新建一个DataBaseHelper类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;import table.User;/*** Created by 80926 on 2016/9/25.*/
public class DataBaseHelper extends OrmLiteSqliteOpenHelper{private final static String DATABASE_NAME = "mydatabase.db";//数据库名称private final static int DATABASE_VERSION = 1;//数据库版本号private Map<String,Dao> maps = new HashMap<>();//使用单例模式进行其他的进行访问private static DataBaseHelper instance;public static synchronized DataBaseHelper getInstance(Context context){if (instance == null){synchronized (DataBaseHelper.class){if (instance == null){instance = new DataBaseHelper(context);}}}return instance;}public synchronized Dao getDao(Class clz) throws SQLException{Dao dao = null;String className = clz.getSimpleName();if (maps.containsKey(className)){dao = maps.get(className);}else {dao = super.getDao(clz);maps.put(className,dao);}return dao;}//关闭所有操作public void close(){super.close();for(String key : maps.keySet()){Dao dao = maps.get(key);dao = null;}}public DataBaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {//完成对数据库的创建和表的建立try {TableUtils.createTable(connectionSource, User.class);} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {try {TableUtils.dropTable(connectionSource,User.class,true);} catch (SQLException e) {e.printStackTrace();}}
}
2、因为OrmLite数据库的建表示在bean类上的,所以新建一个UesrBean类
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;/*** Created by 80926 on 2016/9/25.*/
@DatabaseTable(tableName = "user_info")//数据库表名字
public class User {@DatabaseField(generatedId = true)//数据库的主键--primary keyprivate int id;@DatabaseField(columnName = "name")private String name;@DatabaseField(columnName = "password")//列名private String password;public User(){}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", desc='" + password + '\'' +'}';}public User(String name, String password) {this.name = name;this.password = password;}public User(int id, String name, String desc) {this.id = id;this.name = name;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
3、新建一个UserDao类,用于封装一些增删改查操作
import android.content.Context;import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.j256.ormlite.stmt.Where;import java.sql.SQLException;
import java.util.List;import db.DataBaseHelper;
import table.User;/*** Created by 80926 on 2016/9/25.*/
public class UserDao {private Context context;private Dao<User,Integer> userDao;private DataBaseHelper helper;public UserDao(Context context){//获取userDaohelper = DataBaseHelper.getInstance(context);try {userDao = helper.getDao(User.class);} catch (SQLException e) {e.printStackTrace();}}public void createUser(User user){//创建user表try {userDao.create(user);} catch (SQLException e) {e.printStackTrace();}}public void updateUser(User user){try {userDao.update(user);} catch (SQLException e) {e.printStackTrace();}}/*** @param user* @param id*/public void updateUserById(User user,Integer id){//通过user的id进行更新try {userDao.updateId(user,id);} catch (SQLException e) {e.printStackTrace();}}public void updateUserByBuilder(User user,String name,String id,Integer selectedId){UpdateBuilder builder = userDao.updateBuilder();try {builder.updateColumnValue(name,user.getName()).where().eq(id,selectedId);//将某一条的id(或者其他列中的属性)的name改为新的namebuilder.update();} catch (SQLException e) {e.printStackTrace();}}public void deleteUser(User user) {//删除一整条数据try {userDao.delete(user);} catch (Exception e) {e.printStackTrace();}}public void deleteMuUser(List<User> users){//删除集合中的所有usertry {userDao.delete(users);} catch (SQLException e) {e.printStackTrace();}}public void deleteUserById(List<Integer> ids){//删除list集合中所有的id的条目try {userDao.deleteIds(ids);} catch (SQLException e) {e.printStackTrace();}}//获取所有数据public List<User> listAll(){try {return userDao.queryForAll();} catch (SQLException e) {e.printStackTrace();}return null;}//查询数据库public List<User> queryBuilder1(String name,String password){List<User> list = null;try {list =  userDao.queryBuilder().where().eq("name",name).and().eq("password",password).query();} catch (SQLException e) {e.printStackTrace();}return list;}//查询,或的关系,两者选一即可public List<User> queryBuilder2(String name,String userName1,String userName2){List<User> list  = null;QueryBuilder<User, Integer> queryBuilder = userDao.queryBuilder();Where<User, Integer> where = queryBuilder.where();try {list =    where.and(where.in(name,userName1,userName2),where.eq(name,userName2)).query();}catch (SQLException e){e.printStackTrace();}return list;}
}
4、LoginActivity
public class LoginActivity extends Activity implements View.OnClickListener {private EditText etName;private EditText etPassword;private Button button;private UserDao userDao;private List<User> list;private TextView textViewWangji;private TextView textViewZhuce;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);etName = (EditText) findViewById(R.id.etName);etPassword = (EditText) findViewById(R.id.etPassword);button = (Button) findViewById(R.id.login);textViewWangji = (TextView) findViewById(R.id.text_wangji);textViewZhuce = (TextView) findViewById(R.id.text_zhuce);textViewWangji.setOnClickListener(this);textViewZhuce.setOnClickListener(this);button.setOnClickListener(this);userDao = new UserDao(this);list = userDao.listAll();}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.login:String name = etName.getText().toString();String password = etPassword.getText().toString();if (name.equalsIgnoreCase("")||password.equalsIgnoreCase("")){Toast.makeText(LoginActivity.this, "不能为空!", Toast.LENGTH_SHORT).show();}else {for(int i = 0 ; i < list.size();i++){User user = list.get(i);if (user.getName().equalsIgnoreCase(name)&&user.getPassword().equalsIgnoreCase(password)){startActivity(new Intent(LoginActivity.this,OrmLiteActivity.class));}else if(user.getName().equalsIgnoreCase(name)&&!user.getPassword().equalsIgnoreCase(password)){Toast.makeText(LoginActivity.this, "密码错误,请重新输入", Toast.LENGTH_SHORT).show();etPassword.setText("");}else if(!user.getName().equalsIgnoreCase(name)&&user.getPassword().equalsIgnoreCase(password)){Toast.makeText(LoginActivity.this, "账户错误,请重新输入", Toast.LENGTH_SHORT).show();etName.setText("");}}}break;case R.id.text_wangji:startActivity(new Intent(this,GenggaiActivity.class));break;case R.id.text_zhuce:startActivity(new Intent(this,ZhuceActivity.class));break;}}
}

5、注册界面

public class ZhuceActivity extends Activity implements View.OnClickListener {private EditText etName;private EditText etPW;private EditText etRPW;private Button button;private UserDao userDao;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_zhuce);etName = (EditText) findViewById(R.id.et_userName);etPW = (EditText) findViewById(R.id.et_userPassword);etRPW = (EditText) findViewById(R.id.et_userRePassword);button = (Button) findViewById(R.id.btn_zhuce);button.setOnClickListener(this);userDao = new UserDao(this);}@Overridepublic void onClick(View v) {String name = etName.getText().toString();String etPw = etPW.getText().toString();String etRpw = etRPW.getText().toString();if(name.equalsIgnoreCase("")||etPw.equalsIgnoreCase("")||etRpw.equalsIgnoreCase("")){Toast.makeText(ZhuceActivity.this, "用户密码和账户不能为空", Toast.LENGTH_SHORT).show();etName.setText("");etPW.setText("");etRPW.setText("");}else {if(!etPw.equalsIgnoreCase(etRpw)){Toast.makeText(ZhuceActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();etPW.setText("");etRPW.setText("");}else {User user = new User();user.setName(name);user.setPassword(etPw);userDao.createUser(user);Toast.makeText(ZhuceActivity.this, "注册成功,请登录", Toast.LENGTH_SHORT).show();Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);}}}
}

这篇关于使用OrmLite数据库实现本地的账号登录,注册功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627422

相关文章

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设