2016-07-07 59 views
-2

我是Java和SQL數據庫的初學者,希望得到一些幫助。如何使用MySQL數據庫中的行在Java中創建隊列?

我想製作一個程序,從phpMyAdmin(MySQL)數據庫表中訪問存儲的數據。我知道如何SELECT已經。數據是包含所有關於它們的信息的藥物清單。列標題如下:名稱,day_of_week_to_take,屆滿,製造商等

但我想創建與表的行的隊列抽象數據結構。我想爲每一行創建Nodes。我將首先創建一個class Node因此,對於第一行,它將創建一個Node以及來自該行每列的數據。然後我將不得不創建pushpull方法。 (但是,我相信已經有一個Java庫可以做到這一點?)我怎樣才能創建這個隊列?

該隊列將用於存儲時序的順序。每個Node對象都會在一週中的某一天發生。

E.g.星期一吃藥A,星期二吃藥B.隊列應按每週的順序排列每種藥物。

請查詢我沒有提供的任何信息。謝謝!

編輯

我應該讓這個明確的: 的日子存儲爲String: e.g如果我需要吃藥上週一的字符串是: 1000000。這7個字符串中的每個位置都對應於星期幾。所以如果我必須在星期六和星期天服藥:0000011。等等...

+0

爲什麼不能在Java的集合框架利用現有的隊列? – eatSleepCode

+0

@eatSleepCode那是什麼,你在說什麼?我不知道「Java Collection Framework」是什麼。請你解釋一下嗎? –

+0

我希望你可以通過day_of_week_to_take從db order中選擇行。如果不是如何獲取行? – kaushik

回答

0

這裏是可能的解決方案。我試圖儘可能通用。 我做了一些假設.. i。我沒有設置數據庫。我通過構造函數填充Node類對象。在你的情況下,你通過數據庫行創建Node類對象。數據庫中的每一行對應於Node類的一個對象。

public class Node { 

    private String mediName; 
    private String dayOfWeek; 
    private String expiry; 
    private String manufacturer; 


    public Node(String mediName,String dayOfWeek,String expiry,String manufacturer) 
    { 
     this.mediName=mediName; 
     this.dayOfWeek=dayOfWeek; 
     this.expiry=expiry; 
     this.manufacturer=manufacturer; 
    } 


    public String getMediName() { 
     return mediName; 
    } 
    public void setMediName(String mediName) { 
     this.mediName = mediName; 
    } 
    public String getDayOfWeek() { 
     return dayOfWeek; 
    } 
    public void setDayOfWeek(String dayOfWeek) { 
     this.dayOfWeek = dayOfWeek; 
    } 
    public String getExpiry() { 
     return expiry; 
    } 
    public void setExpiry(String expiry) { 
     this.expiry = expiry; 
    } 
    public String getManufacturer() { 
     return manufacturer; 
    } 
    public void setManufacturer(String manufacturer) { 
     this.manufacturer = manufacturer; 
    } 


} 

以下類被設計成一個隊列數據結構進行操作。

public class QueueNode { 

    private int front=-1; 
    private int rear=-1; 
    private int capacity; 
    private Node[] arr; 

    public QueueNode(int capacity) 
    { 
     this.capacity=capacity; 
     arr=new Node[capacity]; 
    } 

    private boolean isEmpty() 
    { 
     return front==-1; 
    } 

    private boolean isFull() 
    { 
     return (rear+1)%capacity==front; 
    } 

    public void push(Node data) throws Exception 
    { 
     if(isFull()) 
      throw new Exception("Queue is Full"); 
     else 
     { 
      rear=(rear+1)%capacity; 
      arr[rear]=data; 
      if(front==-1) 
       front=rear; 
     } 

    } 

    public Node pull() throws Exception 
    { 
     Node data=null; 
     if(isEmpty()) 
     { 
      throw new Exception("Queue is Empty"); 
     }else 
     { 
      data=arr[front]; 
      if(front==rear) 
       front=rear-1; 
      else 
      { 
       front=(front+1)%capacity; 
      } 
     } 

     return data; 
    } 
} 

下面類的功能是測試節點和QueueNode類..

public class MainClassForQueue { 

     public static void main(String[] args) throws Exception { 

      Node n1=new Node("A","1000000","2017-02-16","ABC"); 
      Node n2=new Node("B","1100000","2017-02-16","ABC"); 
      Node n3=new Node("C","0100000","2017-02-16","ABC"); 
      Node n4=new Node("D","0010000","2017-02-16","ABC"); 
      Node n5=new Node("F","1010000","2017-02-16","ABC"); 
      Node n6=new Node("G","0000001","2017-02-16","ABC"); 
      Node n7=new Node("H","0000011","2017-02-16","ABC"); 
      Node n8=new Node("I","0001000","2017-02-16","ABC"); 
      Node n9=new Node("J","0000100","2017-02-16","ABC"); 
      Node n10=new Node("K","0000010","2017-02-16","ABC"); 
      Node n11=new Node("L","1010101","2017-02-16","ABC"); 
      Node n12=new Node("M","1001011","2017-02-16","ABC"); 
      Node n13=new Node("N","1111111","2017-02-16","ABC"); 

      List<Node> listNode=new ArrayList<Node>(); 
      listNode.add(n1); 
      listNode.add(n2); 
      listNode.add(n3); 
      listNode.add(n4); 
      listNode.add(n5); 
      listNode.add(n6); 
      listNode.add(n7); 
      listNode.add(n8); 
      listNode.add(n9); 
      listNode.add(n10); 
      listNode.add(n11); 
      listNode.add(n12); 
      listNode.add(n13); 

      int size=listNode.size(); 
      QueueNode queue=new QueueNode(7*size); 

      for(int j=0;j<7;j++) 
      { 
       for (int i = 0; i < listNode.size(); i++) { 

        Node data=listNode.get(i); 
        String day=data.getDayOfWeek(); 
        if(day.charAt(j)=='1') 
         queue.push(data); 
       } 

      } 

      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 
      System.out.println(queue.pull().getMediName()); 


     } 

    } 
相關問題