Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Put all Database entries in one JTextArea (Swing)  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently writing a program for my father to save some contracts in a database. I have a gui already and i am able to add data to the database through the gui. Now i am trying to put all the Informations saved in the Database into a JTextArea in my gui.
The thing is now, i only get one entry out of it altough i should get all. I read something about a SwingWorker but i seriously didnt understand anything (i even watched videos). My Code for the entries is:


and this should be put in a TextArea of another Class called "Window".
Do i really need to use a SwingWorker? And how do i actually use it and where?
Hope you can help me.
 
Rancher
Posts: 2971
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i only get one entry 


The setText() method replaces what is shown with the new String.
Use the append() method if you want to keep what was being shown and append the new String following the old.

Note: Please edit your post and fold the long lines to keep them in a smaller horizontal area.
 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

i only get one entry 


The setText() method replaces what is shown with the new String.
Use the append() method if you want to keep what was being shown and append the new String following the old.

Note: Please edit your post and fold the long lines to keep them in a smaller horizontal area.



using append() somehow changed nothing. I still only get one of my 3 Database entries

i sadly can't find an edit key :/
 
Sheriff
Posts: 4838
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the return is inside the while loop.  This would give you only one row.
 
Rancher
Posts: 3454
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above is not how you use a PreparedStatement, by the way.
The code ought to be something like:


And then you bind (set) the values to use for the '?' placeholders:

 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote: It looks like the return is inside the while loop.  This would give you only one row.


indeed it is inside the while loop. When i output it onto the console (via System.out.println) it shows all 3 entries, so i do not think that is wrong. Just to be sure i wrote it outside now. It shows another entry now, but still only one somehow
 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

The above is not how you use a PreparedStatement, by the way.
The code ought to be something like:


And then you bind (set) the values to use for the '?' placeholders:



i did kinda the same just with variables and not manually entering everything
 
Dave Tolls
Rancher
Posts: 3454
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Domenik Steckling wrote:
i did kinda the same just with variables and not manually entering everything



Except that's just a normal Statement, the way you wrote it.
To have a PreparedStatement (and take advantage of the databases ability to pre-plan statements, as well as avoid SQL injection) you need to do the whole binding of variables thing I show above.
Otherwise there's not much point.
 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Domenik Steckling wrote:
i did kinda the same just with variables and not manually entering everything



Except that's just a normal Statement, the way you wrote it.
To have a PreparedStatement (and take advantage of the databases ability to pre-plan statements, as well as avoid SQL injection) you need to do the whole binding of variables thing I show above.
Otherwise there's not much point.



I am very new to the database swing stuff and just saw it in a video and since it works i thought it would be right... will change it then.
 
Knute Snortum
Sheriff
Posts: 4838
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we will need to see the entire program to get a good idea of what is happening.  Or you can write a SSCCE (that's a link).
 
Knute Snortum
Sheriff
Posts: 4838
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Domenik Steckling wrote:I am very new to the database swing stuff ...


Just to be clear, Swing is a library that helps you write a GUI.  It doesn't have anything to do with accessing the database.  That's done with JDBC or other technologies.
 
Knute Snortum
Sheriff
Posts: 4838
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note on posting: since this is a forum and not a mailing list, there is not need to quote the entire post before you reply.  If you quote at all, only quote the relevant part of the previous post.
 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you actually look at my code? i know that is done via JDBC. as i said i already have entries and can put new entries in it but i cant output all of them
 
Norm Radder
Rancher
Posts: 2971
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i cant output all of them


Do some debugging to see what the code is doing:
add a print statement after every place that the code calls the append method (to add text to the text area) to see if there are values being lost(not shown in the text area)
 
Dave Tolls
Rancher
Posts: 3454
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your latest code, as some suggestions have been made for changes, but I don't see a new version of the code?
 
Domenik Steckling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it now. I just had to use a StringWorker

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!