Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql query --on the fly

 
prasanna pati
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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 )
 
prasanna pati
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic