Java JDBC - Display Multiple Query Results in one GUI
Heya this is my first post. I'm using java in Eclipse and trying to create a GUI interface between a SQL 2005 Database. I can get everything to work aside from getting the joptionpane.showdialogmessage GUI to show all of the results. I can't paste my code right now, because my vpn isn't working.
I have a user input part of the query for the where selection which returns 5-6 rows. Instead of showing all rows within the same gui; it displays the first row and allows you to click 'Ok' and the next gui pops up with the next row and so on.
Tomorrow I will post the code if that is needed.
Re: Java JDBC - Display Multiple Query Results in one GUI
Please Help or suggestions
package mapcom.qualified;
import java.sql.*;
import javax.swing.JOptionPane;
class ExecuteSqlQuery {
public static void main (String[] args) {
try {
String url = "jdbc:sqlserver://local;databaseName=local;integratedSecurity=true";
Connection conn = DriverManager.getConnection(url,"","");
Statement stmt = conn.createStatement();
ResultSet rs;
String fn = JOptionPane.showInputDialog ("Enter Slid Location ID:");
int custslid;
custslid = Integer.parseInt(fn);
rs = stmt.executeQuery("select distinct structures.id a, smquals.servtypelink as b from structures, smquals, smservlocs where structures.id = smservlocs.servloclink and smservlocs.id = smquals.servloclink and structures.slid ="+custslid);
while ( rs.next() ) {
String lastname = rs.getString("b");
String id = rs.getString("a");
{JOptionPane.showMessageDialog(null, "The ID is: " +id+"\n The Last Name is: " +lastname, "Services Structure is Qualified for:" +id , JOptionPane.PLAIN_MESSAGE);
}
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Re: Java JDBC - Display Multiple Query Results in one GUI
Should I be using a Jframe and have the results come across as Jlabels instead?
Re: Java JDBC - Display Multiple Query Results in one GUI
Quote:
Originally Posted by
nov072008
Should I be using a Jframe and have the results come across as Jlabels instead?
You can have whatever you wish...to add all the results to a JOptionPane, append the results together (using a StringBuilder or string addition) as you loop over them, then show the Dialog after the loop is complete. To add to a custom JFrame, you might consider adding the results to an ArrayList, then display in a JTable.
Re: Java JDBC - Display Multiple Query Results in one GUI
So in my code a stringbuilder would look something like this?
String lastname = rs.getString("b");
String id = rs.getString("a");
StringBuilder aaa = new StringBuilder(lastname + id);
Re: Java JDBC - Display Multiple Query Results in one GUI