OK,
I am just Wondering can any one point me in the Right Direction, I have two Tables inside my Database one is Patient and the Other is History.. Inside my Swing JFrame I have two JTables Under Each Other. When i Connected to the Database, The Patient Table populates. But how do i go about, when i Select a Name inside the Patient JTable, Get the History Table to populate with that person History
I take it I will need to Create another MYSQL Command, and Connect to that Table.
Well first you need to have some kind of action when clicking on the patient record. Then in the action, query the history database and update/display the history jtable.
There are several ways to do the clicking action but I suggest you look into having a table model for the patient table, and maybe even for the history table.
Your code looks right but it doesn't seem to work. When you select the patient record, does the history query get executed? Is the history query even correct? Do you see "changes" to the JTable?
You may want to output the return value from getValueAt() to check what you have.
After thinking about it, the table model approach may be a bit over the top (complicating things). So instead just set the JTable's selectionModel which extends DefaultListSelectionModel. In the overridden valueChanged() method, do the query stuff.
Did you able to get something going? I did a something testing and using a selection model will get you started.
For my testing I have
Patient class = patientId, firstName, lastName
PatientHistory class = historyId, patientId, txnDate, description
patientId for PatientHistory is clearly a foreign key in DB
I hard-coded the data into maps
Of course instead of all those maps, you mostly want to pass in the returned ResultSet to the table models rather than doing the query inside the table model like what you have done. This will also reduced the number of connections. Also in my code the "historyMap" passed in the PatientSelector class is the original full set of data. And inside the selector, I used a localMap to refresh the history table.
I did get the Two Tables working.
with a ResultSetModel, I am trying to figure out how i get the ActionListener() Working with the Tables.
I have my Query for History
SELECT * FROM History WHERE patientid = ?
So i am Hoping that when the user clicks on a Record inside JTable that it should pick up what the patientid is and display the Query1 into the History JTable
Ok i have The Selection Working if i hard Code the Query
But how can i get it to work with this ?
if i hard code it doesn't make a difference which Row I select it going to give me the Same History i would like to get it working that once i select the Row it gives me the History table for that person.
I have tried
No of them are working giving me a YOU have an error in your SQL Syntax check the Manual that corresponds to your MySQL Server Version for the right Syntax to use near '?') at line 1
and the console gives me
Exception in thread "main" java.lang.NullPointerException
at DisplayQueryResults.<init>(DisplayQueryResults.java:329)
at DisplayQueryResults.main(DisplayQueryResults.java:363)
The ? in the query is part of the string in your case. The query should end at "... where patientid =".
In your code then you do
to make your query complete.
To get the patientId from the JTable, you first need to get the selected row, look up the patientId in the patient result set then query the history table.
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.