• Post Reply Bookmark Topic Watch Topic
  • New Topic

jtable with database connection  RSS feed

 
Bryan van Ritter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am new to java and started with a simple connection to an oracle database and tried to gather data from the database and place it in a jtable. below is the code that i've used. i can see information in my table, but only the NAME from the select statement is shown, thus meaning the loop is executed once i think? could someone tell me how to solve this issue. thanks, bryan

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


'columns' is 2 at this point, so it will only process column 1 (ie only go around this once).
It should probably be 'j <= columns'.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either your method name "DisplayRecords" is incorrect as this method does more other things, or your code inside method is incorrect, as it does other things than method name suggests. Please split your method and separate certain things from what you're trying to accomplish with this "DisplayRecords" method.

1. stringCon, uName, uPass, con keep those variables as instance variables with access modifier private and pass as arguments to the method which is described in step "2.".
2. create method to get Connection object, it means it will be separated from the method DisplayRecords where it is not suppose to be.
3. create another method (think about the name of it) to pass as an argument the query and return ResultSet.
4. with the next half of the code you can do the same by creating methods with a only single purpose.

It is good that all this code does not laying down in "main" method, but still, this method is the way too long as it is now. Try always to decompose your code to a small methods (body of the methods could be even 1 line long), so you could reuse small pieces of code in a format of method in other parts of your code.
 
Bryan van Ritter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found the answer thanks to Dave.
i had to do following:

for (int i=1; i< columns; i++){ , change in: for (int i=1; i <= columns; i++){

and also:

for (int j=1; j< columns; j++){ , change in: for (int j=1; j <= columns; j++){

that did the trick,

thanks again for the fast help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!