|
mocha song wrote:
Brian Tkatch wrote:
mocha song wrote:
I am no expert in jdbc, but i would like to comment on the SQL itself. IMO, SELECT * should only be used in EXISTS() and ad hoc queries. (In EXISTS() the SELECT * FROM is redundant, and in ad hoc queries, not much matters.) Instead, spell out the column names. This is self-documenting, and usually helps to protect the code from column changes, such as additions, subtractions, and reorderings. This latter point may not be relevant in your code, as you seek to use the metadata to get the column names and data types. Though, i do believe this is not common, and certainly adds complexity to the code.
If you are having issues, might i suggest that you go for a simpler version, at least at first. That is, spell out the three columns you want, comment out the metadata code, and just hardcode the retrieval of those three. With a more simple approach, it might be easier to identify any issues that arise.
Paul Clapham wrote:How to cause the table model to contain the data from that query?
Seems to me you would have to read rows from the query and, for each query row, add the data in it to a new row in the table model.
map.put("name", result.getString("Name"));
map.put("type", result.getString("Type"));
map.put("price", result.getString("Price"));
mocha song wrote:
Paul Clapham wrote:How to cause the table model to contain the data from that query?
Seems to me you would have to read rows from the query and, for each query row, add the data in it to a new row in the table model.
How do you do that?
As of now, I've managed to get the data from SQL db and stored it as a HashMap<String,Object>.
example: {12.50,type2,Y},{12.00, type1,Z}...etc
Paul Clapham wrote:
mocha song wrote:
Paul Clapham wrote:How to cause the table model to contain the data from that query?
Seems to me you would have to read rows from the query and, for each query row, add the data in it to a new row in the table model.
How do you do that?
Consider using the table model's addRow() method. You can pass an array of Objects or a Vector to that method; in either case you would get the data from a row of your ResultSet.
As of now, I've managed to get the data from SQL db and stored it as a HashMap<String,Object>.
example: {12.50,type2,Y},{12.00, type1,Z}...etc
Yes, I noticed that. But maps aren't going to be of any use here. I assumed you had that code left over from some previous application, so I didn't comment on it, but I don't see any use in creating Map objects if you really need table rows.
mocha song wrote:Hi Paul! I've completely changed the parsing from a map into Vector.
now I have such values:
column : [Name, Type,Price]
data: [[A, Type1, 0.00],[b, Type2, 9.08]]
Can you please advice me on how do you about the looping to match the data to each column? Thanks again!
Paul Clapham wrote:
Of course you've just shown some text representation so maybe you don't have a Vector of Vectors. So how about if you show us the code which goes through the ResultSet and produces that?
mocha song wrote:Kindly please advise me on how am I passing this from DefaultTableModel to a JTable. :/ Thanks Paul!
Paul Clapham wrote:
Well, there's a JTable constructor which takes a TableModel as a parameter. You could use that. Or JTable has a setModel() method which also takes a TableModel as a parameter, and you could use that too. The latter might be better, because if you want to update the JTable with new data which might be in the database, the easiest way is to just generate a new table model (using that code which you posted) and call your table's setModel() method with it.
mocha song wrote:My issue here is probably that I'm passing it through a buffered Reader before setting it into a DefaultTableModel
Paul Clapham wrote:
Well, if you're doing that then surely you're going to have an issue. So stop doing that. You already have something from which you can create a DefaultTableModel directly.
Paul Clapham wrote:
And I notice you don't seem to have a JDBC problem. Your queries and your extraction code are perfectly fine. You just don't know how to get the stuff into a JTable, right? So let's move this thread to the Swing forum.
mocha song wrote:The structure of my programme is that I'll have to pass it back using a reader.. (it affects others functions- which pulls data from JDBC into the client side GUI)
So would you recommend me setting it into a DefaultTableModel before passing it into a reader?
Paul Clapham wrote:
mocha song wrote:The structure of my programme is that I'll have to pass it back using a reader.. (it affects others functions- which pulls data from JDBC into the client side GUI)
So would you recommend me setting it into a DefaultTableModel before passing it into a reader?
No. I would recommend getting rid of that requirement for this part of your application. You have gone to a lot of trouble to extract the data from the database and put it into a structured object which is needed elsewhere. Smashing all of that structure and sending ordinary text makes absolutely no sense. Really. And anyway this part of your application is only going to be producing a JTable. If you have other parts of the application which need to get data from a Reader (which still seems completely wrong to me) then let them continue to do that.
Now if you have a boss who says "Reader or you're fired" then we might want to discuss how to satisfy the boss's design. Otherwise, as you see, I recommend against that.
Unfortunately I read that as, “It everybody else in the group gets it wrong, I'll get it wrong too.” You will have to convice the rest of the error of their ways.mocha song wrote:. . . Unfortunately it's a group project and I'll have to go with the flow.. . . .
Paul Clapham wrote:Okay, fine. So don't create a DefaultTableModel in that method. Design a suitable XML document to contain a text version of the data from your database query and produce a String containing that XML. Then return a StringReader based on that String. You could even wrap that in a BufferedReader if that's a requirement.
Second step: read the XML document from the StringReader, parse it, and produce a DefaultTableModel using something like the code you already have.
You may think this is a ridiculous amount of extra work to do. I agree completely.
this llama doesn't want your drama, he just wants this tiny ad for his mama
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|