Friday, 30 November 2012
JSF SelectManyCheckBox Tag example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version='1.0' encoding='UTF-8'?>
package com.satyamsoft;
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
Monday, 19 November 2012
How to Insert and Retrieve data from database using JSF
1) How to Insert and Retrieve data from database using JSF example structure
Table Structure :
DROP TABLE IF EXISTS customer;
CREATE TABLE customer (id number(5) NOT NULL, name VARCHAR(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>Insert and Retrieve data from database using JSF</title>
</head>
<body>
<f:view>
<h:form enctype="multipart/form-data">
<table bgcolor="orange" border="0">
<tr>
<td>
<b><h:outputText value="ID:"></h:outputText></b>
</td>
<td>
<h:inputText id="id" value="#{bean.id}">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText>
</td>
<td>
<h:messages style="color: red;"></h:messages>
</td>
</tr>
<tr>
<td>
<b><h:outputText value="NAME:"></h:outputText></b>
</td>
<td>
<h:inputText id="name" value="#{bean.name}">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText>
</td>
<td>
<h:messages style="color: red;"></h:messages>
</td>
</tr>
<tr>
<td>
<b><h:outputText value="City:"></h:outputText></b>
</td>
<td>
<h:selectOneMenu id="city" value="#{bean.city }" title="Select City" >
<f:selectItem itemLabel="- Select City -"/>
<f:selectItem itemLabel="Vijayadada" itemValue="vijayawada"/>
<f:selectItem itemLabel="Hyderabad" itemValue="Hyderabad"/>
<f:selectItem itemLabel="Bhadrak" itemValue="Bhadrak"/>
<f:selectItem itemLabel="Guntur" itemValue="Guntur"/>
</h:selectOneMenu>
</td>
<td>
<h:messages style="color: red;"></h:messages>
</td>
</tr>
<tr>
<td></td>
<td>
<h:commandButton action="#{customer.insert}" value="Insert"></h:commandButton>
</td>
</tr>
</table>
<h:graphicImage value="images/wheel.PNG" width="150" height="100"/>
<br>
<h:dataTable id="dt" value="#{datatable.alldata}" var="datatable" bgcolor="green" 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="#{datatable.id}"/></b>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<b><h:outputText value="#{datatable.name}"/></b>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="City" />
</f:facet>
<b><h:outputText value="#{datatable.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>bean</managed-bean-name>
<managed-bean-class>com.satyamsoft.Bean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>customer</managed-bean-name>
<managed-bean-class>com.satyamsoft.customer</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-action>#{customer.insert}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/fail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>datatable</managed-bean-name>
<managed-bean-class>com.satyamsoft.DataTable</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). customer.java
package com.satyamsoft;
import java.sql.*;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
public class customer {
public String insert() throws Exception
{
HttpSession session =(HttpSession)FacesContext.
getCurrentInstance().getExternalContext().getSession(true);
Bean b=(Bean)session.getAttribute("bean");
Connection con=DBConnect.getConnection();
String sql="insert into customer values(?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1,b.getId());
ps.setString(2,b.getName());
ps.setString(3,b.getCity());
if(ps.executeUpdate()==1)
return "success";
else
return "fail";
}
}
9). DataTable.java
package com.satyamsoft;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DataTable {
Connection con=null;
Statement st=null;
ResultSet rs=null;
public List<Bean> getAlldata() throws Exception
{
List<Bean> alldata=new ArrayList<Bean>();
con=DBConnect.getConnection();
try {
st=con.createStatement();
String sql="select * from customer";
rs=st.executeQuery(sql);
while(rs.next())
{
Bean b=new Bean();
b.setId(rs.getInt("id"));
b.setName(rs.getString("name"));
b.setCity(rs.getString("city"));
alldata.add(b);
}
} catch (Exception e) {
e.printStackTrace();
}
return alldata;
}
}
10).Table Data:
11).OutPut:
Subscribe to:
Posts (Atom)