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 / September 2007

Tip: Looking for answers? Try searching our database.

Repeated SQL and ResultSet in a method

Thread view: 
francan00@yahoo.com - 27 Sep 2007 01:09 GMT
I have methods that call the same ResultSet statement and SQL and was
wondering if I can put them into a method and call the method instead
of repeating the same lines in each one of my methods:

public int mymethod(MyBean theobject)
{
//db connection part here

String query = "SELECT EmailAddress FROM UserT " +
"WHERE firstname  = '" + theobject.getFirstname () + "' and lastname =
'" + theobject.getLastname() + "'";
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(query);
...
}

public int anothermethod(MyBean theobject)
{
//db connection part here

String query = "SELECT EmailAddress FROM UserT " +
"WHERE firstname  = '" + theobject.getFirstname () + "' and lastname =
'" + theobject.getLastname() + "'";
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(query);
...
}

This would be better:

public int mymethod(MyBean theobject)
{
//db connection part here

//method call here
...
}

public int anothermethod(MyBean theobject)
{
//db connection part here

//method call here
...
}
francan00@yahoo.com - 27 Sep 2007 01:10 GMT
I have methods that call the same ResultSet statement and was
wondering if I can put the ResultSet into a method and call it
instead:

CODE
public int mymethod(MyBean theobject)
{
//db connection part here

String query = "SELECT * FROM UserT " +
"WHERE firstname  = '" + theobject.getFirstname () + "' and lastname =
'" + theobject.getLastname() + "'";
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(query);
...
}

public int anothermethod(MyBean theobject)
{
//db connection part here

String query = "SELECT * FROM UserT " +
"WHERE firstname  = '" + theobject.getFirstname () + "' and lastname =
'" + theobject.getLastname() + "'";
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(query);
...
}

This would be better:

CODE
public int mymethod(MyBean theobject)
{
//db connection part here

//method call here
...
}

public int anothermethod(MyBean theobject)
{
//db connection part here

//method call here
...
}
Donkey Hot - 27 Sep 2007 02:32 GMT
francan00@yahoo.com wrote in news:1190851844.196569.67790
@k79g2000hse.googlegroups.com:

> String query = "SELECT * FROM UserT " +
> "WHERE firstname  = '" + theobject.getFirstname () + "' and lastname =
> '" + theobject.getLastname() + "'";

You should/could use prepared statements and bind variables.

String query = "SELECT * FROM UserT " +
"WHERE firstname  = ? and lastname = ?" ;

Make a PreparedStatement, bind variables to it, and use the same prepared
statement over and over again, only varying to data in the bint variables.

That way your application is

a) effective. The SQL statement is parsed and prepared only once in the
database, saves LOTS of resources and processor time

b) secure and safe. No SQL-injection (google:sql injection) posibility.
Creating SQL statements on the fly using variable values is always
dangerous. What happens if the user enters "Anders'; drop table UserT;
select 1 " to the "First Name" field in your application?

1) Use always Prepared Statements.
2) Prepare them once. Keep them as your class's instance variabled for
example, not local to a procedure.
3) Use them many times. Change only data, not the SQL statement.
Roedy Green - 27 Sep 2007 03:05 GMT
>I have methods that call the same ResultSet statement and was
>wondering if I can put the ResultSet into a method and call it
>instead:

There is nothing magic about JDBC calls.  You can abbreviate them by
encapsulating in methods and passing parameters just like any other
code.

You might want also to look up on PreparedStatement to avoid the
overhead of constructing a query from scratch every time.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2008 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.