2015-12-15 76 views
1
package userProfile; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 

public class Database extends JFrame implements ActionListener { 

     JFrame frame; 
     JButton search, mainMenu; 
     JComboBox fromLoc, toLoc, fromDate, fromTime; 
     JLabel fromLabel, toLabel, fromDateLabel, fromTimeLabel; 
     PreparedStatement ps = null; 
     Connection link; 
     ResultSet rs = null; 

    public Database() { 

     frame = new JFrame("Make a Reservation"); 
     frame.getContentPane().setLayout(null);  

     //Arrival date/time comboBoxes and labels 
     fromDateLabel = new JLabel("Departure Date"); 
     fromDateLabel.setBounds(50,230,100,30); 
     fromDate = new JComboBox(new String[]{"12/15/2015", 
        "12/21/2015", "12/21/2015", "12/24/2015"}); 
     fromDate.addActionListener(this); 
     fromDate.setBounds(50,200,100,30); 
     /* 
     fromTimeLabel = new JLabel("Departure Time"); 
     fromTimeLabel.setBounds(160,230,100,30); 
     fromTime = new JComboBox(new String[]{"13:00","15:00", "15:30", "08:00"}); 
     fromTime.addActionListener(this); 
     fromTime.setBounds(160,200,100,30); 
     */ 
     //Departure label and comboBox 
     fromLabel = new JLabel("Departure"); 
     fromLabel.setBounds(50,300,100,30); 
     fromLoc = new JComboBox(new String[]{"Atlanta", "Charleston", "New York", "Los Angeles", "Orlando", "San Francisco"}); 
     fromLoc.addActionListener(this); 
     fromLoc.setBounds(50,270,100,30); 

     toLabel = new JLabel("Arrival"); 
     toLabel.setBounds(160,300,100,30); 
     toLoc = new JComboBox(new String[]{"Atlanta", "Charleston", "New York", "Los Angeles", "Orlando", "San Francisco"}); 
     toLoc.addActionListener(this); 
     toLoc.setBounds(160,270,100,30); 

     search = new JButton("Ok"); 
     search.addActionListener(this); 
     search.setBounds(270,270,100,30); 

     //adding the buttons in frame 
     frame.getContentPane().add(fromDateLabel); 
     frame.getContentPane().add(fromDate); 
     frame.getContentPane().add(fromTimeLabel); 
     frame.getContentPane().add(fromTime); 
     frame.getContentPane().add(fromLabel); 
     frame.getContentPane().add(fromLoc); 
     frame.getContentPane().add(toLabel); 
     frame.getContentPane().add(toLoc); 
     frame.getContentPane().add(search); 

     frame.setSize(400,400); 
     frame.setVisible(true); 

      try { 
       // Driver for mysql 
       Class.forName("com.mysql.jdbc.Driver"); 
       // connection link obj 
       link = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root"); 
       // query statement obj 
      } catch (SQLException sqle) { 
       System.out.println("An error occurred.Maybe user/password is invalid"); 
       sqle.printStackTrace(); 
      } catch (ClassNotFoundException cfne) { 
       cfne.printStackTrace(); 
      } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     JComboBox fromLoc = (JComboBox)e.getSource(); 
     String fromL = fromLoc.getSelectedItem().toString(); 
     JComboBox fromDate = (JComboBox)e.getSource(); 
     String fromD = fromDate.getSelectedItem().toString(); 
     JComboBox toLoc = (JComboBox)e.getSource(); 
     String toL = toLoc.getSelectedItem().toString(); 

     ArrayList<Flights> results = new ArrayList<>(); 

     try { 
     ps = link.prepareStatement("select * from flights where from_date = ? and from_loc = ? and to_loc = ?"); 
     ps.setString(1, fromD); 
     ps.setString(2, fromL); 
     ps.setString(3, toL); 
     rs = ps.executeQuery(); 

      while(rs.next()) { 

      Flights flight = new Flights(); 
      flight.setFromD(rs.getString("from_date")); 
      flight.setFromL(rs.getString("from_loc")); 
      flight.setToL(rs.getString("to_loc")); 

      results.add(flight); 
      } 

     } catch (SQLException sqle) { 
      System.out.println("An error occurred.Maybe user/password is invalid"); 
      sqle.printStackTrace(); 
     } 


    } 

} 

我在運行時遇到NullPointerException。該程序應該使用3個組合框中的數組中的硬編碼字符串查詢數據庫,並將返回的字符串作爲對象保存到ArrayList中。 請幫幫忙,再次感謝 Java:如何從JcomboBox返回JDBC查詢

+0

查詢結果不是在組合框中。它們在您的程序中,作爲ResultSet的連續值。不清楚你在問什麼。 – EJP

+0

感謝您的回覆。我想我的第一個問題是如何在執行preparedStatement之後檢索結果。 這是我第一次使用GUI和JDBC,並且我不完全熟悉我可以在ResultSet上調用的所有方法。 因此,如果我想從結果集中檢索這些值,我可以在while循環中的ResultSet中調用toString嗎? – Giolla

回答

0

我將建立一個飛行模型類,並使用它作爲DTO。所以,在你的while(rs.next)我會使用結果實例化Flight對象並將它們添加到ArrayList<Flight>,這可以返回到您需要使用它的地方。 你的第三個問題我不太確定,因爲我沒有和Swing一起工作過很多。 希望我的回覆對你有幫助。如果我誤解了你的問題,請告訴我。

編輯:

甲DTO是用於包含和傳輸數據的數據傳輸對象。因此,一個飛行類看起來是這樣的:

public class Flight{ 
public String fromD; 
public String fromL; 
public String toL;... 
//And so on 
//You also need setters and getters 
} 

然後在您的數據庫類:

ArrayList<Flight> results = new ArrayList<>(); 
while(rs.next){ 
Flight flight = new Flight(); 
flight.setFromD(rs.getString("columnname")); 
//Same method for the other variables you need to set. 
results.add(flight); 
} 
+0

感謝您的回覆。 DTO是一種中間元素嗎?我想更具體地說,我正在尋找Syntax。我不知道我可以在ResultSet上調用哪些方法來創建可放入ArrayList的對象。 謝謝 – Giolla

+0

添加代碼到原始回覆而不是評論:)希望它澄清我的意思 –

+0

再次感謝你給我一個明確的方向。一個問題...我的方法從comboBox獲取字符串是否正確? 我有一個字符串變量分配給comboBox.getSelectedItem()。toString() – Giolla