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;
}
}
error on following line
ReplyDeleteConnection con=DBConnect.getConnection();
non static method getConnection(); cannot be referenced from a static context