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?
 
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
0
 
KaraaCommented:
don't u have desc column in database?
0
 
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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);
0
 
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);
0
 
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



0
 
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
0
 
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

0
 
KaraaCommented:
the same problem here, please put your code
0
 
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
0
 
Jasbir21Author Commented:
I really need this urgetly...
0
 
KaraaCommented:
i am really sorry but i have some urgent work to do today, now i am looking at it
0
 
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>
0
 
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
0
 
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() ;
}
%>
0
 
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
0
 
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
0
 
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
0
 
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
0
 
KuldeepchaturvediCommented:
okay I will answer u there...
0
 
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.