1
javaモバイルアプリケーションでdbhelperを使用してデータを出力する方法に問題があります。Java Meを使用しています。dbhelperでレコードを出力する方法
データベースのリストを印刷する必要があります。
ネットで見つかったのはすべてsqliteデータベースですが、使用していたデータベースではありません。
私はこのDbHelper
クラスはJ2ME RecordStore
を抽象化し、それがuserregistration
パッケージで見つかったUser
オブジェクトのためにそうするすべてのデータ
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;
}
}
}
}