Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
180
JSF Webgrid working with ResultSetDataModel
posted

Hi,

I want to use a java.sql.ResultSet as datasource to my webgrid. I see that there is a ResultSetDataModel provided for use. I tried creating one using the result set I have but did not  get any success.

Can anyone please provide me a sample code to use the resultset as a data source. Thanks in advance.

  • 1324
    posted in reply to Mangru
  • 1324
    Suggested Answer
    posted

    Hello Leo....

    I am trying to copy the sample code for you but I am  not able to see the updates..retrying in parts...

    DAO.java
    ----------------------------------

    package MyPackage.Test;

    import javax.faces.model.ResultSetDataModel;
    import java.sql.*;

    public class DAO {
     
     private static ResultSetDataModel rs = null;
     private static Connection conn= null;
     private static ResultSet result =null;

     public static ResultSetDataModel getEmployees()
     {
       try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Creating Connection");
      conn = DriverManager.getConnection("jdbc:odbc:MyTable");
           if(conn!=null)
           {
     
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      System.out.println("Using ResultSet");
      result = stmt.executeQuery("select * from Table1");
      rs = new ResultSetDataModel(result);
      System.out.println("Accessing Data");
     
            }
            else
           {
      System.out.println("Error");
        }
           }
            catch(Exception e){
      e.printStackTrace();
      }
      return rs;
      }
    }

  • 1324
    posted

    Hello Leo...

    Here is the sample page with managed beans for you:

    DAO.java
    ----------------------------------

    package MyPackage.Test;

    import javax.faces.model.ResultSetDataModel;
    import java.sql.*;

    public class DAO {
     
     private static ResultSetDataModel rs = null;
     private static Connection conn= null;
     private static ResultSet result =null;

     public static ResultSetDataModel getEmployees()
     {
       try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Creating Connection");
      conn = DriverManager.getConnection("jdbc:odbc:MyTable");
           if(conn!=null)
           {
      
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      System.out.println("Using ResultSet");
      result = stmt.executeQuery("select * from Table1");
      rs = new ResultSetDataModel(result);
      System.out.println("Accessing Data");
      
            }
            else
           {
      System.out.println("Error");
        }
           }
            catch(Exception e){
      e.printStackTrace();
      }
      return rs;
      }
    }

    DataTest.java
    ------------------------------------

    package MyPackage.Test;

    import javax.faces.model.ResultSetDataModel;

    public class DataTest {

     protected ResultSetDataModel rs =null;

     public ResultSetDataModel getEmployees() {

      rs= DAO.getEmployees();
      return rs;
     }

    }

    index.jsp
    ----------------------------------

    <%@ page contentType="text/html" language="java"%>
    <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
    <%@ taglib prefix="ig" uri="http://es.infragistics.com/faces/netadvantage"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Infragistics NetAdvantage For Java Server Faces - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="description"
        content="Infragistics NetAdvantage for Java Server Faces - Demo">
    <link href="../../resources/default.css" rel="stylesheet"
        type="text/css">
    </head>
    <body class="rightPanel">
    <f:view>
        <h:form>
            <h:panelGroup styleClass="main">
                <%-- TITLE --%>
                <h:outputText styleClass="title"
                    value="WebGrid Sample" />
                <h:panelGroup styleClass="section">

    <ig:gridView
        dataSource="#{Test.employees}"
        pageSize="10">
        <f:facet name="header">
            <h:outputText value="Employee List" />
        </f:facet>
        <ig:column>
            <f:facet name="header">
                <h:outputText value="First Name" />
            </f:facet>
            <h:outputText value="#{DATA_ROW.Name}" />
        </ig:column>
      
        <ig:column>
            <f:facet name="header">
                <h:outputText value="Phone Number" />
            </f:facet>
            <h:outputText value="#{DATA_ROW.Phone}" />
        </ig:column>
    </ig:gridView>

                </h:panelGroup>
              </h:panelGroup>
        </h:form>
    </f:view>
    </body>
    </html>

    faces-config.xml
    ---------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
                                  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

    <faces-config>
    <managed-bean>
        <managed-bean-name>Test</managed-bean-name>
        <managed-bean-class>MyPackage.Test.DataTest</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>

    </faces-config>

    Hope it helps you.

    Roshan

  • 1324
    posted

    Hello Leo...

    Here is the solution for you.

    DAO.java
    ---------------------------------------------------------------
    package MyPackage.Test;

    import javax.faces.model.ResultSetDataModel;
    import java.sql.*;

    public class DAO {
     
     private static ResultSetDataModel rs = null;
     private static Connection conn= null;
     private static ResultSet result =null;

     public static ResultSetDataModel getEmployees()
     {
       try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Creating Connection");
      conn = DriverManager.getConnection("jdbc:odbc:MyTable");
           if(conn!=null)
           {
      
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      System.out.println("Using ResultSet");
      result = stmt.executeQuery("select * from Table1");
      rs = new ResultSetDataModel(result);
      System.out.println("Accessing Data");
      
            }
            else
           {
      System.out.println("Error");
        }
           }
            catch(Exception e){
      e.printStackTrace();
      }
      return rs;
      }
    }

    DataTest.java
    ------------------------------------------------------------------------
    package MyPackage.Test;

    import javax.faces.model.ResultSetDataModel;

    public class DataTest {

     protected ResultSetDataModel rs =null;

     public ResultSetDataModel getEmployees() {

      rs= DAO.getEmployees();
      return rs;
     }

    }

    faces-config.xml
    -----------------------------------------
    <faces-config>
    <managed-bean>
        <managed-bean-name>Test</managed-bean-name>
        <managed-bean-class>MyPackage.Test.DAO.</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>
    ------------------------------------------

    index.jsp
    --------------------------------------------
    <%@ page contentType="text/html" language="java"%>
    <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
    <%@ taglib prefix="ig" uri="http://es.infragistics.com/faces/netadvantage"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Infragistics NetAdvantage For Java Server Faces - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="description"
        content="Infragistics NetAdvantage for Java Server Faces - Demo">
    <link href="../../resources/default.css" rel="stylesheet"
        type="text/css">
    </head>
    <body class="rightPanel">
    <f:view>
        <h:form>
            <h:panelGroup styleClass="main">
                <%-- TITLE --%>
                <h:outputText styleClass="title"
                    value="WebGrid - Sample" />
                <h:panelGroup styleClass="section">

    <ig:gridView
        dataSource="#{Test.employees}"
        pageSize="10">
        <f:facet name="header">
            <h:outputText value="Employee List" />
        </f:facet>
        <ig:column>
            <f:facet name="header">
                <h:outputText value="First Name" />
            </f:facet>
            <h:outputText value="#{DATA_ROW.Name}" />
        </ig:column>
      
        <ig:column>
            <f:facet name="header">
                <h:outputText value="Phone Number" />
            </f:facet>
            <h:outputText value="#{DATA_ROW.Phone}" />
        </ig:column>
    </ig:gridView>

                </h:panelGroup>
              </h:panelGroup>
        </h:form>
    </f:view>
    </body>
    </html>
    ------------------------------------------
    Hope it helps you...

    Roshan