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

📄 emailconfig.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.rms.*;

/**
 * 该类描述了邮件服务器的配置信息。
 */
public class EMailConfig {
    private static EMailConfig configInstance;   //配置对象实例
    private static final String CONFIG_RMS = "Config";
    private String name;        //用户姓名
    private String emailAddr;   //E-mail
    private String accounts;    //邮箱帐号
    private String password;    //邮箱密码
    private String smtpServer;  //发送邮件服务器
    
    private EMailConfig(){}
    
    //返回一个邮件服务器配置数据实例对象,如果返回null,表示没有配置信息
    public static EMailConfig getInstance() {
        try {
            RecordStore rs = RecordStore.openRecordStore(CONFIG_RMS, false);
            configInstance = new EMailConfig();
            configInstance.name = new String(rs.getRecord(1));
            configInstance.emailAddr = new String(rs.getRecord(2));
            configInstance.accounts = new String(rs.getRecord(3));
            configInstance.password = new String(rs.getRecord(4));
            configInstance.smtpServer = new String(rs.getRecord(5));
            rs.closeRecordStore();
        }
        catch(RecordStoreException rse) {
            System.out.println("error: " + rse.toString());
            configInstance = null;
        }
        
        return configInstance;
    }
    
    //更新邮件服务器的配置数据
    public static void updateConfig(String name, String emailAddr, 
                String accounts, String password, String smtpHost) {
        try {
            byte[] nameBytes = name.getBytes();
            byte[] emailAddrBytes = emailAddr.getBytes();
            byte[] accountsBytes = accounts.getBytes();
            byte[] pswBytes = password.getBytes();
            byte[] smtpBytes = smtpHost.getBytes();
            
            //打开数据库
            RecordStore rs = RecordStore.openRecordStore(CONFIG_RMS, true);
            int numRecords = rs.getNumRecords();
            if(numRecords == 0) {  //新建的数据库,添加记录 
                rs.addRecord(nameBytes, 0, nameBytes.length);
                rs.addRecord(emailAddrBytes, 0, emailAddrBytes.length);
                rs.addRecord(accountsBytes, 0, accountsBytes.length);
                rs.addRecord(pswBytes, 0, pswBytes.length);
                rs.addRecord(smtpBytes, 0, smtpBytes.length);
            }
            else {  //数据库已经存在,更新记录
                rs.setRecord(1, nameBytes, 0, nameBytes.length);
                rs.setRecord(2, emailAddrBytes, 0, emailAddrBytes.length);
                rs.setRecord(3, accountsBytes, 0, accountsBytes.length);
                rs.setRecord(4, pswBytes, 0, pswBytes.length);
                rs.setRecord(5, smtpBytes, 0, smtpBytes.length);
            }
            rs.closeRecordStore();
        }
        catch(RecordStoreException rse) {
            System.out.println("error: " + rse.toString());
        }
        
        if(configInstance != null) {    //更新实例对象中的数据
            configInstance.name = name;
            configInstance.emailAddr = emailAddr;
            configInstance.accounts = accounts;
            configInstance.password = password;
            configInstance.smtpServer = smtpHost;
        }
    }
    
    public String getName() {
        return name;
    }
    
    public String getEmailAddr() {
        return emailAddr;
    }
    
    public String getAccounts() {
        return accounts;
    }
    
    public String getPassword() {
        return password;
    }
    
    public String getSMTPServer() {
        return smtpServer;
    }
}

⌨️ 快捷键说明

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