In the bean put all the relevant commands in order to open the connection
with the database. Lets say that you have a method called

public void openConnection()
{
    try
    {
        // Load the database's drivers.
        Class.forName("database's drivers here");

        // Obtain connection with the database.
        Connection dbcon = DriverManager.getConnection("databse address
here", "username here", "password here");

        // create new statemtn here.
        Statement stmt = dbcon.createStatement();

        String select = "SELECT * FROM <table name here>";

        // get the whole result set
        ResultSet rs = stmt.executeQuery(retrieve);

        // go through all rows
        while (rs.next())
        {
             System.out.println(rs.toString());
        }

    }       // end try
    // If driver not found.
    catch (ClassNotFoundException cnfe)
    {
        System.out.println("Driver not found. " + cnfe);
    }
    catch (SQLException sqle)
    {
          System.out.println("Connection to the database could not be established.
" + sqle);
    }
}

Then define a bean in the jsp page using the <jsp:useBean> tag.

  Call the openConnection using the beans reference name (lets say that the
reference to the bean is called myBean) myBean.openConnection();
this will open the connection with the database and will display all the
data in the specified table.

  You can have two diffreent methods if you want. One to open the
copnnection and the other to get the data. You can call them with the same
way. Do not forget to close the connection with the databse after you
finish. Use the close() method in order to do it.

  Hope this helps.




>From: Deepak Kumar <[EMAIL PROTECTED]>
>Reply-To: A mailing list about Java Server Pages specification and
>     reference <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Executing SQL query in bean
>Date: Thu, 4 Jan 2001 02:48:54 -0800
>
>Hi Friends,
>
>I want to write the code of JDBC connectivity in a
>bean and then use the resultset in JSP page how can i
>accomplish this.
>
>Thanks in advance.
>Deepak Kumar
>
>__________________________________________________
>Do You Yahoo!?
>Yahoo! Photos - Share your holiday photos online!
>http://photos.yahoo.com/
>
>===========================================================================
>To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
>JSP-INTEREST".
>For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
>DIGEST".
>Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to