2017-12-08 1 views
1

Java Me을 사용하여 Java 모바일 애플리케이션에서 dbhelper를 사용하여 데이터를 출력하는 방법에 문제가 있습니다.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

DbHelper 클래스는 J2ME RecordStore을 추상화를 인쇄해야하지만 userregistration 패키지에서 발견 User 개체에 대해 그렇게.

RecordStore이 비어 있으면 목록도 비어 있습니다. 일부 User 인스턴스를 추가 시작

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

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

을 다음으로 읽을