4 Replies - 416 Views - Last Post: 15 December 2015 - 03:10 PM Rate Topic: -----

#1 Giolla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 15

How to display query results from comboBox in GUI

Posted 15 December 2015 - 02:25 PM

Hi, I need to display the results of the query in the same JFrame that hosts my comboBoxes or in another window. I have created a flight DAO but i'm not sure how to diplay the query results.


	@Override
	public void actionPerformed(ActionEvent e) {

		try {
		JComboBox depart_date = (JComboBox)e.getSource();	
		String departD = (String)depart_date.getSelectedItem();
		
		JComboBox fromLoc = (JComboBox)e.getSource();
		String fromL = (String)fromLoc.getSelectedItem();
		
		JComboBox toLoc = (JComboBox)e.getSource();
		String toL = (String)toLoc.getSelectedItem();
		
		ArrayList<Flights> results = new ArrayList<>();
		
		
		 ps = link.prepareStatement("select depart_date, from_loc, to_loc from flights where depart_date = ? and FROM_LOC = ? and to_loc = ?;");
		 ps.setString(1, departD);
		 ps.setString(2, fromL);
		 ps.setString(3, toL);
		 rs = ps.executeQuery();
		 
		 	 while(rs.next()) { 
		 		 
		 		 System.out.println(rs.getString("depart_date") + " " + rs.getString("from_loc") + " " + rs.getString("to_loc"));
		 		 
		 		 
		 	 Flights flight = new Flights(); 
			 flight.setFromD(rs.getString("depart_date"));
			 flight.setFromL(rs.getString("from_loc"));
			 flight.setToL(rs.getString("to_loc"));

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




Is This A Good Question/Topic? 0
  • +

Replies To: How to display query results from comboBox in GUI

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: How to display query results from comboBox in GUI

Posted 15 December 2015 - 02:38 PM

Quote

how to display the query results

That could depend on how you want them to appear to the user.
A text area would be a simple way.
More complicated would be in a JTable.
Was This Post Helpful? 0
  • +
  • -

#3 Giolla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 15

Re: How to display query results from comboBox in GUI

Posted 15 December 2015 - 02:55 PM

View PostNormR, on 15 December 2015 - 02:38 PM, said:

Quote

how to display the query results

That could depend on how you want them to appear to the user.
A text area would be a simple way.
More complicated would be in a JTable.



Thanks for responding,

I have to use GUI for this project however i'm not familiar with JTable. I have String arrays hard-coded into comboBoxes that display options.

I've got to the point where i can select from comboBoxes and call ActionEvent to run a querry in JDBC. I just don't know what is the best way i can display those results in GUI (currently just have everything in JFrame, working with 2 other amateur programmers on this project)

I saw some tutorials on creating a DAO class to store those variables as objects so i created a Flights class and an Arraylist that holds query results as flight objects (i think).. not sure how to move forward.

If i created a String ArrayList instead of using a Flights DAO could i display those as textfields?

thank you
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: How to display query results from comboBox in GUI

Posted 15 December 2015 - 03:07 PM

text areas allow the easy display of lines of text, like would be shown in an editor. No headers or columns. Very simple.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3449
  • View blog
  • Posts: 15,618
  • Joined: 20-September 08

Re: How to display query results from comboBox in GUI

Posted 15 December 2015 - 03:10 PM

rs = ps.executeQuery();
JOptionPane.showMessageDialog(new JTable(DbUtils.resultSetToTableModel(rs)), this);


will do it for you

http://technojeeves....t-to-tablemodel
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1