Get one record instead of List of records

T

teser3

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);
.....
 
A

Arne Vajhøj

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(){
List rows = new ArrayList();
try
{
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);
.....

Why not just return a RowBean ?

Arne
 
T

teser3

Why not just return a RowBean ?

Arne- Hide quoted text -

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

teser3

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

Lew

Arne said:
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.
 
A

Arne Vajhøj

Lew said:
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.

That is some very good points !

Arne
 
T

teser3

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top