Solved

Combobox and description from database

Posted on 2003-10-27
21
361 Views
Last Modified: 2008-01-09
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

0
Comment
Question by:Jasbir21
  • 10
  • 8
  • 3
21 Comments
 
LVL 2

Expert Comment

by:Karaa
ID: 9626741
don't u have desc column in database?
0
 
LVL 2

Expert Comment

by:Karaa
ID: 9626753
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
 
LVL 2

Expert Comment

by:Karaa
ID: 9626773

<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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Karaa
ID: 9626776

<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
 

Author Comment

by:Jasbir21
ID: 9627080
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
 
LVL 2

Expert Comment

by:Karaa
ID: 9627141
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
 

Author Comment

by:Jasbir21
ID: 9627483
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
 
LVL 2

Expert Comment

by:Karaa
ID: 9630325
the same problem here, please put your code
0
 

Author Comment

by:Jasbir21
ID: 9632536
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
 

Author Comment

by:Jasbir21
ID: 9632822
I really need this urgetly...
0
 
LVL 2

Expert Comment

by:Karaa
ID: 9632843
i am really sorry but i have some urgent work to do today, now i am looking at it
0
 
LVL 2

Expert Comment

by:Karaa
ID: 9632932
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
 

Author Comment

by:Jasbir21
ID: 9632966
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
 
LVL 2

Expert Comment

by:Karaa
ID: 9633015
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
 

Author Comment

by:Jasbir21
ID: 9633116
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
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 300 total points
ID: 9639571
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
 

Author Comment

by:Jasbir21
ID: 9641297
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9641373
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
 

Author Comment

by:Jasbir21
ID: 9641425
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9641626
okay I will answer u there...
0
 
LVL 2

Expert Comment

by:Karaa
ID: 9647851
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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