我在使用 Java Me 时在java移动应用程序中使用dbhelper输出数据时遇到了麻烦。
我需要在数据库上打印出列表。
我在网上找到的只是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;
}
}
}
}
答案 0 :(得分:0)
这个DbHelper
类抽象了J2ME RecordStore
,但它对User
包中的userregistration
对象进行了抽象。
如果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();
}