如何在dbhelper中输出记录

How to ouput records in dbhelper

我在如何在 java 移动应用程序中使用 dbhelper 输出数据时遇到问题,我正在使用 Java Me

我需要打印出数据库中的列表。

我在网上找到的都是sqlite数据库,但不是我用的数据库,对吧?

我只需要打印出所有的数据

package userregistration;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class DbHelper {

    private RecordStore rs;

    public DbHelper() throws RecordStoreException {
        rs = RecordStore.openRecordStore(
                "userregistration",
                true
        );
    }

    public Vector getAll() throws RecordStoreException {
        RecordEnumeration recordEnum
                = rs.enumerateRecords(null, null, false);
        Vector users = new Vector();

        while (recordEnum.hasNextElement()) {
            int id = recordEnum.nextRecordId();
            byte[] data = rs.getRecord(id);

            ByteArrayInputStream bais
                    = new ByteArrayInputStream(data);
            DataInputStream dis
                    = new DataInputStream(bais);

            try {
                String username = dis.readUTF();
                String password = dis.readUTF();
                String fullName = dis.readUTF();

                dis.close();
                bais.close();

                User user = new User();
                user.setId(id);
                user.setFullName(fullName);
                user.setUsername(username);
                user.setPassword(password);
                users.addElement(user);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return users;
    }

    public void delete(int id) throws RecordStoreException {
        rs.deleteRecord(id);
    }

    public void edit(int id,
            String username,
            String password,
            String fullName)
            throws RecordStoreException {

        ByteArrayOutputStream baos
                = new ByteArrayOutputStream();
        DataOutputStream dos
                = new DataOutputStream(baos);
        try {
            dos.writeUTF(username);
            dos.writeUTF(password);
            dos.writeUTF(fullName);

            byte[] b = baos.toByteArray();

            rs.setRecord(id, b, 0, b.length);
        } catch (IOException e) {
            System.out.print(e);
        }

    }

    public void add(String username,
            String password,
            String fullName)
            throws RecordStoreException {

        ByteArrayOutputStream baos
                = new ByteArrayOutputStream();
        DataOutputStream dos
                = new DataOutputStream(baos);
        try {
            dos.writeUTF(username);
            dos.writeUTF(password);
            dos.writeUTF(fullName);

            byte[] b = baos.toByteArray();

            rs.addRecord(b, 0, b.length);
        } catch (IOException e) {
            System.out.print(e);
        }

    }

    public User login(String username, String password) throws RecordStoreNotOpenException, RecordStoreException {
        RecordFilter filter = new LoginFilter(username, password);
        RecordEnumeration recordEnum
                = rs.enumerateRecords(filter, null, false);

        User user = null;

        while (recordEnum.hasNextElement()) {
            int id;
            try {
                id = recordEnum.nextRecordId();
                byte[] data = rs.getRecord(id);

                ByteArrayInputStream bais
                        = new ByteArrayInputStream(data);
                DataInputStream dis
                        = new DataInputStream(bais);

                String fullName = dis.readUTF();

                dis.close();
                bais.close();

                user = new User();
                user.setId(id);
                user.setFullName(fullName);
                user.setUsername(username);
                user.setPassword(password);

            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return user;
    }

    private class LoginFilter implements RecordFilter {

        private final String username;
        private final String password;

        public LoginFilter(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public boolean matches(byte[] candidate) {
            ByteArrayInputStream bais
                    = new ByteArrayInputStream(candidate);
            DataInputStream dis
                    = new DataInputStream(bais);

            try {
                String candidateUsername = dis.readUTF();
                String candidatePassword = dis.readUTF();

                if (username.equals(candidateUsername)
                        && password.equals(candidatePassword)) {
                    return true;
                }
                return false;
            } catch (IOException ex) {
                ex.printStackTrace();
                return false;
            }
        }

    }
}

DbHelper class 抽象了 J2ME RecordStore,但它对 userregistration 包中的 User 对象进行了抽象。

如果RecordStore为空,列表也为空。开始添加一些 User 个实例:

try {
    DbHelper dbHelper = new DbHelper();
    dbHelper.add("john", "password", "John Doe");
    dbHelper.add("mary", "password", "Mary Doe");
} catch (RecordStoreException e) {
    e.printStackTrace();
}

然后阅读:

Vector users = dbHelper.getAll();
for (int i = 0; i < users.size(); i++) {
    User user = (User) users.elementAt(i);
    user.getUsername();
    user.getFullName();
}