Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get all records in GUI in B&S

 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when application starts in standalone mode (no networking) user will select database file and click on connect button and will be able to see all records in GUI in JTable .


how to retrive intially all records when Application Starts? or there is no need to display all records when Application starts ?

In DBMain interface provided by Sun does not have any methods that gives all records .

I created Data class which implements DBMain interface . In that class I created getContractorList which retrives all the records.

But in GuiController has connection field which is type of DBMain not of Data class .

in GuiController what should be type of connetion it should be type of DBMain interface of Data class ?

I created one class ContractorConnector which makes connection between GUI client and database file.

getLocal is defined in the ContractorConnector class what should be return type of getLocal in ContractorConnector class should it be DBMain or Data ?




please reply
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Data implements DBMain then what is Data? It's a DBMain! So you can pass in an instance of Data or DBMain to your GuiController.

About displaying JTable data, I'm doing UB and once I connect to the data class I display all records using a TableModel.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod KARNANI wrote:

how to retrive intially all records when Application Starts?



Pass an array of empty strings as criteria and use

public int[] find(String[] criteria) throws RecordNotFoundException

to get an array of record numbers. Then loop thru for each record number in the returned array using

public String[] read(int recNo) throws RecordNotFoundException

to get String[] data. Now add the String[] to tableModel and the String[] data would get displayed in the gui table associated with the tableModel

pramod KARNANI wrote:
when application starts in standalone mode (no networking) user will select database file and click on connect button and will be able to see all records in GUI in JTable .

or there is no need to display all records when Application starts ?



If the text you have posted in bold is part of requirement, then you are required to display all records in JTable GUI when application starts in standalone mode
 
Yudiman Kwanmas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding Kumaraguru's suggestion, I think the sole purpose of method "public int[] find(String[] criteria) throws RecordNotFoundException" is to implement the search functionality of our application (invoked after the search button in our UI is clicked).

In my case, I decided to create a public method in class Data which implements DB (DBMain in Karnani's project). This method returns a list type "Record" (the value/transfer object in my app).
Well, the drawback is this method is not defined in the DB/DBMain as we are not allowed to change the supplied interface.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Searching for all records is also searching. So it can be used when Search button is clicked or by any other code.

2. If getAllRecords() public method is added to Data class which implements DB interface and your GUI class is using a member of DB interface, then the new method you added would not be known to the GUI class though an instance of Data class can be passed wherever an instance of DB interface is expected.

DB mydb = new Data(...);
mydb.getAllRecords(); //Compile error
 
Yudiman Kwanmas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...point taken for the search method, but well in my case, this will be "Data db = new Data();" if we run my app using the non-networked mode.

Because the spec only describes that the class Data has to implement the DB interface. But it doesn't mention that we have to use DB to instantiate the class Data.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yudiman Kwanmas wrote:
Because the spec only describes that the class Data has to implement the DB interface. But it doesn't mention that we have to use DB to instantiate the class Data.


OOPs : read thru

Program to an interface, not an implementation


 
Yudiman Kwanmas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I almost made a fatal mistake because my app breaks one of the fundamental OOP.

Then, what if I did something like below:

1) I created an interface that extends the DB interface. Let's call it DB2. So "interface DB2 extends DB {...}".

2) DB2 contains the declaration for method "getAllRecords()", and methods already declared in interface DB.

3) Class Data implements both DB and DB2, therefore class Data must implement methods from both interfaces.

4) Then, it would be DB2 db = new DB2();

Would it be acceptable from either the OOP & the spec's point of view?
For me, at least it doesn't break the rule that class Data must implement interface DB.

Thanks a lot for correcting my mistake & Thanks in advance for your help
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yudiman Kwanmas wrote:

1) I created an interface that extends the DB interface. Let's call it DB2. So "interface DB2 extends DB {...}".

2) DB2 contains the declaration for method "getAllRecords()", and methods already declared in interface DB.



If an interface extends another interface, then all the methods of the extended interface are part of the extending interface.

Yudiman Kwanmas wrote:

3) Class Data implements both DB and DB2, therefore class Data must implement methods from both interfaces.



Class Data only needs to implement DB2, it will automatically be required to provide method bodies for all methods of interface DB also, thats part of interface DB2 extending interface DB

Yudiman Kwanmas wrote:

4) Then, it would be DB2 db = new DB2();

Would it be acceptable from either the OOP & the spec's point of view?



Then it has to be

DB2 db = new Data(...)

Remember, you cannot create instance of an interface, you can only create instance of a non-abstract class which implements an interface. Your reference is of type interface.

I do not know how this satisfies the spec requirement.

 
Yudiman Kwanmas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ups, yep you right. It must be DB2 db = new Data();

Well what I meant by satisfying the spec's requirement is because the spec says "Your data access class must be called "Data.java", must be in package called "suncertify.db", and must implement the following interface: public interface DB {...} "

If my class Data only implements DB2, then I'm afraid I would break the requirement because class Data doesn't implement interface DB, even though interface DB2 extends interface DB.
I do aware that my code would look a bit "less elegant" if I used this approach. Also, there is a risk that this might confuse some "junior programmers", but I'll just explain everything in my technical documentation.

Anyway, thanks a lot for the correction.




 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your class Data implements interface DB2 and interface DB2 extends interface DB, then class Data statisfies the requirement to implement interface DB
 
Yudiman Kwanmas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the confirmation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic