Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Get one record instead of List of records

Thread view: 
teser3@hotmail.com - 10 Dec 2007 00:57 GMT
I have a class that fetches a record but dont think I need to use List
object because I am fetching one record and not an array of records.
Please advise the best way to change the below class where I assume I
should use something different instead of List? Maybe HashMap? Not
sure how to do this?

public List getRecords(){
       ResultSet rs = null;
       Statement stmt = null;
       Connection connection = null;

       List rows = new ArrayList();

       try
       {
           Class.forName("org.gjt.mm.mysql.Driver");
           connection = DriverManager.getConnection("jdbc:mysql://
localhost/dbase?user=myname&password=thepassword");
           stmt = connection.createStatement();
           rs = stmt.executeQuery("SELECT * from user where userid =
10");

           while(rs.next()){
           RowBean row = new RowBean();
           row.setFirstname(rs.getString("firstname"));
           row.setLastname( rs.getString("lastname"));
           rows.add(row);
           .....
Arne Vajhøj - 10 Dec 2007 01:07 GMT
> I have a class that fetches a record but dont think I need to use List
> object because I am fetching one record and not an array of records.
[quoted text clipped - 3 lines]
>
> public List getRecords(){

>         List rows = new ArrayList();
>         try
>         {

>             rs = stmt.executeQuery("SELECT * from user where userid = 10");
>             while(rs.next()){
[quoted text clipped - 3 lines]
>             rows.add(row);
>             .....

Why not just return a RowBean ?

Arne
teser3@hotmail.com - 10 Dec 2007 01:37 GMT
> tes...@hotmail.com wrote:
> > I have a class that fetches a record but dont think I need to use List
[quoted text clipped - 20 lines]
>
> - Show quoted text -

Thanks,

It would be like this?

public RowBean getRecord(){
       ResultSet rs = null;
       Statement stmt = null;
       Connection connection = null;

       //List rows = new ArrayList();
       RowBean row = new RowBean();

       try
       {
           Class.forName("org.gjt.mm.mysql.Driver");
           connection = DriverManager.getConnection("jdbc:mysql://
localhost/dbase?user=myname&password=thepassword");
           stmt = connection.createStatement();
           rs = stmt.executeQuery("SELECT * from user where userid =
10");

           while(rs.next()){

           row.setFirstname(rs.getString("firstname"));
           row.setLastname( rs.getString("lastname"));
           //rows.add(row);
           }
           .....

           return row;
teser3@hotmail.com - 10 Dec 2007 01:40 GMT
Thanks,

Would it be like this?

public List getRecord(){
       ResultSet rs = null;
       Statement stmt = null;
       Connection connection = null;

       //List rows = new ArrayList();
       RowBean row = new RowBean();

       try
       {
           Class.forName("org.gjt.mm.mysql.Driver");
           connection = DriverManager.getConnection("jdbc:mysql://
localhost/dbase?user=myname&password=thepassword");
           stmt = connection.createStatement();
           rs = stmt.executeQuery("SELECT * from user where userid =
10");

           while(rs.next()){

           row.setFirstname(rs.getString("firstname"));
           row.setLastname( rs.getString("lastname"));
           //rows.add(row);
           }
           .....
           //after end of try block
           return row;
Arne Vajhøj - 10 Dec 2007 02:15 GMT
> Would it be like this?
>
> public List getRecord(){

>         RowBean row = new RowBean();

>             rs = stmt.executeQuery("SELECT * from user where userid = 10");
>
>             while(rs.next()){
>             row.setFirstname(rs.getString("firstname"));
>             row.setLastname( rs.getString("lastname"));
>             }

>             return row;

Yes. Except that you could use if instead of while when
you know there will be one row.

Arne
Lew - 10 Dec 2007 03:18 GMT
>> Would it be like this?
>>
[quoted text clipped - 14 lines]
> Yes. Except that you could use if instead of while when
> you know there will be one row.

Make sure to close the connection.  A finally{} block is the place to do that.

It's also unnecessary to initialize every variable to null at the top of the
method.  Just declare it and initialize it to its value in the moment when the
value becomes available.

You do not need to load the JDBC driver but once, not every time you run the
method.  A static initializer is a good place for that.

Signature

Lew

Arne Vajhøj - 10 Dec 2007 03:34 GMT
>>> Would it be like this?
>>>
[quoted text clipped - 24 lines]
> You do not need to load the JDBC driver but once, not every time you run
> the method.  A static initializer is a good place for that.

That is some very good points !

Arne
teser3@hotmail.com - 12 Dec 2007 00:39 GMT
Thanks, is this an example of DTO (Data Transfer Object)??

public RowBean getRecord(){

       ...
       RowBean row = new RowBean();

       try
       {
           ....

           if(rs.next()){
           row.setFirstname(rs.getString("firstname"));
           row.setLastname( rs.getString("lastname"));
           //rows.add(row);
           }
           //catch and finally blocks here

           return row;


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.