> 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;