Using java class and variables declared in java file in jsp

hi everyone
i m trying to seperate business logic form web layer. i don't know i am doing in a right way or not.
i wanted to access my own java class and its variables in jsp.
for this i created java file like this
package ris;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class  NewClass{
    public static void main(String args[]){
        Connection con = null;
        ResultSet rs=null;
        Statement smt=null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
            smt=con.createStatement();
           rs= smt.executeQuery("SELECT * FROM emp");
           while(rs.next()){
            String  str = rs.getString("Name");
            }catch( Exception e){
                String msg="Exception:"+e.getMessage();
            }finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
}next i created a jsp where i want to access String str defined in java class above.
<%--
    Document   : fisrt
    Created on : Jul 25, 2009, 3:00:38 PM
    Author     : REiSHI
--%>
<%@page import="ris.NewClass"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><%=str%></h1>
    </body>
</html>I wanted to print the name field extracted from database by ResultSet.
but it gives error cannot find symbol str.
please help me to find right way to do this.
i am using netbeans ide.

Very bad approach
1) Think if your table contains more than one NAMEs then you will get only the last one with your code.
2) Your String is declared as local variable in the method.
3) You have not created any object of NewClass nor called the method in JSP page. Then who will call the method to run sql?
4) Your NewClass contains main method which will not work in web application, it's not standalone desktop application so remove main.
Better create an ArrayList and then call the method of NewClass and then store the data into ArrayList and return the ArrayList.
It should look like
{code:java}
package ris;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class  NewClass{
    public static ArrayList getNames(){
        Connection con = null;
        ResultSet rs=null;
        Statement smt=null;
        ArrayList nameList = new ArrayList();
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
            smt=con.createStatement();
           rs= smt.executeQuery("SELECT * FROM emp");
           while(rs.next()){
            nameList.add(rs.getString("Name"));
           return nameList;
            }catch( Exception e){
                String msg="Exception:"+e.getMessage();
               </code><code class="jive-code jive-java"><font>return nameList;</code><code class="jive-code jive-java">
            }finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
      </code><code>return nameList;</code>
<code class="jive-code jive-java">    }

Similar Messages

  • Only to import  JAVA CLASS  and JAVA SOURCE  from .DMP file using IMPDP

    hi,
    I have a schema X, In that schema some of the *"JAVA CLASS" and "JAVA SOURCE"* objects are missing ..
    The procedures ,functions..etc objects were updated at X schema..
    I have 1 dmp file of Y schema , containing the missing "JAVA CLASS" and "JAVA SOURCE" s.. Can I import the the same to the schema X using IMPDP
    i tried using INCLUDE clause but it is not working
    eg:
    impdp john1/john1@me11g22 directory=datadump dumpfile=DEVF2WAR.DMP remap_schema=devf2war:john INCLUDE="JAVA CLASS","JAVA CLASS"but error..
    ORA-39001: invalid argument value
    ORA-39041: Filter  "INCLUDE" either identifies all object types or no object types.regards,
    jp

    Double post: IMPDP to import  JAVA CLASS  and JAVA SOURCE from .DMP file
    Already replied.
    Regards
    Gokhan

  • Binding a JavaFX variable to a Java class instance variable

    Hi,
    I am pretty new to JavaFX but have been developing in Java for many years. I am trying to develop a JavaFX webservice client. What I am doing is creating a basic scene that displays the data values that I am polling with a Java class that extends Thread. The Java class is reading temperature and voltage from a remote server and storing the response in an instance variable. I would like to bind a JavaFx variable to the Java class instance variable so that I can display the values whenever they change.
    var conn: WebserviceConnection; // Java class that extends Thread
    var response: WebserviceResponse;
    try {
    conn = new WebserviceConnection("some_url");
    conn.start();
    Thread.sleep(10000);
    } catch (e:Exception) {
    e.printStackTrace();
    def bindTemp = bind conn.getResponse().getTemperature();
    def bindVolt = bind conn.getResponse().getVoltage();
    The WebserviceConnection class is opening a socket connection and reading some data in a separate thread. A regular socket connection is used because the server is not using HTTP.
    When I run the application, the bindTemp and bindVolt are not updated whenever new data values are received.
    Am I missing something with how bind works? Can I do what I want to do with 'bind'. I basically want to run a separate thread to retrieve data and want my UI to be updated when the data changes.
    Is there a better way to do this than the way I am trying to do it?
    Thanks for any help in advance.
    -Richard

    Hi,
    If you don't want to constantly poll for value change, you can use the observer design pattern, but you need to modify the classes that serve the values to javafx.
    Heres a simple example:
    The Thread which updates a value in every second:
    // TimeServer.java
    public class TimeServer extends Thread {
        private boolean interrupted = false;
        public ValueObject valueObject = new ValueObject();
        @Override
        public void run() {
            while (!interrupted) {
                try {
                    valueObject.setValue(Long.toString(System.currentTimeMillis()));
                    sleep(1000);
                } catch (InterruptedException ex) {
                    interrupted = true;
    }The ValueObject class which contains the values we want to bind in javafx:
    // ValueObject.java
    import java.util.Observable;
    public class ValueObject extends Observable {
        private String value;
        public String getValue() {
            return this.value;
        public void setValue(String value) {
            this.value = value;
            fireNotify();
        private void fireNotify() {
            setChanged();
            notifyObservers();
    }We also need an adapter class in JFX so we can use bind:
    // ValueObjectAdapter.fx
    import java.util.Observer;
    import java.util.Observable;
    public class ValueObjectAdapter extends Observer {
        public-read var value : String;
        public var valueObject : ValueObject
            on replace { valueObject.addObserver(this)}
        override function update(observable: Observable, arg: Object) {
             // We need to run every code in the JFX EDT
             // do not change if the update method can be called outside the Event Dispatch Thread!
             FX.deferAction(
                 function(): Void {
                    value = valueObject.getValue();
    }And finally the main JFX code which displays the canging value:
    // Main.fx
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.text.Text;
    import javafx.scene.text.Font;
    import threadbindfx.TimeServer;
    var timeServer : TimeServer;
    var valueObjectAdapter : ValueObjectAdapter = new ValueObjectAdapter();
    timeServer = new TimeServer();
    valueObjectAdapter.valueObject = timeServer.valueObject;
    timeServer.start();
    Stage {
        title: "Time Application"
        width: 250
        height: 80
        scene: Scene {
            content: Text {
                font : Font {
                    size : 24
                x : 10, y : 30
                content: bind valueObjectAdapter.value;
    }This approach uses less cpu time than constant polling, and changes aren't dependent on the polling interval.
    However this cannot be applied to code which you cannot change obviously.
    I hope this helps.

  • Pass table rowtype between java class and PL/SQL package

    I am a complete newbie when it comes to java itself but I've been asked to investigate the possibility of passing a rowtype datatype from a PL/SQL package to a java class and vice versa.
    I have heard of oracle.sql.ArrayDescriptor as an option but this appears to address only SQL types that are defined outside of a package.
    I'm talking about using, say, SCOTT.EMP%rowtype instead of a user-defined type. I'm thinking that a java array could be defined from the definition of a table in the database but I don't know what class would be appropriate.
    Could someone provide a simple proof-of-concept example that shows how you could use a java array to pass data to a package that is expecting a table rowtype variable and conversely a java class that reads the rowtype returned into a java array?
    Would oracle.jdbc.driver.OracleDatabaseMetaData be a way of doing this?
    Thanks!

    Kuassi:
    <br><br>
    Not at all. I didn't feel like you misled me at all. I'm grateful for your help. I think that maybe I failed to describe my problem adequately in the first place.
    <br><br>
    Perhaps I've also gotten stuck on the term <i>ref cursor</i>.
    <br><br> At any rate, the people I'm gathering information for have further refined their requirement. We want to take a collection of data and send it to a procedure. It doesn't have to be a "ref cursor" or "%rowtype" per se, but something that is maintainable and workable from both ends.
    <br><br>
    For example, to use the old SCOTT/TIGER paradigm, let's suppose we have a website where you can indicate which employees work for a given department. The website allows us to fill in information for multiple employees at once. We want to use a servlet that gathers all the employees data and sends it on through to a PL/SQL package so that the data is validated and committed in the database.<br><br>
    Perhaps the java servlet should handle the multiple employee records as an array? If so, how can a PL/SQL procedure be made to recognize that construct?<br><br>
    I can accept that java cannot create a "ref cursor" type to send information to a package, but might this be possible via some other implementation?
    <br><br>
    Hopefully this is a little more clear. If not, please let me know and I'll try to inject more details as to what we are trying to accomplish.
    <br><br>
    Once again, thanks for your persistent replies and help.
    <br>

  • Java Classes and JSP`s (urgent)

    hi, my question is:
    How can i invoke normal java classes` methods using Jsp`s for this??, is it possible??, if it is, then how do you do this, and where do you have to put the java classes in order to find them from a jsp.
    thanks

    Hi,
    You can access normal java classes in JSP pages. You have to import them in the header like this:
    <%@ page language="java" import="//your java class file with package"%>
    or if u r using java beans, then use the <usebean> tag. Put the java classes in the classes directory. Which server u r using? If it is JWS, then u have a specified classes directory, where u have to put all the class files along with their packages. Hope this will help.
    atanu.

  • Java classes and MySql Files

    Hello,
    There is a way to distribute the Mysql db files with yout java application without need to install Mysql in the computer, like in access, you just copy the .mdb file with your java class and create a conection to it.
    Thank You

    No. If you want to do that you should just use Access. mySQL files will not work without the mySQL server.

  • Difference b/w Java Class and Bean class

    hi,
    can anybody please tell me the clear difference between ordinary java class and java Bean class. i know that bean is also a java class but i donno the exact difference between the both.
    can anybody please do help me in understanding the concept behind the bean class.
    Thank u in advance.
    Regards,
    Fazlina

    While researching this question, I came across this answer by Kim Fowler. I think it explains it better than any other answer I have seen in the forum.
    Many thanks Kim
    Hi
    Luckily in the java world the definition of components is a little
    less severe than when using COM (I also have, and still occasionaly
    do, worked in the COM world)
    Firstly there are two definitions that need to be clarified and
    separated: JavaBean and EnterpriseJavaBean (EJB)
    EJB are the high end, enterprise level, support for distributed
    component architectures. They are roughly equivalent to the use of MTS
    components in the COM/ COM+ world. They can only run within an EJB
    server and provide support, via the server, for functionality such as
    object pooling, scalability, security, transactions etc. In order to
    hook into this ability EJB have sets of interfaces that they are
    required to support
    JavaBeans are standard Java Classes that follow a set of rules:
    a) Hava a public, no argument constructor
    b) follow a naming patterns such that all accessor and modifier
    functions begin with set/ get or is, e.g.
    public void setAge( int x)
    public int getAge()
    The system can then use a mechanism known as 'reflection/
    introspection' to determine the properties of a JavaBean, literally
    interacting with the class file to find its method and constructor
    signatures, in the example above the JavaBean would end with a single
    property named 'age' and of type 'int' The system simply drops the
    'set' 'get' or 'is' prefix, switches the first letter to lower case
    and deduces the property type via the method definition.
    Event support is handled in a similar manner, the system looks for
    methods similar to
    addFredListener(...)
    addXXXListener
    means the JavaBean supports Fred and XXX events, this information is
    particularly useful for Visual builder tools
    In addition there is the abiliity to define a "BeanInfo' class that
    explicitly defines the above information giving the capability to hide
    methods, change names etc. this can also be used in the case where you
    cannot, for one reason or another, use the naming patterns.
    Finally the JavaBean can optionally - though usually does - support
    the Serializable interface to allow persistence of state.
    As well as standard application programming, JavaBeans are regularly
    used in the interaction between Servlets and JSP giving the java
    developer the ability to ceate ojbect using standard java whilst the
    JSP developer can potentially use JSP markup tags to interact in a
    more property based mechanism. EJB are heaviliy used in Enterprise
    application to allow the robust distribution of process
    HTH.
    Kim

  • Drop java class and java source

    Hi,
    When I query user_objects I get some java class and java source objects
    select * from user_objects where object_type like 'JAVA %';
    How do I drop these java source and java class objects from the database
    I tried using the following command
    drop java class /4a524d89_AutoTransliteratorPk
    drop java source AutoTransliteratorPkg
    But I get error ORA-29501: invalid or missing Java source, class, or resource name
    Please someone help me in dropping these objects
    thanks
    saaz

    was the java source all caps or was it exactly AutoTransliteratorPkg. If its exactly AutoTransliteratorPkg then try this
    drop java source "AutoTransliteratorPkg"

  • IMPDP to import  JAVA CLASS  and JAVA SOURCE from .DMP file

    hi,
    I have a schema X, In that schema some of the *"JAVA CLASS" and "JAVA SOURCE"* objects are missing ..
    The procedures ,functions..etc objects were updated at X schema..
    I have 1 dmp file of Y schema , containing the missing "JAVA CLASS" and "JAVA SOURCE" s.. Can I import the the same to the schema X using IMPDP
    i tried using INCLUDE clause but it is not working
    eg:
    impdp john1/john1@me11g22 directory=datadump dumpfile=DEVF2WAR.DMP remap_schema=devf2war:john INCLUDE="JAVA CLASS","JAVA CLASS"but error..
    ORA-39001: invalid argument value
    ORA-39041: Filter  "INCLUDE" either identifies all object types or no object types.regards,
    jp

    Hello,
    You should type JAVA_CLASS and JAVA_SOURCE (use underscore instead of space).
    impdp john1/john1@me11g22 directory=datadump dumpfile=DEVF2WAR.DMP remap_schema=devf2war:john INCLUDE="JAVA_CLASS","JAVA_CLASS"Best Regards,
    Gokhan Atil
    If this question is answered, please mark appropriate posts as correct/helpful and the thread as closed. Thanks

  • Difference between Java class and JavaBean?

    What is the difference between a Java class and a JavaBean?
    A class has variables which hold state.
    A class has methods which can do things (like change state).
    And if I understand a JavaBean it is rather like a BIG class...
    i.e.
    A JavaBean has variables which hold state.
    A JavaBean has methods which can do things (like change state).
    So, what's the difference between the two?
    And in case it helps...What is the crossover point between the two? Is there a minimalist type JavaBean which is the same as a class? What is the difference between Java beans and Enterprise Java Beans?
    Thanks.

    Introspection, as I understand it is a bunch of
    methods which allows me to ask what a class can do
    etc. right? So, if I implement a bunch of
    introspection methods for my class then my class
    becomes a JavaBean?Introspection allows a builder tool to discover a Bean's properties, methods, and events, either by:
    Following design patterns when naming Bean features which the Introspector class examines for these design patterns to discover Bean features.
    By explicitly providing the information with a related Bean Information class (which implements the BeanInfo interface).
    I understand now they are completely different.
    Thanks. Very clear.
    I do not understand how they are completely different.
    In fact I don't have a clue what the differences are
    other than to quote what you have written. In your
    own words, what is the difference? This is the "New
    to Java Technology" forum ;-) and I'm new to these
    things.In that case ejbs are way too advanced for you, so don't worry about it.

  • Help Needed in Creating Java Class and Java Stored Procedures

    Hi,
    Can anyone tell how can i create Java Class, Java Source and Java Resource in Oracle Database.
    I have seen the Documents. But i couldn't able to understand it correctly.
    I will be helpful when i get some Examples for creating a Java Class, Java Source and Stored Procedures with Java with details.
    Is that possible to Create a Java class in the oracle Database itself ?.
    Where are the files located for the existing Java Class ?..
    Help Needed Please.
    Thanks,
    Murali.v

    Hi Murali,
    Heres a thread which discussed uploading java source file instead of runnable code on to the database, which might be helpful :
    Configure deployment to a database to upload the java file instead of class
    The files for the java class you created in JDev project is located in the myworks folder in jdev, eg, <jdev_home>\jdev\mywork\Application1\Project1\src\project1
    Hope this helps,
    Sunil..

  • Do we have INCLUDE=JAVA CLASS AND INCLUDE=JAVA SOURCE OPTION IN DATA PUMP

    Hi All ,
    Can any one please tell me ,
    how to import only java class and java source from datapump export file.
    Include=java class or include=java source is not working ..
    Thanks in advance,
    Sanjeev.

    Duplicate post.
    datapump include option.
    Lock this thread

  • Call Java Class and Methods from ABAP

    Hi
    I install de JCo, But how i can call java class and methods from ABAP?. somebody has an example?
    The tutorial say that's is possible,  but don't explain how do that?
    Thanks.
    Angel G. Hurtado

    If you need only simple java program, you do not need to install JCo. The following codes can call java class.
    DATA: COMMAND TYPE STRING VALUE 'C:\j2sdk1.4.2_08\bin\java',
          DIR TYPE STRING VALUE D:\eclipse\workspace',
          PARAMETER TYPE STRING VALUE 'Helloworld'. "here the name of your java program
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE
       EXPORTING
         APPLICATION = COMMAND
         PARAMETER = PARAMETER
         DEFAULT_DIRECTORY = DIR
       MAXIMIZED =
         MINIMIZED = 'X'     "If you need the DOS window to be minimized
      EXCEPTIONS
        CNTL_ERROR = 1
        ERROR_NO_GUI = 2
        BAD_PARAMETER = 3
        FILE_NOT_FOUND = 4
        PATH_NOT_FOUND = 5
        FILE_EXTENSION_UNKNOWN = 6
        ERROR_EXECUTE_FAILED = 7
        OTHERS = 8.
    Tell me if it works.
    Nuno.

  • How to use line wrapping and line spacing in java?how to use line wrapping

    how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?

    Hello,
    You should post your question at:
    http://java.forum.sun.com
    Thanks
    - Rose

  • How to use line wrapping and line spacing in java?

    how to use line wrapping and line spacing in java?

    Hi,
    This is explained in the java Tutorial. Please see the link:
    http://java.sun.com/docs/books/tutorial/i18n/text/line.html
    and find some sample examples.
    Hope this helps,
    --Sun/DTS                                                                                                                                                                                                                                                                                                                                                                                       

Maybe you are looking for

  • Cash Advance Discover IT

    I saw in another post that cash advances from Discover are not recorded as a cash advance on your Discover account, rather listed as just a purchase. Anyone know if this is true? I use my Wells Fargo Visa as my overdraft protection for my checking ac

  • MYSAPSSO2 issue ...

    Hello all, Maybe you've a good idea ... We're using an ITS 6.20 with PAS module for SSO; in parallel we have an ABAP webdynpro application which uses SSO via a JAVA Engine ( SPNego with Ticket ); when calling the ITS application, we get an MYSAPSSO2

  • Corrupt thumbnails in PSE9 Organizer

    Hi All, I have been putting up with an issue for the last few weeks but I've had enough.  For some reason the thumbnails of images I have edited in elements appear as a default thumbnail of a photo torn in half in the organiser.  The information rele

  • No "Keep Appointments" field in my calendar options

    I am trying to limit the number of past appointments my calendar keeps on my 8100; the user guide and all support docs say to set this in the Calendar menu>Options>Keep Appointments field, but there is no Keep Appointments field in my Options menu. C

  • Cannot enter correct values in the SQL server express 2012 using labview

    Hi everyone, I am trying to enter the 2D array I get from DAQ into the SQL server express 2012 using Labview. When I am run the software, it connects with the table but doesn't enter the correct values. It only enters the "0" for each column of the t