Combobox and description from database

Hi,
 i have a table
called Color with colorid and description.

For example,Color

<select name="Col">
<%
while(rs2.next()){

<option value=<%=rs2...%>><%=rs2....%></option
</select>
a href="javascript:windowHandle = window.open('description.jsp','Project','width=350,height=350,resizeable=yes'); windowHandle.focus()">Add</a>
</p>
The colorid |Description
      red      | The color that reminds of fury.
      blue     | The color that reminds of sea.
      yellow  | The .......

If the user selects red from the combo and wants to see the description of that color he needs to press the desc button where a pop up window would pop up and gives the description of that color.

For example if he goes to red and press the desc button ,
a pop up window pops and gives this description ='
The color that reminds of fury'

I know the jdbc part but i donot how to the desc part, i mean how the database would knowi need the decrip for color red,
or when i need description for color blue

Thanks

Jasbir21Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KaraaCommented:
don't u have desc column in database?
KaraaCommented:
put each color with an id and desc column
when color is chosen respective to that color some id will be selected which can be used to get desc from db
KaraaCommented:

<select name="Col">
<%
while(rs2.next()){

<option value=<%=color>><%=rs2....%></option
</select>

even with this "color" value(jSP varaiable) u can do search of desc from db
String      str = "SELECT COLOR_DESC FROM COL where SURGEON_ID="+color;
            rset = stmt.executeQuery(str);
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

KaraaCommented:

<select name="Col">
<%
while(rs2.next()){

<option value=<%=color>><%=rs2....%></option
</select>

even with this "color" value(jSP varaiable) u can do search of desc from db
String      str = "SELECT COLOR_DESC FROM COL where COL_ID="+color;
            rset = stmt.executeQuery(str);
Jasbir21Author Commented:
Hi,
  I have 2 jsp forms ,
One jsp called view.jsp that has the options.

<select name="Col">
<%
while(rs2.next()){

<option value=<%=color>><%=rs2....%></option
</select>

The other called color.jsp

try
{

   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   
 statement=connection.createStatement();
 String my1="select desc from color where id='"+rs.getString(1)+"'";
rs.getString(1) represents the color

 rs=statement.executeQuery(my1);


My problem is , how do i pass the colorid from view.jsp to color.jsp
Thanks



KaraaCommented:
oh
either use javascript
<script language="JavaScript">
<!--
function getSelected( selectbox ) {
 for (i=0;i<selectbox.length;i++)
    if (selectbox.options[i].selected) return selectbox.options[i];
}

function updateColor( ) {
  var action = "color.jsp?cid="+getSelected(form1.Col).value;
location.replace(action);
}
<select name="Col" onChange="javascript:updateColor()"
>
<%
while(rs2.next()){

<option value=<%=rs2...%>><%=rs2....%></option
</select>

or u can use session variables
Jasbir21Author Commented:
Hi,
 
 I tried that but i got this error:Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
      org.apache.jsp.jsp2.skill_jsp._jspService(skill_jsp.java:60)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(
Thanks

KaraaCommented:
the same problem here, please put your code
Jasbir21Author Commented:
Hi,
This is the code for color.jsp

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

<html>
<body>

<%
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;

ResultSet rs = null;
try
{

   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   
 


}//try
catch( SQLException ex )
{
   ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
   ex.printStackTrace() ;
}

%>


<%
String my="select id from color";
Statement stmt=connection.createStatement();
ResultSet rs2=stmt.executeQuery(my);
%>


Existing Skills :  

<select name="ChooseSkill"
>

<%
while(rs2.next()){ %>

<option value=<%=rs2.getString(1).trim()%>><%=rs2.getString(1).trim()%></option>
<%}%>
</select>


<a href="javascript:windowHandle = window.open('skill.jsp','Skill','width=350,height=350,resizeable=yes'); windowHandle.focus()">Desc</a>


</p>

<input type="submit" value="Submit">
</form>
</Font>


The problem i am facing is that when the user just chooses the red, he has not click it,he just wants to read the description of it, he clicks the desc button, where a small pop up window pops -up and gives teh description.

thanks
Jasbir21Author Commented:
I really need this urgetly...
KaraaCommented:
i am really sorry but i have some urgent work to do today, now i am looking at it
KaraaCommented:
I have tried it like this way
it works
but on the second page skill.jsp , you must have code to get cid (i have used it) value
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<html>
<script language="JavaScript">
<!--
function getSelected( selectbox ) {
 for (i=0;i<selectbox.length;i++)
    if (selectbox.options[i].selected) return selectbox.options[i];
}

function updateColor(level ) {
  var action = "skill.jsp?cid="+getSelected(form1.Col).value;
location.replace(action);
}
</script>
<body>

<%
Connection conn = null;
            Statement stmt = null;
            ResultSet rs2 = null;
            
            String connStr="jdbc:oracle:thin:@localhost:1521:greyston";
            conn = DriverManager.getConnection(connStr,
                                                         "jar_admin", "hello");
            stmt = conn.createStatement ();
            String      str = "SELECT SRG_ID FROM F_SURGEON";
            rs2 = stmt.executeQuery(str);
%>

<form name="form1" method="get">
Existing Skills :  
<select name="Col" onChange="javascript:updateColor(1)"

>

<%
while(rs2.next()){ %>

<option value=<%=rs2.getString(1).trim()%>><%=rs2.getString(1).trim()%></option>
<%}%>
</select>


<a href="javascript:windowHandle = window.open('skill.jsp?cid=','Skill','width=800,height=600,resizeable=yes'); windowHandle.focus();">Desc</a>


</p>

<input type="submit" value="Submit">
</form>

</body>
</html>
Jasbir21Author Commented:
Hi,
  I know i am causing you a lot of trouble.I am sorry, hope you understand, i am new to jsp , what is cid value and where do i add it in my code:


skill.jsp
<%@ page import="java.sql.*"%>
<html>
<body>


<%

String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;


ResultSet rs = null;
try
{

   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   
 statement=connection.createStatement();


 String my1="select * from color where id='"+rs.getString(1).trim()+"'";

 rs=statement.executeQuery(my1);
  boolean anyRecords=rs.next();

}//try
catch( SQLException ex )
{
   ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
   ex.printStackTrace() ;
}
/*
finally
{
   try { if( rs != null ) rs.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
   try { if( prep1 != null ) prep1.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}

try { if( prep2 != null ) prep2.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
  try { if( connection != null ) connection.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}


}
*/



%>

Description : <%=rs.getString(2).trim()%>
</body>
</html>


Thanks
KaraaCommented:
in skill.jsp/// i have not tested it but hopefully it will work if your connection to data base works
use
<%

String      colorid= request.getParameter("cid");
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;


ResultSet rs = null;
try
{

   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   
 statement=connection.createStatement();


 String my1="select * from color where id="+colorid;

 rs=statement.executeQuery(my1);
  while(rs.next()){ %>

Description : <%=rs.getString(2).trim()%>
<%}%>
}//try
catch( SQLException ex )
{
   ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
   ex.printStackTrace() ;
}
%>
Jasbir21Author Commented:
Hi,
  I tried that on skill.jsp and i got the error:type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
      org.apache.jsp.jsp2.skill_jsp._jspService(skill_jsp.java:99)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


I think my connection to database is correct because if i use skill.jsp to query the database for different table it work.

Thanks adn sorry for any inconviniece cause
KuldeepchaturvediCommented:
Well what I gather from above discussion is that there is a table named color and it creates a drop down for user to select the color once user selects it then on clicking the desc button he should see a pop up with the description for the color...
based on jasbir21's code above, This is what you have to do..

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

<html>
<body>
<script language="JavaScript">
<!--
function dothis()
{
var x=document.forms[0].ChooseSkill;
var val='';
for (i=0;i<x.length;i++)
   if (x.options[i].selected)
val=x.options[i].value;

var target='skill.jsp?colorid='+val;
 window.open(target,'Skill','width=350,height=350,resizeable=yes');
}
</script>
<%
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;

ResultSet rs = null;
try
{

 
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  connection = DriverManager.getConnection(connectionURL, "", "");
 
 


}//try
catch( SQLException ex )
{
  ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
  ex.printStackTrace() ;
}

%>


<%
String my="select id from color";
Statement stmt=connection.createStatement();
ResultSet rs2=stmt.executeQuery(my);
%>


Existing Skills :  

<select name="ChooseSkill"
>

<%
while(rs2.next()){ %>

<option value=<%=rs2.getString(1).trim()%>><%=rs2.getString(1).trim()%></option>
<%}%>
</select>


<a href="javascript:dothis();">Desc</a>


</p>

<input type="submit" value="Submit">
</form>
</Font>

and in skill.jsp
>>>>>>>>>>



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


<%

String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{

 
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  connection = DriverManager.getConnection(connectionURL, "", "");
 
 statement=connection.createStatement();


 String my1="select * from color where id='"+request.getParameter("colorid").trim()+"'";

rs=statement.executeQuery(my1);
}//try
catch( SQLException ex )
{
  ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
  ex.printStackTrace() ;
}
%>

Description : <%=rs.getString(2).trim()%>
</body>
</html>

This is bound to work...
Though I have another idea in my mind as well ( since both code and desc are coming from the same table, its a waste of database connection to get the description on a separate query (if they are not big ) so in my opinion , you should have fetched them in first jsp page it self and then should have created javascript popups with the desc, this would have saved creation of skill.jsp and would have also enhanced the performance...)

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasbir21Author Commented:
hi,
 I tried that but i got the error:
javax.servlet.ServletException: Before start of result set
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:673)
      org.apache.jsp.jsp2.skill_jsp._jspService(skill_jsp.java:83)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.sql.SQLException: Before start of result set
      com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:3622)
      com.mysql.jdbc.ResultSet.getString(ResultSet.java:1772)
      org.apache.jsp.jsp2.skill_jsp._jspService(skill_jsp.java:74)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service

Thanks
KuldeepchaturvediCommented:
yup you will get this for sure.... just put this line in the code

statement=connection.createStatement();


 String my1="select * from color where id='"+request.getParameter("colorid").trim()+"'";

rs=statement.executeQuery(my1);
}//try
catch( SQLException ex )
{
  ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
  ex.printStackTrace() ;
}
if(rs.next())
{
%>

Description : <%=rs.getString(2).trim()%>
<%}%>
</body>
</html>

In the previous code we were not doing a rs.next()....

Regards
Jasbir21Author Commented:
It works, thanks so much.

I like the idea of using javascript but i donot know how to do it , so i am posting another question.

thanks
KuldeepchaturvediCommented:
okay I will answer u there...
KaraaCommented:
I am surprised why u can't concieve my idea? It was also using javascript. There was nothing wrong in that.  Any way good luck!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.