• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in usebean

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i am developing a application in jsp. & i am getting an error in tomcat server on using usebean

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 36 in the jsp file: /tool/tool.jsp
Generated servlet error:
tool.connection.SqlConnect cannot be resolved or is not a type

An error occurred at line: 36 in the jsp file: /tool/tool.jsp
Generated servlet error:
tool.connection.SqlConnect cannot be resolved or is not a type


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:389)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)




here is the code that i am using

<jsp:useBean id = "connect" scope = "session" type = "tool.connection.SqlConnect" />

its very urgent plz help
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose that tool.connection.SqlConnect is not on the classpath. Try to fix it.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can u plz explain that
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to have your packages somewhere on classpath. Easiest way is put them to WEB-INF/classes.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks david
that problem is solved but for each & every field in the bean class, i am getting this error
The field connect.stmt is not visible

where connect is the id name in the usebean & stmt is a field in the bean class.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Must be public to be visible, but good practice is to use getters and setters for fields.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you show us this SqlConnect object you're using?
Also describe how it's getting bound to session scope before your JSP is invoked.
[ September 20, 2005: Message edited by: Ben Souther ]
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* tool.connection.sqlconnect.java */
package tool.connection;

import java.sql.*;

public class SqlConnect
{
Connection conn = null;
Statement stmt = null;
//ResultSet rs = null;
public SqlConnect(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc dbc:mydsn","","");
stmt = conn.createStatement();
}catch(Exception e)
{
System.out.println(" e value is "+e);
}
}
}
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone help
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This class really isn't a bean.

In order to access a bean's fields from JSP using <jsp:getProperty or EL, the fields need to have accessors (getters). Even then, they will coerced into strings in the process.

A better approach would be to perform all of your JDBC operations in the bean, store the results in beans or lists of beans, bind the results to either request or session scope, THEN access the bean's fields from the JSP.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Betsy
---------------------------
The field connect.stmt is not visible



From your posting of the code you use for the bean class, stmt is a variable of type Statement. Why do you try to access such variables from your useBean getProperty actions ?

Are you trying to get the statement object (in which case, what would you do with it ? ). You cannot set Statement objects using setProperty either. Your whole design needs another look. Perhaps it would help if you tell what you are trying to achieve.

cheers,
ram.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have changed my code
<jsp:useBean id = "myclass" scope = "session" type = "tool.connection.SqlConnect" />

<%
java.sql.ResultSet res = myclass.selectQuery();
%>


and in the bean i have it

public static ResultSet selectQuery() throws SQLException
{
rs = stmt.executeQuery("select * from Machine_Details");
return rs;
}

& i am getting an error

java.lang.InstantiationException: bean myclass not found within scope
org.apache.jsp.tool.tool_jsp._jspService(org.apache.jsp.tool.tool_jsp:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

help needed immediately...
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Betsy
------------------------

<jsp:useBean id = "myclass" scope = "session" type = "tool.connection.SqlConnect" />



When you use useBean in this fashion (by defining the 'type' attribute rather than 'class'), the container will not instantiate a bean object for you. It would simply try to find an existing bean object in session scope (bound by an attribute name 'myclass') and give you a reference to that in the jsp. If it cannot find one, it throws an error , which's exactly what happened in your case. To put it short, the code above requires an object of SqlConnect bound to the session by an attribute name 'myclass', else it would fail.

To make it work, use the class attribute, which would do the same as above. Additionally if it does not find such a bean object, it would create one for you (and add it to that scope)





<%
java.sql.ResultSet res = myclass.selectQuery();
%>

Here's the useBean syntax reference docs.


public static ResultSet selectQuery() throws SQLException
{
rs = stmt.executeQuery("select * from Machine_Details");
return rs;
}



Also, at some point you will have to do the above in a different way. A result set is alive only as long as the connection is - in the javabean you create a Connection, get a ResultSet and when you pass the result set to your front end, the associated Connection cannot be released until the 'view' is over with it. Also the clsoing of the ResultSet and Connection will have to occur in the 'view' because that's the place its used last. All this would result in quite unscalable code.

Solution :Use a DataTransferObject - when you get the ResultSet in your bean, populate the data into a model class (a POJO that follows JavaBean conventions) and then close the result set and connection. The view will now manipulate the model object rather than the result set.

Here's a sample -



And then in your Bean



In your jsp



Hope that helps.
cheers,
ram.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have implemented that..
but its giving an error..

java.lang.NoSuchMethodError: tool.connection.SqlConnect.selectQuery()Ljava/sql/ResultSet;
org.apache.jsp.tool.tool_jsp._jspService(org.apache.jsp.tool.tool_jsp:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Betsy,
I would strongly recommend that you find a JDBC tutorial and get your bean working as a standalone (command line) application.
In your main method, you should be able to:
  • Instanciate the bean,
  • Call a method the performs a database query and returns the data in something other than a resultSet (list of beans or multi dimensioned array).
  • Iterates over the data, printing it to the screen.


  • Once you've done this, you'll know that your database code is in good working order.
    At that point look into what it will take to call this bean from a servlet or JSP and present the data in a browser.

    One of server side Java's greatest strengths is that you can separate these concerns and not have to debug 15 different issues at the same time.
     
    ramprasad madathil
    Ranch Hand
    Posts: 489
    Eclipse IDE Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Did you copy paste the code I posted ? Did the bean compile ?

    I ask because



    will not work (which is what's giving a NoSuchMethodError perhaps).

    It ought to be


    Having said that, I think Ben's suggestion is very valid and should be the way to go for you. Your learning will be a lot more organized and less frsutrating (which means you can learn even more) that way.

    cheers,
    ram.
     
    Betsy Camel
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi
    i have dont that but facing another problem. i am able to get all the fields in the database into the list.while displaying i want it to be displayed exactly in the same way as in the database,which i am not able to implement using the arraylist.what other collection can i use ?
    please help..
     
    Sheriff
    Posts: 67747
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    i want it to be displayed exactly in the same way as in the database,which i am not able to implement using the arraylist



    This tells us nothing. Firstly, DBs have no display, so the concept of having it display like it is in the DB makes no sense. What do you mean by that?

    And what is it that the list isn't doing for you?

    You'll get better answers to your questions if you take the time to explain what it is that you are after.
     
    Betsy Camel
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    by using a list all the column values are displayed in a stretch. I need to display the values with respect to the columns.
    eg

    Name SerNo Marks

    Tom 1 99
    Jim 2 89


    while i am able to get it in this manner

    Name SerNo Marks
    Tom
    Jim
    1
    2
    99
    89

    by using the above given code...
    How to i align it....
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You could use nested lists or nested arrays to represent a two dimensional table. A better approach, if you know the column names and types, is to create a simple bean to represent a row in your table and return a List of those.



    Then, for each row in your resultSet, create and populate a MarksRowBean and add it to your List.
     
    Betsy Camel
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thats the problem...the column Number keeps changing & the user has rights to add a column...
    this implies i cannot use the above method.
    Can u tell me some other means
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I also mentioned in the last post that you can use nested arrays or lists to work with dynamic tables. You can use the methods found in ResultSetMetaData to determine how many columns you need to create.
    http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSetMetaData.html
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic