Solved

Java (Select Box) OnChange - Not JavaScript

Posted on 2009-05-14
25
711 Views
Last Modified: 2013-12-07
I need to able to retrieve some values and place them in another select box(parameter_pool_time)
basically when you choose from the select box session type i want it to do a onchange and get me the values in SQL Database i have linked

I am totally clueless as to where to start, please help me i really need this.

i have done this in C# and Access Database before but this is totally different i have no clue how id do it in Java. please help me i have really less time left.

PS: Remember Java not JavaScript.
out.println("<TR><TH>Session Type</TH><TH>Day</TH><TH>Click Button to get times</TH></TR><TR><TD><select name=\"parameter_pool_sessiontype\"><option value=\"Members Swimming\">Members Swimming</option><option value=\"Aqua Aerobic\">Aqua Aerobic</option><option value=\"Adults Only\">Adults Only</option><option value=\"Aqua Deep End Swim\">Aqua Deep End Swim</option><option value=\"Splash Time\">Splash Time</option></select></TD>");

Open in new window

0
Comment
Question by:WarAngelos
  • 14
  • 11
25 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to activate a form submission via javascript that performs the *java* at the server side
0
 

Author Comment

by:WarAngelos
Comment Utility
roughly how do i do that?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Which bit, the javascript or the java?
0
 

Author Comment

by:WarAngelos
Comment Utility
the javascript bit please
0
 

Author Comment

by:WarAngelos
Comment Utility
If this helps this is the whole thing i have

this is incomplete i am using it form another of my pages and working top to down i have not really reached the try/catch part yet I am stuck on how to do onchange.
package dh101;
 
 
 

import java.io.*;
 

import java.util.*;
 

import java.sql.*;
 
 
 

import javax.servlet.*;
 

import javax.servlet.http.*;
 
 
 
 
 

public class Pool extends HttpServlet
 

{ 
 

    public void doGet (
 

			HttpServletRequest	request,
 

			HttpServletResponse	response
 

		) throws ServletException, IOException
 

		{
 
 
 

		PrintWriter	out;
 

		String Title = "Pool Bookings";
 

		String parameter = "";
 

		String pool_username = "";

		String pool_time = "";

		String pool_day = "";

		String pool_sessiontype = "";
 

		String sql = "";
 

		Connection dbcon = null;
 

		Statement stmt = null;
 

		ResultSet rs = null;
 
 
 

		if( (parameter = request.getParameter("parameter_pool_time")) != null) 
 

		  pool_time  = parameter;
 

		if( (parameter = request.getParameter("parameter_pool_day")) != null) 
 

		  pool_day  = parameter;

		if( (parameter = request.getParameter("parameter_pool_sessiontype")) != null) 
 

		  pool_sessiontype = parameter;
 
 
 

 		// set content type 
 

		response.setContentType("text/html");
 

		// then write the data of the response
 

		out = response.getWriter();
 

		out.println("<HTML><HEAD><TITLE>" + Title + "</TITLE></HEAD>");
 

		out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"dh101.css\" />");

		out.println("<BODY><CENTER><H1>" + Title + "</H1>");
 

		out.println("<img height=450 width=700 src=\"images/pool.jpg\">");
 

		out.println("<H3>Pool Booking Form</H3>");
 

		out.println("<form>");

		out.println("<TABLE>");

		out.println("<TR><TH>Personal Details</TH></TR>");
 

		out.println("<TR><TH>Session Type</TH><TH>Day</TH><TH>Click Button to get times</TH></TR><TR><TD><select name=\"parameter_pool_sessiontype\" onchange=\"function()\"><option value=\"Select\">Select</option><option value=\"Members Swimming\">Members Swimming</option><option value=\"Aqua Aerobic\">Aqua Aerobic</option><option value=\"Adults Only\">Adults Only</option><option value=\"Aqua Deep End Swim\">Aqua Deep End Swim</option><option value=\"Splash Time\">Splash Time</option></select></TD>");
 

		out.println("<TD><select name=\"parameter_pool_day\"><option value=\"Select\">Select</option><option value=\"Monday\">Monday</option><option value=\"Tuesday\">Tuesday</option><option value=\"Wednesday\">Wednesday</option><opjavation value=\"Thursday\">Thursday</option><option value=\"Friday\">Friday</option><option value=\"Saturday\">Saturday</option><option value=\"Sunday\">Sunday</option></select></TD>");

		out.println("<TR><TH></BR>Booking Details</TH></TR>");
 

		out.println("<TR><TH>Time</TH><TR><TD><select name=\"parameter_pool_time\"><option value=\"B\">B</option></select></TD>");
 

		out.println("</TR></TABLE></BR>");

		out.println("<input type=submit>");
 

		out.println("</form>");
 

		try 
 

		{
 

			// Load the JDBC-ODBC driver
 

			Class.forName("com.mysql.jdbc.Driver").newInstance();	
 

			dbcon = DriverManager.getConnection("jdbc:mysql://localhost/test");
 

			stmt = dbcon.createStatement();
 

			//insert data into event database table
 

			if(!pool_username.equals("") && !pool_time.equals("") && !pool_day.equals("") && !pool_sessiontype.equals("") )
 

			{
 

				//sql = "INSERT INTO pool VALUES ('" + pool_firstname + "','" + pool_lastname + "','" + pool_start + "','" + pool_end + "','" + pool_date + "','" + pool_sessiontype + "')";
 

				// Load the JDBC-ODBC driver and execute SQL statement
 

				//stmt.executeUpdate(sql);
 

			}
 

			
 

			//read data from database table
 

			rs = stmt.executeQuery("SELECT * FROM pool");
 

			out.println("<TABLE BORDER=1>" );		
 

			out.println("<TR><TH>First Name</TH><TH>Last Name</TH><TH>Start Time</TH><TH>End Time</TH><TH>Date</TH><TH>Session Type</TH></TR>" );		
 

			while ( rs.next() )
 

			{
 

				/*out.println("<TR><TD>" +  rs.getString("pool_firstname") + "</TD>" );

				out.println("<TD>" +  rs.getString("pool_lastname") + "</TD>" );
 

				out.println("<TD>" +  rs.getString("pool_start") + "</TD>" );
 

				out.println("<TD>" +  rs.getString("pool_end").toString() + "</TD>" );

				out.println("<TD>" +  rs.getString("pool_date") + "</TD>" );

				out.println("<TD>" +  rs.getString("pool_sessiontype") + "</TD>" );*/
 

			}
 

			out.println("</TR></TABLE>" );				
 

			stmt.close();
 

			dbcon.close();
 

		}		
 

		 catch( Exception e ) 
 

		{ 
 

            e.printStackTrace(out);
 
 
 

		}//catch	
 

		out.println("</BODY></HTML>");
 

		out.close();
 

			
 

  }
 

}
 

//end/////////////////////////////////////////////////////////////////////////////////////////////////////////

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Are you sure about this onchange thing? I would advise against it and make them click explictly or you'll find that you'll be handling more queries than you (or they) need
0
 

Author Comment

by:WarAngelos
Comment Utility
Please i have no other choice i have a TEST 2morrow Network Software then i have to work Friday Night Shift and Saturday Night Shift Sunday University is closed library does not have linux and Monday i need to Hand this in am totally paniking right now.
0
 

Author Comment

by:WarAngelos
Comment Utility
Basically what i want is if your look at the code there are 3 Select Boxes... 2 of them are preloaded... when they are selected i want it to do the onchange thing then check compare the two value is the thing and get the times and put them in the 3rd select box.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
OK. Here's an onchange example

http://www.java2s.com/Code/JavaScriptReference/Event-Handlers-Reference/onChangeExample.htm

In your case, all you need in 'function1' is
document.forms[0].submit();

Open in new window

0
 

Author Comment

by:WarAngelos
Comment Utility
but how would i connect it to the sql i am really sorry for being a pain... basically i have sql totally figure out in java but in javascript i have not got a clue how to access it and stuff
0
 

Author Comment

by:WarAngelos
Comment Utility
nvm thank you soo much i deffinetly owe you one... i have got it man thanks
0
 

Author Comment

by:WarAngelos
Comment Utility
Just one more thing once i get to sql how to i replace the stuff in select box i know whats going on now its submiting hence activating the try and catch but in there i know how to retrieve the details but how to do add it to the SELECT BOX just this one please tell me thank you.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's it. It's the java side that does the SQL - nothing to do with the javascript. JS just submits the form
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Just one more thing once i get to sql how to i replace the stuff in select box

Well i don't know what your application logic is but basically all you do is rewrite the html (and hence the select element) based on what the query returns
0
 

Author Comment

by:WarAngelos
Comment Utility
Hmm sorry but err right for e.g. when i say choose members swimming and monday

sooo in sql i would be like SELECT SESSION, DAY, TIME FROM SWIMMING WHERE SESSION = '"+SELECTBOX1+"'" and the other select box...

write so it gets me the stuff but it would not change the 3rd select box on its own thats for sure i need the code to put these results into the SELECT BOX.
I dunno how to add not even a clue on how to start

I hope you get me.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to iterate the ResultSet and get the values you need from which you can rewrite the html. What purpose to your app does the third select serve?
0
 

Author Comment

by:WarAngelos
Comment Utility
basically this is pool booking site...

its not a app....its a webpage

Session Type(SELECT BOX 1) and DAY(SELECT BOX 2)

With them I want the time for these two togeather for e.g. session members swimming and day monday
with these two i would like the time of session members swimming available on monday
and insert these times into TIME(SELECTBOX 3) soo the user may choose the time and then place a booking.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
OK, so what you should do is iterate the ResultSet and get the times and put them in the 3rd select. By 'app' i mean of course 'webapp', however it happens to be deployed
0
 

Author Comment

by:WarAngelos
Comment Utility
yeah that is what i want to do i no how to use resultset but how do i insert these values into the 3rd select thats the question i have sorry for being a pain... i appreciate that you have not ditched me so far or i would have thought some one else would have left me ages back LOL
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
Your code should look something like this:
out.println("<TR><TH>Time</TH><TR><TD><select name=\"parameter_pool_time\">");

while (rs.next()) {

	String time = rs.getString("time");

	out.println("<option value=\"" + time + "\">" time + "</option>";

}
 

out.println("</select></TD>");

Open in new window

0
 

Author Comment

by:WarAngelos
Comment Utility
Will this add even if the there are multiple members swimming sessions basically multiple times more than 1? thank you so much ^^
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
It will loop through any and all times present in the ResultSet
0
 

Author Comment

by:WarAngelos
Comment Utility
Okay Thank you very much for you help i really appreciate it... I have to leave campus now the build is closing only library's stay open 24/7.. thank you again.

Ill give the code a go thank you very much.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
OK :-)
0
 

Author Closing Comment

by:WarAngelos
Comment Utility
Thanks a lot you were really helpful thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now