首页 技术 正文
技术 2022年11月8日
0 收藏 758 点赞 1,587 浏览 4652 个字

bean

package com.test.deamo.bean;import android.os.Parcel;
import android.os.Parcelable;import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;/**
* author: yhf
* Description:
* date: 2016/04/20 18:22
*/
@DatabaseTable(tableName = "tb_account")
public class Account implements Parcelable {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField(columnName = "user_name")
private String userName;// 用户姓名
@DatabaseField(columnName = "password")
private String password;//密码
@DatabaseField(columnName = "token")
private String token;// token值 public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getToken() {
return token;
} public void setToken(String token) {
this.token = token;
} @Override
public int describeContents() {
return ;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.userName);
dest.writeString(this.password);
dest.writeString(this.token);
} public Account() {
} protected Account(Parcel in) {
this.id = in.readInt();
this.userName = in.readString();
this.password = in.readString();
this.token = in.readString();
} public static final Creator<Account> CREATOR = new Creator<Account>() {
@Override
public Account createFromParcel(Parcel source) {
return new Account(source);
} @Override
public Account[] newArray(int size) {
return new Account[size];
}
};
}
DatabaseHelper
package com.test.deamo.utils.db;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 com.test.deamo.bean.Account;import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;/**
* Created by administrator
* Description 数据库操作封装
* 2016/4/27 9:06.
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String TABLE_NAME = "test.db"; private Map<String, Dao> daos = new HashMap<String, Dao>(); private DatabaseHelper(Context context)
{
super(context, TABLE_NAME, null, );
} @Override
public void onCreate(SQLiteDatabase database,
ConnectionSource connectionSource)
{
try
{
TableUtils.createTable(connectionSource, Account.class);
} catch (SQLException e)
{
e.printStackTrace();
}
} @Override
public void onUpgrade(SQLiteDatabase database,
ConnectionSource connectionSource, int oldVersion, int newVersion)
{
try
{
TableUtils.dropTable(connectionSource, Account.class, true);
onCreate(database, connectionSource);
} catch (SQLException e)
{
e.printStackTrace();
}
} private static DatabaseHelper instance; /**
* 单例获取该Helper
*
* @param context
* @return
*/
public static synchronized DatabaseHelper getHelper(Context context)
{
context = context.getApplicationContext();
if (instance == null)
{
synchronized (DatabaseHelper.class)
{
if (instance == null)
instance = new DatabaseHelper(context);
}
} return instance;
} public synchronized Dao getDao(Class clazz) throws SQLException
{
Dao dao = null;
String className = clazz.getSimpleName(); if (daos.containsKey(className))
{
dao = daos.get(className);
}
if (dao == null)
{
dao = super.getDao(clazz);
daos.put(className, dao);
}
return dao;
} /**
* 释放资源
*/
@Override
public void close()
{
super.close(); for (String key : daos.keySet())
{
Dao dao = daos.get(key);
dao = null;
}
}
}

dao类

package com.test.deamo.dao;import android.content.Context;import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.test.deamo.bean.Account;
import com.test.deamo.utils.db.DatabaseHelper;import java.sql.SQLException;public class AccountDao {
private Context context;
private Dao<Account, Integer> accountDao;
private DatabaseHelper databaseHelper; public AccountDao(Context context) {
this.context = context;
try {
databaseHelper = DatabaseHelper.getHelper(this.context);
accountDao = databaseHelper.getDao(Account.class);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 添加用户信息
* @param account
*/
public void add(Account account){
try {
accountDao.create(account);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 更新用户信息
* @param account
*/
public void update(Account account){
try {
accountDao.update(account);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 根据token获取account实体
* @param token
* @return Account account
*/
public Account getAccountWithToken(String token){
return getAccountWithFiled("token", token);
} /**
* 根据字段名和值获取账号实体
* @param fieldName
* @param fieldVal
* @return
*/
public Account getAccountWithFiled(String fieldName, String fieldVal){
Account account = null;
try {
QueryBuilder<Account, Integer> queryBuilder = accountDao.queryBuilder();
Where<Account, Integer> where = queryBuilder.where();
where.eq(fieldName, fieldVal);
account = where.queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
}
return account;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290