File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql query --on the fly" Watch "sql query --on the fly" New topic

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

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


Set<Employees> employeeSet = someMethodthatGetsEmployeeSetFromRecordSet(rs);

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:
subject: sql query --on the fly
Similar Threads
URGENT --Help needed --- Delay in executing SQL Query..
JasperReport : Dynamic SQL Query to inside <queryString> Tag of jrxml file
Create xml file from JDBC result set?
Result set is empty