This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes sql query --on the fly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql query --on the fly" Watch "sql query --on the fly" New topic
Author

sql query --on the fly

prasanna pati
Ranch Hand

Joined: Jan 29, 2005
Posts: 46
from a java program I want to make a SQL query on the fly . I mean the query will be setup based on my logic ( it should not be like I wrote a query in the program and I will make a JDBC call to access a DB ). now what is the best design to do that . I'll use IntelJIdea with MySQL . and what is a DAO .
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
//SQL Query
String sqlQuery = "";

// put your business logic here to build the sqlQuery string

// Create a SQL statement context to execute the Query
Statement stmt = connection.createStatement();

// Execute the formed query and obtain the ResultSet
ResultSet resultSet = stmt.executeQuery( sqlQuery )


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
prasanna pati
Ranch Hand

Joined: Jan 29, 2005
Posts: 46
thanks Lynette Dawson never expected I will get a reply sooo early . anyway then how to show the query result in the Jsp page or like that ? and

actually in that project I have to make one extra layer of abstruction like

view--control---business-logic----------extra layer-----------database

now
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
lets say that your query gets a bunch of employee objects (select * from employees)

loop through the resultset, construct a collection of employee objects and put it in the request attribute

ie.,

Set<Employees> employeeSet = someMethodthatGetsEmployeeSetFromRecordSet(rs);
request.setAttribute("employeeSet",employeeSet);


And in your jsp. get this attribute out of the request:

<% Set<Employees> employeeSet = request.getAttribute("employeeSet"); %>

and use the JSTL tags to loop through this set and display the results
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql query --on the fly
 
Similar Threads
Create xml file from JDBC result set?
FLY BY NIGHT TRAVEL AGENCY
Result set is empty
URGENT --Help needed --- Delay in executing SQL Query..
JasperReport : Dynamic SQL Query to inside <queryString> Tag of jrxml file