5.15数据库操作封装
- 格式:pptx
- 大小:486.87 KB
- 文档页数:26


[Android]Sqlite数据库操作⼯具封装类sqlite 数据库封装类DatabaseUtil.java(封装的类)package com.jack.androidbase.tools;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/*** sqlite 数据库操作类*/public class DatabaseUtil {private static final String TAG = "DatabaseUtil";/*** Database Name*/private static final String DATABASE_NAME = "student_data";/*** Database Version*/private static final int DATABASE_VERSION = 1;/*** Table Name*/private static final String DATABASE_TABLE = "tb_student";/*** Table columns*/public static final String KEY_NAME = "name";public static final String KEY_GRADE = "grade";public static final String KEY_ROWID = "_id";/*** Database creation sql statement*/private static final String CREATE_TABLE ="create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "+ KEY_NAME + " text not null, " + KEY_GRADE + " text not null);";/*** Context*/private final Context mCtx;private DatabaseHelper mDbHelper;private SQLiteDatabase mDb;/*** Inner private class. Database Helper class for creating and updating database.*/private static class DatabaseHelper extends SQLiteOpenHelper {DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}/*** onCreate method is called for the 1st time when database doesn't exists.*/@Overridepublic void onCreate(SQLiteDatabase db) {Log.i(TAG, "Creating DataBase: " + CREATE_TABLE);db.execSQL(CREATE_TABLE);}/*** onUpgrade method is called when database version changes.*/@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.w(TAG, "Upgrading database from version " + oldVersion + " to "+ newVersion);}}/*** Constructor - takes the context to allow the database to be* opened/created** @param ctx the Context within which to work*/public DatabaseUtil(Context ctx) {this.mCtx = ctx;}/*** This method is used for creating/opening connection** @return instance of DatabaseUtil* @throws SQLException*/public DatabaseUtil open() throws SQLException {mDbHelper = new DatabaseHelper(mCtx);mDb = mDbHelper.getWritableDatabase();return this;}/*** This method is used for closing the connection.*/public void close() {mDbHelper.close();}/*** This method is used to create/insert new record record.** @param name* @param grade* @return long*/public long insert(String name, String grade) {ContentValues initialValues = new ContentValues();initialValues.put(KEY_NAME, name);initialValues.put(KEY_GRADE, grade);return mDb.insert(DATABASE_TABLE, null, initialValues);}/*** This method will delete record.** @param rowId* @return boolean*/public boolean delete(long rowId) {return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; }/*** This method will deleteAll record.** @return*/public boolean deleteAll() {return mDb.delete(DATABASE_TABLE, " 1 ", null) > 0;}/*** This method will return Cursor holding all the records.** @return Cursor*/public Cursor fetchAll() {return mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_GRADE}, null, null, null, null, null);}/*** This method will return Cursor holding the specific record.** @param id* @return Cursor* @throws SQLException*/public Cursor fetch(long id) throws SQLException {Cursor mCursor =mDb.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME, KEY_GRADE}, KEY_ROWID + "=" + id, null,null, null, null, null);if (mCursor != null) {mCursor.moveToFirst();}return mCursor;}/*** This method will update record.** @param id* @param name* @param standard* @return boolean*/public boolean update(int id, String name, String standard) {ContentValues args = new ContentValues();args.put(KEY_NAME, name);args.put(KEY_GRADE, standard);return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0;}}使⽤详解:DatabaseUtil dbUtil = new DatabaseUtil(this);dbUtil.open();switch (v.getId()) {case R.id.other_btn_sqlite_list: //查询Cursor cursor = dbUtil.fetchAll();if (cursor != null) {while (cursor.moveToNext()) {Log.i("Student", "ID:" + cursor.getInt(0) + ",Student Name: " + cursor.getString(1) + ",Grade: " + cursor.getString(2));}}break;case R.id.other_btn_sqlite_add:dbUtil.insert("Prashant Thakkar", "100");Log.i("Student", "add over");break;case R.id.other_btn_sqlite_update:dbUtil.update(1, "aa", "bb");Log.i("Student", "update over");break;case R.id.other_btn_sqlite_del:dbUtil.delete(2);Log.i("Student", "delete over");break;case R.id.other_btn_sqlite_del_all:dbUtil.deleteAll();Log.i("Student", "delete all over");break;}dbUtil.close();参考⽹址:https:///sowhat4999/p/4439856.htmlhttps:///ProMonkey/p/5765616.html (有添加单例模式)本博客地址:本⽂原⽂地址:转载请著名出处!谢谢~~。
数据帧封装过程
数据帧封装过程是将原始数据转换为网络上传输的比特流的过程,具体包括以下几个步骤:
1. 应用层:原始数据被转换成二进制数据。
这是数据封装的起点,任何形式的数据最终都会被转换成二进制形式,以便于计算机处理和网络传输。
2. 传输层:在传输层,数据被打上传输层头部,例如TCP或UDP头部,封装成Segment(数据段)。
这一步的关键信息包括端口号,它用于标识发送和接收数据的应用程序。
3. 网络层:在网络层,数据会被打上IP头部,封装成Packet(数据包)。
这一步涉及到的关键信息是IP地址,它用于标识数据包的来源和目的地。
4. 数据链路层:在数据链路层,数据包会被封装成Frame(数据帧)。
这一步通常涉及到添加MAC地址作为物理地址,以及进行CRC校验等,确保数据帧在物理媒介上的准确传输。
5. 物理层:最后,在物理层,数据帧被转换成比特流,通过物理媒介如电缆、光纤等进行传输。
总的来说,整个数据帧封装过程是一个从高层到低层的逐层封装过程,每一层都添加了特定的头部信息,以确保数据能够正确地在网络中传输并最终到达目的地。
在接收端,这个过程会逆向进行,即解封装过程,从比特流中提取出原始数据。
Java-jdbc-封装类形式的数据库操作1、建⽴数据库连接的类package cn.bruce.MySql;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Conutils {//创建数据库连接的⼯具类private static Connection con=null;static{String usename = "root";String password = "jinqi1986";String url = "jdbc:mysql://localhost:3306/mytrain?useSSL=false&serverTimezone=CTT";String jdbc = "com.mysql.cj.jdbc.Driver";try{Class.forName(jdbc);con = DriverManager.getConnection(url, usename, password);} catch (Exception e){throw new RuntimeException(e + "数据库连接失败!");}}public static Connection recon() {return con;}//创建数据库关闭的⼯具类(查询)public static void close(Connection con,Statement st,ResultSet rs){if (rs!=null){try{rs.close();} catch (SQLException e){e.printStackTrace();}}if (st!=null){try{st.close();} catch (SQLException e){e.printStackTrace();}}if (con!=null){try{con.close();} catch (SQLException e){e.printStackTrace();}}}//创建数据库关闭的⼯具类(⾮查询)public static void close(Connection con,Statement st){if (st!=null){try{st.close();} catch (SQLException e){e.printStackTrace();}}if (con!=null){try{con.close();} catch (SQLException e){e.printStackTrace();}}}}2、建⽴存放集合的封装类package cn.bruce.MySql;public class sort {private int zid;private String zname;private double zprice;private String zdesc;// 有参构造器public sort(int zid, String zname, double zprice, String zdesc){// 依照数据库⾥的表字段类型创建this.zid = zid;this.zname = zname;this.zprice = zprice;this.zdesc = zdesc;}// ⽆参构造器public sort(){}// get/set⽅法public int getZid() {return zid;}public void setZid(int zid) {this.zid = zid;}public String getZname() {return zname;}public void setZname(String zname) {this.zname = zname;}public double getZprice() {return zprice;}public void setZprice(double zprice) {this.zprice = zprice;}public String getZdesc() {return zdesc;}public void setZdesc(String zdesc) {this.zdesc = zdesc;}// 重写toStringpublic String toString() {return "sort [zid=" + zid + ", zname=" + zname + ", zprice=" + zprice + ", zdesc=" + zdesc + "]"; }}3、建⽴main类package cn.bruce.MySql;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;public class useutli {public static void main(String[] args) throws Exception {// 调⽤数据库连接的⼯具类Connection connection = Conutils.recon();String sql = "select * from test;";PreparedStatement ps = connection.prepareStatement(sql);ResultSet rs = ps.executeQuery();// 建⽴泛型-sort类型list集合List<sort> list = new ArrayList<sort>();while (rs.next()){sort s = new sort(rs.getInt("zid"), rs.getString("zname"), rs.getDouble("zprice"), rs.getString("zdesc")); list.add(s);}Conutils.close(connection, ps, rs);// 遍历 listfor (sort s : list){System.out.println(s);}}}。