1) Retrieve data from database into table using JSF example structure
Table Structure :
DROP TABLE IF EXISTS customer;
CREATE TABLE customer (id number(5) NOT NULL, nameVARCHAR(10) NOT NULL,city VARCHAR(10) NOT NULL,PRIMARY KEY (id));
2).index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="Refresh" content="0; URL=index.faces"/>
</head>
</html>
3). index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Data from database into table using JSF</title>
</head>
<body>
<f:view>
<h:form enctype="multipart/form-data">
<br>
<h:dataTable id="dt1" value="#{dataAction.allData}" var="data" bgcolor="pink" border="1"
cellpadding="8" width="30%" styleClass="order-table" headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header"><h:outputText value="Id" />
</f:facet>
<b><h:outputText value="#{data.id}"/></b>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<b><h:outputText value="#{data.name}"/></b>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="City" />
</f:facet>
<b><h:outputText value="#{data.city}"/></b>
</h:column>
</h:dataTable><br>
</h:form>
</f:view>
</body>
</html>
4). web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
5). faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>dataAction</managed-bean-name>
<managed-bean-class>com.satyamsoft.DataAction
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
6). Bean.java
package com.satyamsoft;
import java.util.List;
public class Bean {
public int id;
public String name;
public String city;
public List<?> allData;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public List<?> getAllData() {
return allData;
}
public void setAllData(List<?> allData) {
this.allData = allData;
}
}
7). DBConnect.java
package com.satyamsoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect {
Connection con = null;
public Connection getConnection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","123456");
}catch (ClassNotFoundException e){
System.err.println("ClassNotFoundException in getConnection," + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException in getConnection, " + e.getMessage());
}
return con;
}
public void setConnectionClose() throws SQLException
{
con.close();
}
}
8). DataAction.java
package com.satyamsoft;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.satyamsoft.DBConnect;
import com.satyamsoft.Bean;
public class DataAction {
Connection con = null;
Statement stmt = null;
ResultSet rs;
public List<Bean> getAllData(){
List<Bean> tabledata = new ArrayList<Bean>();
DBConnect lc = new DBConnect();
con = lc.getConnection();
try {
stmt=con.createStatement();
String strSql="select * from customer";
rs=stmt.executeQuery(strSql);
while(rs.next()){
Bean bean = new Bean();
bean.setId(rs.getInt("id"));
bean.setName(rs.getString("name"));
bean.setCity(rs.getString("city"));
tabledata.add(bean);
}
} catch (SQLException e) {
System.out.println("Exception in getAllImage::"+e.getMessage());
}
return tabledata;
}
}
9).Table Data:
10).Output:
I use microsoft sql server. How to retrieve. pls help me!
ReplyDelete