Creating a dynamic query by choosing the field and getting the report
Hi,
Is there a way to create a report by allowing the user to choose a field from the table and design a query based on that field.
Ex In a table students with fields
(StudentNo, StudentName,Age,Qualification,Hobbies, PlaceofBirth)
Is it possible to generate a query
select * from students where Hobbies=programming;
where the user can be given the option of selecting the field and the value (Hobbies, programming), the query is generated for that, then the report is designed for that query. How could the query be generated?
Regards,
Reshma
[599 byte] By [
ReshmaCBa] at [2007-11-27 5:34:06]

# 1
You sure can ;)
Here's an example.
You can create a JComboBox and have the different categories of hobbies in there and then use ActionListener on the JcomboBox.
The code will be something like this
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==combo)//assuming the JComboBox object is combo
{
String query=new String("");
String st = combo.getSelectedItem().toString();
Connection connection=DriverManager.getConnection("jdbc:odbc:DSN","user","password");
Statement stmt=connection.createStatement();
String qry="SELECT * FROM tablename WHERE hobby=";
String query=qry+st;
ResultSet rs=stmt.execute(query);//Then do whatever you want with the ResultSet object
}
}