FeaturesPluginsDocs & SupportCommunityPartners

NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Jtable.SetvalueAT

 
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users
View previous topic :: View next topic  
Author Message
greatkalu



Joined: 21 May 2009
Posts: 1

PostPosted: Thu May 21, 2009 8:13 am    Post subject: Jtable.SetvalueAT Reply with quote

I'm new at java and Nebeans, so please let me know where I'm going wrong. I'm using NetBeans IDE 6.5.1 and the latest MySql.

The database connects and works fine with my Java Project. I want to list all entries of a table in the DB to a JTable.

I've implement a singleton connection to the database, so cannot access the DB directly from the JFrom (class where the table was generated - in my case, BookList.java).

I've looked through the generated code in booklist.java, and the Jtable is created in private void initComponents() { }.

I have a Jbutton that triggers a method in another class, which access the DB and puts the results in arrays. Initially I created arrays here (method on button pressed) and passed them onto the method in the other class (Class Controller, method BkListen) which puts the values from the DB into these passed arrays correctly.

However, in the orginal method (onjButton2), only the last entry is passed back.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int[] BookIDList = new int[100];
String[] AuthorList = new String[100];
String[] TitleList = new String[100];
Controller BkListen = new Controller ();
BkListen.BkListListen(BookIDList, AuthorList, TitleList);

int i = 1;
while (i <= 50 && BookIDList[i]!=0 ) { //
jTable1.setValueAt(BookIDList[i], (i-1), 0);
jTable1.setValueAt(TitleList[i], (i-1), 1);
jTable1.setValueAt(AuthorList[i], (i-1), 2);
i++;
}
}

I'm assuming this has something to do with the new object being instantiated as the arrays don't pass values between classes. This is obviously a Java concept I'm not understanding, so would appreciate any comments that would help me understand more. How can one pass arrays between two methods in different classes?

Thereafter, seeing that only the last instance of the array is transmitted back, I decided to do declare the arrays within the Listener that accesses the DB and call a newly created method in my class JTable specifically for outputing values into the JTable.

public void SQLBookList (){
int BookIDAr[] = new int [100];
String TitleAr[] = new String [100];
String AuthorAr[] = new String [100];
try{
String SQL = "SELECT * FROM booklist";
stmt = (Statement)SingletonConnection.instance().establishConnection().createStatement();

rs = stmt.executeQuery(SQL);
int i = 1;
while (rs.next()) {

BookIDAr[i]= (rs.getInt("id"));
TitleAr[i]= (rs.getString("title"));
AuthorAr[i]= (rs.getString("author"));

BookListing PrintList = new BookListing ();
PrintList.PrintTable(BookIDAr[i], TitleAr[i], AuthorAr[i]);
}
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
}

... with the new method (PrintTable) in class BookListing (which contains the JTable) being:

public void PrintTable (int BookID, String Title,
String Author){


jTable1.setValueAt(BookID, (BookID-1), 0);
jTable1.setValueAt(Title, (BookID-1), 1);
jTable1.setValueAt(Author, (BookID-1), 2);

System.out.println(BookID); // TEST AND IT WORKS
}

I'm finally getting the values from the DB passed into class BookListing. Now the problem is, the output isn't being displayed in the JTable. In fact, I cannot set the value at any cell in this new method (PrintTable) ... where as I could set the values of any table cell in the original on-button-click method (jButton2ActionPerformed). HOW CAN I SOLVE THIS?

Right now, I'm looking to just get it to work. But I would like to know if this is a good programming design. This is why I posted most my code, and if something isn't clear, please let me know.

Will appreciate any help I can get. I am desperate!

Warm regards.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB