Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop down box

 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,

I have two drop down boxes for a field.I select one item from each drop down box,concatenate them and send them to the database.This works fine.


The problem i have is when i retrieve the same data from the database i am not able to place them in the drop down boxes.Can anyone help me out.Here's the code of drop down box.

<tr>
<td>Result</td>
<td><select name="rst1">
<option value="Qualified" id="1">Qualified
<option value="Not Qualified" id="2">Not Qualified
<option value="On Hold" id="3">On Hold
</select>
</td>
<td><select name="rst2">
<option value="Written Test" id="4">Written Test
<option value="Round 1" id="5">Round 1
<option value="Round 2" id="6">Round 2
<option value="Round 3" id="7">Round 3
<option value="Final Round" id="8">Final Round
</select>
</td>
</tr>

I use request.getParameter("rst1") and request.getParameter("rst2") place them in a string and send to database.Now i want to retrieve the selected value and place it in the drop down box.

Thanx in advance.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you trying to place DB values in your drop down box? I mean the code you supplied is not trying to do this thing anywhere.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does your question lie, in the JDBC/SQL, in your servlet or in the HTML?

In HTML, you can indicate a selected option in the following way.

I would suggest the use of a framework like JSF versus coding raw HTML.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right now this looks like a web question, so I'll move it to our Servlets forum. If it turns into a JDBC one, someone can move it back.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest the use of a framework like JSF versus coding raw HTML.


Adopting a framework in order to set one attribute on an HTML element seems a bit extreme to me. In fact, until one knows the ins and outs of Servlets and JSPs fairly well, I actively discourage the use of a framework.
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html>
<body>
<form name="insert" action="test.jsp" method="post">
<table>
<tr>
<td>Result</td>
<td><select name="rst1">
<option value="1">
<option value="2">Qualified
<option value="3">Not Qualified
</select>
<select name="rst2">
<option value="4">
<option value="5">Written Test
<option value="6">Round 1
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

I am using this html form and the jsp code below to store data in database.

<%@ page import="java.sql.*" %>

<%! private Connection con = null; %>
<%! private Statement stmt = null; %>
<%! private ResultSet rs = null; %>

<%
String s1 = request.getParameter("rst1");
String s2 = request.getParameter("rst2");
String s3 = s1 + " "+s2;

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc racle:thin:@10.11.32.86:1521:Resumedb","scott","tiger");
stmt = con.createStatement();
stmt.executeUpdate("insert into test values('"+s3+"')");
}catch(Exception e)
{
out.println("Process error " + e);
}


%>

Now when i retrieve the data i get a particular value.as per the value i want the corresponding data to be dispplayed in the drop down box.can anyone help me out.I have tried till here and am not getting any idea to proceed.

<%@ page import="java.sql.*" %>

<%! private Connection con = null; %>
<%! private Statement stmt = null; %>
<%! private ResultSet rs = null; %>
<%! String s1,s3,s4 = null; %>
<%! String[] s2 = null; %>

<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc racle:thin:@10.11.32.86:1521:Resumedb","scott","tiger");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from test");

while(rs.next())
{
s1 = rs.getString(1);
System.out.println(s1);
s2 = s1.split("\\s");
System.out.println(s2[0]);
System.out.println(s2[1]);

if(s2[0] == "1")
{
s3 = "";
}
if(s2[0] == "2")
{
s3 = "Qualified";
}
if(s2[0] == "3")
{
s3 = "Not Qualified";
}
if(s2[1] == "4")
{
s4 = "";
}
if(s2[1] == "5")
{
s4 = "Written Test";
}
if(s2[2] == "6")
{
s4 = "Round 1";
}
%>
<html>
<body>
<tr>
<td>Result</td>
<td><select name="rst1">
<option value="<%= %>">
</tr>
</body>
</html>
<%
}
}catch(Exception e)
{
out.println("Process error " + e);
}


%>

Thanx in advance
 
Anoop Chandran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your option tag from
<option value="Qualified" id="1">Qualified
to
<option value="Qualified" <%= "Qualified".equalsIgnorecase(s1) ? "selected" : "" %>id="1">Qualified

This you may need to do for all option tags which is not a good practise , so better have a collectiosn where you can store the collection and use a dropdown class to display the same
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page import="java.sql.*" %>

<%! private Connection con = null; %>
<%! private Statement stmt = null; %>
<%! private ResultSet rs = null; %>
<%! String s1,s3,s4 = null; %>
<%! String[] s2 = null; %>

<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc racle:thin:@10.11.32.86:1521:Resumedb","scott","tiger");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from test");

while(rs.next())
{
s1 = rs.getString(1);
System.out.println(s1);
s2 = s1.split("\\s");
System.out.println(s2[0]);
System.out.println(s2[1]);


%>
<html>
<body>
<tr>
<td>Result</td>
<td><select name="rst1">
<%

if(s2[0] == "1")

{
System.out.println("a");
%>
<option selected value="<%= s2[0] %>"></option>
<option value="2">Qualified</option>
<option value="3">Not Qualified</option>

<%
}
else if(s2[0] == "2")
{
System.out.println("b");
%>
<option value="1"></option>
<option selected value="<%= s2[0] %>">Qualified</option>
<option value="3">Not Qualified</option>

<%
}
else if(s2[0] == "3")
{
System.out.println("c");
%>
<option value="1"></option>
<option value="2">Qualified</option>
<option selected value="<%= s2[0] %>">Not Qualified</option>

<%
}
System.out.println("d");
%>
</select>
<select name="rst2">
<%
if(s2[1] == "1")
{
System.out.println("e");
%>
<option selected value="<%= s2[1] %>"></option>
<option value="2">Written Test</option>
<option value="3">Round 1</option>

<%
}
else if(s2[1] == "2")
{
System.out.println("f");
%>
<option value="1"></option>
<option selected value="<%= s2[1] %>">Written Test</option>
<option value="3">Round 1</option>

<%
}
else if(s2[1] == "3")
{
System.out.println("g");
%>
<option value="1" ></option>
<option value="2" >Written Test</option>
<option selected value="<%= s2[1] %>">Round 1</option>

<%
}
System.out.println("h");
%>
</select>
</td>
</tr>
</body>
</html>
<%
}
}catch(Exception e)
{
out.println("Process error " + e);
}


%>

I tried out this way also.but it's not working.please help me out.

Thanx in advance.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried out this way also.but it's not working.please help me out.
Can you give us a hint why it is not working?
 
Steve S.
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one quick comment, you may not want to check the value of strings with the "==" sign. if("1".equals(s1[0])) might do better for you.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Steve S.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
[ December 15, 2005: Message edited by: Bear Bibeault ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice point though Steve, and a point worth making.
While we're making useful points, visu Nekk, you should stop doing this:



It will create instance variables in your JSP and they are very bad. This is unlikely to be the cause of your problem (although we can only guess at the moment), but it is bad form anyway. At any time you will only have a single instance of a JSP and multiple thread accessing it. Therefore each thread can change the value to something it wants, and have that value over written by another thread. We call this 'not thread-safe'.
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve. It worked.

Is there any other way apart from this,because this process requires a lot of hard coding?

Thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic