⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgdb.java

📁 J2me实现的MSN Messeger客户端程序。聊天、添加好友、删除好友、阻止好友
💻 JAVA
字号:
package vitular.ui;

public class MsgDB{
  private static final short MAX_MSG_COUNT = 20;//最大存储消息数
  private RecordDB db = new RecordDB();
  private String dbName;
  public MsgDB(String dbName) {
    this.dbName = dbName;
    db.open(dbName);
  }

  /**
   * 关闭数据库
   */
  public void close(){
    if (db != null)
      db.close();
  }

  /**
   * 添加消息纪录
   * @param userName String
   * @param msgStr String
   * @throws Exception
   */
  public void addMsg(String name, String msgStr, String timeStr) throws Exception {
    addMsg(name + "$" + msgStr +"$" + timeStr);
//    if (db.getNumRecords() < MAX_MSG_COUNT){//如果还没有超出范围
//      Calendar cal = Calendar.getInstance();
//       db.addRecord(name + "$" + msgStr +"$" + timeStr);
//                    + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH)
//                    + " " + cal.get(Calendar.HOUR_OF_DAY) + ":"+ cal.get(Calendar.MINUTE));
//    }
//    else throw(new Exception("数据库已满"));
  }

  /**
   * 添加消息
   * @param str String
   * @throws Exception
   */
  public void addMsg(String str) throws Exception {
    if (db.getNumRecords() < MAX_MSG_COUNT){//如果还没有超出范围
//      Calendar cal = Calendar.getInstance();
       db.addRecord(str);
    }
    else throw(new Exception("数据库已满"));
  }


  /**
   * 取得消息
   * @param index int
   * @return String
   */
  public String getMsg(int index){
    if (db.getNumRecords() > 0){
      return db.getRecord(index+1);
    }
    else
      return null;
  }

  /**
   * 设置信息
   * @param index int
   * @param str String
   * @throws Exception
   */
  public void setMsg(int index, String str) throws Exception {
    if (this.getMsgCount() < 0 || this.getMsgCount() < index){
      throw(new Exception("超出范围"));
    }else if (this.getMsgCount() == index){
        db.addRecord(str);
    }else{
      db.setRecord(index + 1, str);
    }
  }

  /**
   * 删除消息
   * @param index int
   */
  public void removeMsg(int index){
    db.deleteRecord(index + 1);
  }

  /**
   * 取得存储的消息数
   * @return int
   */
  public int getMsgCount(){
    return db.getNumRecords();
  }

  /**
   * 打开数据库
   */
  public void open(){
    db.open(this.dbName);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -