Struts Tutorial Home

Struts Home

JSP Resources

Community

Site

Writing the Struts Business Component

text zoom


Now let us write our business class that has the ability to perform several actions on the user like adding a new user to the database,updating an existing user,deleting a user or fetching a user from the database. Let us call this class a UserManager.


package com.visualbuilder.struts.db;

public class UserManager {
         
private static UserManager mgr = null;

         
private UserManager()
        
 {
         
 }
         
public static UserManager getInstance()
         
{
                   
if(mgr == null)
                   
{
                             
mgr = new UserManager();
                     }
                    
return mgr;
          
 }
}


Because for one application,there can exist only a single UserManager,we have made the UserManager a singleton. We will use UserManager.getInstance() whenever we need an instance of UserManager.


Connecting to a database
The purpose of the UserManager is to provide an interface between the database and the client applications that need access to the database.  Let’s connect to the database and add the following code in the constructor.


String driverClass = ResourceManager.getString("database.driver");


String dbUrl = ResourceManager.getString("database.url");


String dbUser = ResourceManager.getString("database.user");


String dbPassword = ResourceManager.getString("database.password");
try{
            
Class.forName(driverClass);
            
con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
}catch(Exception exp){
          
System.err.println("Could not connect to dtabase.\n" exp.getMessage());
}


 
Where con is a global variable of type java.sql.Connection. 
Now that we have connected to the database,let us add the necessary methods to manipulate the users. After adding these methods,UserManager


package com.visualbuilder.struts.db;


import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;


import com.visualbuilder.struts.ResourceManager;
import com.visualbuilder.struts.beans.User;


public class UserManager {
        private static UserManager mgr = null;
        private Connection con = null;
       
        private UserManager()
        {
                String driverClass = ResourceManager.getString("database.driver");
                String dbUrl = ResourceManager.getString("database.url");
                String dbUser = ResourceManager.getString("database.user");
                String dbPassword = ResourceManager.getString("database.password");
                try{
                        Class.forName(driverClass);
                        con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
                }catch(Exception exp){
                        System.err.println("Could not connect to dtabase.\n" exp.getMessage());
                }
        }
        public void saveUser(User user) throws SQLException
        {
                if(user == null)
                        throw new SQLException(ResourceManager.getString("save.user.null"));
                Connection connection = getConnection();
               
                PreparedStatement pstmt = connection.prepareStatement("insert into users(user_id,first_name,last_name,age,email) values(?,?,?,?,?)");
                pstmt.setString(1,user.getUserId());
                pstmt.setString(2,user.getFirstName());
                pstmt.setString(3,user.getLastName());
                pstmt.setInt(4,user.getAge());
                pstmt.setString(5,user.getEmail());
               
                pstmt.executeUpdate();
               
                pstmt.close();
        }      
        public User getUser(String userId) throws SQLException
        {
                if(userId == null || userId.length() == 0)
                        throw new SQLException(ResourceManager.getString("retrieve.user.null"));
                Connection connection = getConnection();
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("select * from users where user_id='" userId "'");
                User user = null;
                if(rs.next())
                {                      
                        user = new User(userId);
                        user.setFirstName(rs.getString("first_name"));
                        user.setLastName(rs.getString("last_name"));
                        user.setAge(rs.getInt("age"));
                        user.setEmail(rs.getString("email"));
                }
                rs.close();
                stmt.close();
                return user;
        }
        public List list() throws SQLException
        {
                Connection connection = getConnection();
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("select * from users");
                User user = null;
                List list = new ArrayList();
                while(rs.next())
                {                      
                        user = new User(rs.getString("user_id"));
                        user.setFirstName(rs.getString("first_name"));
                        user.setLastName(rs.getString("last_name"));
                        user.setAge(rs.getInt("age"));
                        user.setEmail(rs.getString("email"));
                        list.add(user);
                }
                rs.close();
                stmt.close();
                return list;
        }
        private Connection getConnection() throws SQLException
        {
                if(con == null)
                        throw new SQLException(ResourceManager.getString("database.notConnected"));
                return con;
        }
        public static UserManager getInstance()
        {
                if(mgr == null)
                {
                        mgr = new UserManager();
                }
                return mgr;
        }
        public void finalize()
        {
                try{
                        con.close();                   
                }catch(Exception exp){}
        }
}


We have used several properties in this class. Add these properties in the ApplicationResources.properties file.


#database connection properties
database.driver = sun.jdbc.odbc.JdbcOdbcDriver
database.url = jdbc:odbc:users
database.user = ""
database.password = ""


#User operations messages

save.user.null = User is null
retrieve.user.null = User is null
user.notFound = User not found
database.notConnected = Not connected to a database


Now we have all the business functions available,we are ready to move to the formal development of the struts based application.

                    

Copyright © 2013 VisualBuilder. All rights reserved