Home Jsf Jsftraining Working with Database through JDBC

Related Tutorials


Share on Google+Share on Google+

Working with Database through JDBC

You can use JDBC to work with database. For this you can write JDBC code in the action method of the managed bean. Let's understand through an example.

Working with Database through JDBC


You can use JDBC to work with database. For this you can write JDBC code in the action method of the managed bean. Let's understand through an example.

The code of jdbc.jsp

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<title>Login Page</title>
<h:form id="loginForm">
<h:outputText value="User Name or Password is incorrect." rendered="#{JdbcBean.error}" style="color:red"/>

<h:panelGrid columns="3" style="font-weight:bold;">
<h:outputText value="User Name"/>
<h:inputText id="uname" required="true" value="#{JdbcBean.username}"/>
<h:message for="uname" style="color:red"/>

<h:outputText value="Password" />
<h:inputSecret id="pwd" required="true" value="#{JdbcBean.password}"/> 
<h:message for="pwd" style="color:red"/>

<h:outputText value="" />
<h:commandButton value="Submit" action="#{JdbcBean.checkUserInDatabase}"/>


Here the managed bean name is JdbcBean and checkUserInDatabase is the action method. Now the action method will contain the jdbc code to check whether the user name and password is authentic.

Here is the code of JdbcBean.java

package roseindia;

import java.sql.*;

public class JdbcBean {
public JdbcBean(){}

String username;
String password;

boolean error; 

public String getUsername() {
   return username;
public void setUsername(String username) {
   this.username = username;

public String getPassword() {
   return password;
public void setPassword(String password) {
   this.password = password;

public boolean isError() {
   return error;
public void setError(boolean error) {
   this.error = error;

public String checkUserInDatabase(){
   String returnString = "failure";

  Connection con = null;
  Statement st = null;
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsfexamples","root","root");
   st = con.createStatement();
   ResultSet rs = st.executeQuery("select * from jsftable");

   if(username.equals(rs.getString(2)) && password.equals(rs.getString(2))){
   returnString = "success";
   error = true;
  catch(Exception ex){
  return returnString;

Download code for all examples


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 30, 2008

Related Tutorials

Discuss: Working with Database through JDBC   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Demi Chua
November 27, 2012
My Comment

Can you have JSF(Managed Bean) to EJB to PostgreSQL