2017-12-08 3 views
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; 
      } 
     } 

    } 
} 

答えて

0

をプリントアウトする必要があります。

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(); 
} 
関連する問題