Nearby lessons
14 of 35Spring - JDBC / DAO
- Understand Spring - JDBC / DAO
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring - JDBC / DAO step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
Provide an implementation to DAO interface
public class StudentDAOImpl implements StudentDao{ ---implementation for all StudentDao interface methods----- ---> We can provide our methods inorder to improve code reusability along with DAO interface methods--- }
Prepare DTOs as per the requirement
public class Student{ private String sid; private String sname; private String saddr; ----- ----- setXXX() and getXXX() ----- ----- }
Creat Factory Methods/ Factory Classes to generate DAOs
public class StudentDaoFactory{ private static Student Dao dao; static{ dao = new StudentDaoImpl(); } public static StudentDao getStudentDao(){ return dao; } }
In Service layer StudentDao dao = StudentDao.getStudentDao();
5) We must not cache DAO references, because, Factory classes/ Factory methods are providing single instances of DAO to the service layer, if DAO is required in multiple modules then it is requyired to create more than one DAO reference.
6) In case of DAOs, It is suggestible to interact with Databases by using Connection Pooling mechanisms, not by using DriverManager approach. 7) DAO is not threadsafe, we must not use DAOs in multi threadded environment.
8) In DAOs we can access close() method inorder to close the resources like connections,.... , so here, before calling close() method we must ensure that whether the resources are going to be released or not with our close() method call.
9) We have make sure that all the objects which are used by DAOs are following Java bean conventions or not.
htmls
a)addform.html b)searchform.html c)deleteform.html d)existed.html e)notexisted.html f)success.html g)failure.html h)layout.html i)header.html j)menu.html k)welcome.html l)footer.html
- jsps a)display.jsp
- Servlets a)ControllerServlet
- Services:
a)StudentService
- DAOs a)StudentDao
6)DTOs a)StudentTo
7)Factories a)ConnectionFactory b)StudentServiceFactory c)StudentDaoFactory
JARS
a)ojdbc6.jar
Design Patterns — layout.html
a)DAO b)MVC c)DTO d)Factory
Example:
Design Patterns — header.html
Design Patterns — footer.html
Design Patterns — menu.html
Durga Software Solutions, 202, Mitrivanam, Ameerpet, Hyd-38 </b> </font> </center> </body> </html>
Design Patterns — welcome.html
Design Patterns — addform.html
Welcome To Durga Software Solutions </marquee> </b> </font> </center> </body> </html>
Design Patterns — searchform.html
Design Patterns — deleteform.html
Design Patterns — display.jsp
Design Patterns — existed.html
Design Patterns — notexisted.html
Design Patterns — success.html
Design Patterns — failure.html
Design Patterns — ControllerServlet.java
Design Patterns — StudentService.java
Design Patterns — StudentServiceImpl.java
Design Patterns — StudentDao.java
Design Patterns — StudentDaoImpl.java
Design Patterns — StudentTo.java
Design Patterns — StudentServiceFactory.java
Design Patterns — StudentDaoFactory.java
Design Patterns — ConnectionFactory.java
Design Patterns
To provide support for DAOs kind of implementations in Spring Applications, Spring has provided a separate module called as ―Spring DAO‖. Spring DAO modules has provided a set of predefined classes and interfaces in order to provide DAO support in the form of ―org.springframework.dao‖ package.
Spring provides a convenient translation from technology-specific exceptions like SQLException , HibernateException,…… to its own exception class hierarchy with the DataAccessException as the root exception. Spring JDBC
In Enterprise Applications, to prepare Data Access Layer or DAO layer Spring has provided Modules in the form of JDBC and ORM . IN ORM we may use no of ORM implementation tools like Hibernate, JPA, Ibatis,....
Q)In Enterprise Applications, to prepare Data Access Layer we have already Plain JDBC tech. then what is the requirement to go for Spring JDBC Module?
Ans — StudentDao.java
- To prepare Data Access Layer in enterprise applications, if we use JDBC then we must take explicit responsibility to prepare the steps load and register the driver, Establisg Connection, creating Statement, executing SQl Queries and closing the resources like ResultSet, Statement and Connection.
If we use Spring JDBC module to prepare Data Access Layer, we must take explicit responsibility to write and execute SQL Queries only, not to take any responsibility to load and register driver, connection establishment, creating Statement and closing the resources.
- In case of Plain JDBC, almost all the exceptions are checked exceptions, we have to handle them explicitly by providing some java code.
In case of Spring JDBC module, all the internal checked exceptions are converted into Unchecked Exceptions which are defined by Spring DAO module , it is very simple to handle these unchecked Exceptions.
- In Plain JDBC, limited support is available for Transactions.
In Spring JDBC Module, very good support is available for transactions, we may use Transaction module also to provide transactions.
- In Plain JDBC, to hold the results we are able to use only ResultSet object, which is not implementing java.io.Serializable interface, which is not transferable in network.
In Spring JDBC, we are able to get results of SQL Queries in our required form like in the form of RowSet, Collections, ..... which are implementing java.io.Serializable interface and which are transferable in Network.
- In plain JDBC, we are able to get Connections either by using DriverManager or by using Datasource.
In Spring JDBC, we are able to get Connection internally by using Datasource only, that is through Connection Pooling only.
- In plain JDBC, to map records to Bean objects in the form of Collection Object we have to write java code explicitly, no predefined support is provided by JDBC tech.
In case of Spring JDBC, to map Database records to Bean objects in the form of Collection Spring JDBC has provided predefined support in the form of "RowMapper".
- In Plain JDBC, no callback interfaces support is available to create and execute the sql queries in PrfeparedStatement style.
In Spring JDBC, callback interfaces support is available to create and execute sql queries in PreparedStatement style.
To prepare Data Access Layer in enterprise applications, Spring JDBC module has provided the complete predefined library in the from of the following classes and interfaces in "org.springframework.jdbc" and its sub packages.
JdbcTemplate NamedParameterJdbcTemplate SimpleJdbcTemplate SimpleJdbcInsert and SimpleJdbcCall SQL Mapping through SQLUpdate and SQLInsert
In Spring Applications, if we want to JdbcTemplate[Jdbc Module] then we have to use the following steps.
1)Create DAO interface with the required methods. 2)Create DAO implementation class with implementation for DAO interface methods. 3)In Configuration File provide configuration for DataSource class , JdbcTemplate class and DAO implementation class. 4)Prepare Test Application to access Dao methods.
IN Spring configuration file we have to configure DataSource with the following properties.
driverClassName url username password In Spring applications, to configure DataSource Spring has provided a seperate a predefined Datasource class in the form of "org.springframework.jdbc.datasource.DriverManagerDataSource", it is not suggestible for production mode, it is suggestible for testing mode of our applications , In spring applications, it is always suggestible to use third party Connection Pooling mechanisms like dbcp, C3P0, Proxool,..
JdbcTemplate class is providing basic environment to interact with Database like Loading Driver class, Getting Connection between Java application and DB, Creating Statement , PreparedStatement and CallableStatement and closing the connection with the help of the provided Datasource and JdbcTemplate class has provided the following methods to execute SQL Queries. 1)For Non Select sql queries and DML SQL queries
public int update(String query) 2)For DDL Sql Queries
public void execute(String query) 3)For Select sql queries
public int queryForInt(String query) public int queryForLong(String query) public String queryForString(String query) public Object queryForObject(String query) public List query(String query) public List queryForList(String query) public Map queryForMap(String query) public RowSet queryForRowSet(String query)
While performing retrival operations to convert data from ResultSet object[records] to Bean objects Spring Framework has provided a predefined interface in the form of "org.springframework.jdbc.core.RowMapper" which contains the following method .
public Object mapRow(ResultSet rs, int rowCount)
Example
Ans — StudentDaoImpl.java
Ans — Student.java
Ans — StudentMapper.java
Ans — Test.java
Ans — StudentDao.java
In Spring JDBC Applications, we will use positional parameters[?] also in sql queries which we are providing along with JdbcTemplate class provided query execution methods.
If we provide positional parameters in sql queries then JdbcTemplate class will use "PreparedStatement" internally to execute sql query instead of Statement.
To provide values to the Positional parameters in SQL Queries we have to use Object[] with values as parametyer to all JdbcTemplate class provided query execution methods.
public int update(String query, Object[] param_Values) public int queryForInt(String query, Object[] param_Values) public long queryForLong(String query, Object[] param_Values) public Object queryForObject(String query, Object[] param_Values, RowMapper rm) ----- ----- -----
Ex:
String query = "insert into student values(?,?,?)"; int rowCount = jdbcTemplate.update(query, new Object[]{"S-111", "AAA", "Hyd"});
Example:
Ans — StudentDaoImpl.java
Ans — applicationContext.xml
Ans — StudentMapper.java
Ans — Test.java
Ans — CustomerDao.java
NamedParameterJdbcTemplate class is same as JdbcTemplate class , but, NamedParameterJdbcTemplate class is able to define and run sql queries with Named Parameters instead of positional parameters.
EX:
String query = "insert into student values(:sid, :sname, :saddr)";
Where :sid, :sname, :saddr are named parameters for which we have to provide values.
In case of NamedParameterJdbcTemplate , we are able to provide values to the named parameters in the following two approaches.
- By Using Map directly.
- By using SqlParameterSource interface.
- By Using Map directly.
String query = "insert into student values(:sid, :sname, :saddr)"; Map map = new HashMap(); map.put("sid", "S-111"); map.put("sname", "AAA"); map.put("saddr", "Hyd"); namedParameterJdbcTemplate.update(query, map);
- By using SqlParameterSource interface.
To provide values to the Named parameters Spring has provided the following two implementation classes for SqlParameterSoure interface.
a)MapSqlParameterSource b)BeanPropertySqlParameterSource
To provide values to the named parameters if we want to use MapSqlParameterSource then first we have to create object for MapSqlParameterSource and we have to use the following method to add values to the named parameters.
public MapSqlParameterSource addValue(String name, Object val)
EX:
String query = "insert into student values(:sid, :sname, :saddr)"; SqlParameterSource param_Source = new MapSqlParameterSource("sid", "S-111"); param_Source = param_Source.addValue("sname", "AAA"); param_Source = param_Source.addValue("saddr", "Hyd"); namedParameterJdbcTemplate.update(query, param_Source);
To provide values to the named parameters if we want to use BeanPropertySqlParameterSource then first we have to create bean object with data then we have to create Object for BeanPropertySqlParameterSource with the generated Bean reference then provide BeanPropertySqlParameterSource object to query methods.
EX:
String query = "insert into student values(:sid, :sname, :saddr)"; Student std = new Student(); std.setSid("S-111"); std.setSname("AAA"); std.setSaddr("Hyd"); SqlParameterSource param_Source = new BeanPropertySqlParameterSource(std ); namedParameterJdbcTemplate.update(query, param_Source);
Note: JdbcTemplate is allowing DataSource object injection through setter method, but, NamedParameterJdbcTemplate class is allowing DataSource object injection through Constructor Dependency Injection.
Example
Ans — CustomerDaoImpl.java
Ans — Customer.java
Ans — CustomerMapper.java
Ans — applicationContext.xml
Ans — Test.java
SimpleJdbcTemplate — EmployeeDao.java
In Spring JDBC module, the main intention of SimpleJdbcTemplate class is to provide support for JDK5.0 version feratures like Auto Boxing, Auto Unboxing, Var-Arg methods,.....
SimpleJdbcTemplate class was provided in Spring2.5 version only and it was deprecated in the later versions Spring3.x and Spring4.x , in Spring5.x version SimpleJdbcTemplate class was removed.
If we want to use SimpleJdbcTemplate class we have to use Spring2.5 version jar files in Spring applications.
To execute SQL queries , SimpleJdbcTemplate class has provided the following methods.
public Object execute(String sqlQuery) Note: To use this method we have to get JdbcOperations class by using getJdbcOperations() method.
public int update(String query, Object ... params) public Object queryForInt(String query, Object ... params) public Object queryForLong(String query, Object ... params) public Object query(String query, Object ... params) public Object queryForObject(String query,Object ... params) ---- ---- Note: In case of SimpleJdbcTemplate class, to perform retrival operations, we have to use "ParameterizedRowMapper" inplace of RowMapper interface.
Example:
SimpleJdbcTemplate — EmployeeDaoImpl.java
SimpleJdbcTemplate — Employee.java
SimpleJdbcTemplate — EmployeeMapper.java
SimpleJdbcTemplate — applicationContext.xml
SimpleJdbcTemplate — Test.java
DAO Support Classes
In Spring JDBC , we have to prepare DAO implementation classes with XXXTemplate property and the corresponding setXXX() method inorder to inject XXXTemplate class.
In Spring JDBC applications, if we want to get XXXTemplate classes with out declaring Template properties and corresponding setXXX() methods we have to use DAO Support classes provided Spring JDBC module.
There are three types of DAOSupport classes inorder to get Template object in DAO classes.
- JdbcDaoSupport
- NamedParameterJdbcDaoSupport
- SimpleJdbcDaoSupport
Where JdbcDaoSupport class will provide JdbcTemplate reference in DAO classes by using the following method.
public JdbcTemplate getJdbcTemplate()
Where NamesParameterJdbcDaoSupport class will provide NamedParameterJdbcTempate reference in DAO classes by using the following method.
public NamedParameterJdbcTemplate getNamedparameterJdbctemplate() Where SimpleJdbcDaoSupport class is able to provide SimpleJdbctemplate reference in Dao class by using the following method.
public SimpleJdbcTemplate getSimpleJdbcTemplate()
EX:
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao{
public String insert(int eno, String ename, float esal, String eaddr){ getJdbcTemplate().update("insert into emp1 values("+eno+",'"+ename+"',"+esal+",'"+eaddr+"')"); return "SUCCESS"; } ---- ---- }
Batch Processing — Employee.java
To perform Batch Updations in Spring JDBC we have to use the following method from JdbcTemplate class.
public int[] batchUpdate(String sql_prepared_Statement, BatchPreparedStatementSetter setter)
Where BatchPreparedStatementSetter interface contains the following two methods public void setValues(PreparedStatement ps, int index) public int getBatchSize()
Where setValues() method will be executed for each and every record to set values to the positional parameters existed in PreparedStatement object by getting values from the provided List.
Batch Processing — EmployeeDao.java
Batch Processing — EmployeeDaoImpl.java
Batch Processing
// TODO Auto-generated method stub return list.size(); } }); } catch (Exception e) { e.printStackTrace(); } return rowCounts; } }
Jdbc.properties — applicationContext.xml
jdbc.driverClassName = oracle.jdbc.OracleDriver jdbc.url = jdbc:oracle:thin:@localhost:1521:xe jdbc.username = system jdbc.password = durga
Jdbc.properties — Test.java
Stored Procedure and Functions in Spring JDBC
If we want to access stored procedures and functions ehich are available at database from Spring Jdbc application then we have to use "SimpleJdbcCall".
To use SimpleJdbcCall in Spring Jdbc applications we have to use the following steps. 1)Create DAO interface and its implementation class. 2)IN DAO implementation class, we have to declare DataSource and JdbcTemplate and its respective setter method . 3)In side setter method we have to create SimpleJdbcCall object.
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(); jdbcCall.withProcedureName("proc_Name"); 4)Configure DataSource and DAO implementation class in beans configuration file. 5)Access "execute" method by passing IN type parameters values in the form of "SQLParameterSource". public Map execute(Map m) pubhlic Map execute(SqlParameterSource paramSource) public Map execute(Object ... obj)
Procedure to copy at Database — Employee.java
create or replace procedure getSalary(no IN number, sal OUT int) AS BEGIN select esal into sal from emp1 where eno = no; END getSalary; /
Procedure to copy at Database — EmployeeDao.java
Procedure to copy at Database — EmployeeDaoImpl.java
Procedure to copy at Database — applicationContext.xml
Procedure to copy at Database — Test.java
Procedure from Spring JDBC Application by using SimpleJdbcCall
If we want to use CURSOR types in Stored Procedures inorder to retrive multiple Records data then we have to use the following method on SimpleJdbcCall reference.
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource) jdbcCall = jdbcCall.withProcedureName("getAllEmployees"); jdbcCall = jdbcCall.returningResultSet("emps",BeanPropertyRowMapper.newInstance(Employee.class));
After adding returningResultSet(--,--) method, if we access execute() method on SimpleJdbcCall then execute() method will execute procedure, it will get all the results from CURSOR type variable and stored all recordfs in the form of Employee objects in an ArrayList object with "emps"[CURSOR TYPE variable] key in a Map.
Example:
COPY this Procedure in Database — Employee.java
create or replace procedure getAllEmployees(emps OUT SYS_REFCURSOR) AS BEGIN open emps for select * from emp1; END getAllEmployees; /
COPY this Procedure in Database — EmployeeDao.java
COPY this Procedure in Database — EmployeeDaoImpl.java
COPY this Procedure in Database — applicationContext.xml
COPY this Procedure in Database — Test.java
Blob and Clob processing in Spring JDBC
BLOB: It is a data type available at Databases to represent large volumes of binary data. CLOB: It is a data type available at Database to represent large volumes of character data.
In Spring JDBC Applications, to process BLOB and CLOB Data, Spring JDBC has provided the following three interfaces mainly.
AbstractLobCreatingPreparedStatementCallback
--> It will be used to store Blob and Clob related data in Database.
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException
AbstractLobStreamingResultSetExtractor
--> It will be used to retrive BLOB and CLOB data from database.
streamData(ResultSet rs)throws SQLException, IOException, DataAccessException
LobCreator
--> It contains the following methods to prepare Binary stream and character streams to send blob and clob data to database.
setBlobAsBinaryStream() setClobAsCharacterStream()
LobHolder — Employee.java
--> It contains the following methods to get Binary stream and character stream to get blob and clob data.
getBlobAsBinaryStream() getClobAsCharacterStream()
Example:
LobHolder — EmployeeDao.java
LobHolder — EmployeeDaoImpl.java
LobHolder — applicationContext.xml
jdbc.properties — Test.java
jdbc.driverClassName = oracle.jdbc.OracleDriver jdbc.url = jdbc:oracle:thin:@localhost:1521:xe jdbc.username = system jdbc.password = durga
Spring JDBC Connection Pooling Mechanism
In Database related applications, if we want to perform database operations first we have to creater Connection object then we have to close connection object when the database operations are completed. IN Database related applications, every time creating Connection object and every time destroying Connection object may reduce application performance, because, Creating Connection object and destroying Connection object are two expensive processes, which may reduce application performance.
To overcome the above problem we have to use Connection Pooling in applications.In Connection pooling we will create a set of Connection object in the form of a pool at the application startup time and we will reuse that COnnection objects while executing applications , when database operations are completed then we will send back that connection objects to Pool object with out destroying that connection objects.
In SPring JDBC applications there are three approaches to provide connection pooling.
- Default Connection Pooling Mech.
- Third Party Connection Pooling Mechanisms
- Application Servers provided Connection Pooling Mechanism
- Default Connection Pooling Mech.
In SPring Framework, Default Connection pooling mechanism is existed in the form of org.springframework.jdbc.datasource.DriverManagerDataSource, it is usefull upto testging only, it is usefull for production environment of the application.
If we want to use default Connection Pooling mechanism in SPring JDBC application then we have to configure org.springframework.jdbc.datasource.DriverManagerDataSource in beans configuration file with the following properties .
- driverClassName
- url
- username
- password
EX:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/> <property name="username" value="system"/> <property name="password" value="durga"/> </bean>
Third Party Connection Pooling Mechanisms
In Spring JDBC applications we are able to use the following third party connection pooling mechanisms
- Apache DBCP
- C3P0
- Proxool
Apache DBCP
To use Apcahes DBCP connection pooling mechanism then we have to configure org.apache.commons.dbcp2.BasicDataSource class with the following properties in spring beans configuration file.
- driverClassName
- url
- username
- password
- initialSize:It will take Initial pool size.
- maxTotal: It will allow the specified no of max connections.
EX:
--- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="initialSize" value="20" /> <property name="maxTotal" value="30" /> </bean>
Note: To use this mechanism in Spring JDBC Applications then we have to add the following two jar files to Library.
- commons-dbcp2-2.2.0.jar
- commons-pool2-2.5.0.jar
C3P0 — Employee.java
To use C3P0 connection pooling mechanism then we have to configure com.mchange.v2.c3p0.ComboPooledDataSource class with the following properties in spring beans configuration file.
- driverClass
- jdbcUrl
- user
- password
- minPoolSize:It will take Initial pool size.
- maxPoolSize: It will allow the specified no of max connections.
- maxStatements: Max statements it allows.
- testConnectionOnCheckOut:true/false for Checking Connection before use.
EX:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="user" value="system" /> <property name="password" value="durga" /> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <property name="maxStatements" value="100" /> <property name="testConnectionOnCheckout" value="true" />
</bean>
Note: To use this mechanism in Spring JDBC Applications then we have to add the following two jar files to Library.
- c3p0-0.9.5.2.jar
- mchange-commons-java-0.2.11.jar
- Proxool:
To use Proxool connection pooling mechanism then we have to configure org.logicalcobwebs.proxool.ProxoolDataSource class with the following properties in spring beans configuration file.
- driver
- driverUrl
- user
- password
- minimumConnectionCount:It will take Initial pool size.
- maximumConnectionCount: It will allow the specified no of max connections.
EX:
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="oracle.jdbc.OracleDriver" /> <property name="driverUrl" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="user" value="system" /> <property name="password" value="durga" /> <property name="maximumConnectionCount" value="30" /> <property name="minimumConnectionCount" value="10" />
</bean>
Note: To use this mechanism in Spring JDBC Applications then we have to add the following two jar files to Library.
- proxool-0.9.1.jar
- proxool-cglib.jar
Example:
C3P0 — EmployeeDao.java
C3P0 — EmployeeDaoImpl.java
C3P0 — applicationContext.xml
C3P0 — Test.java
Application Servers provided Connection Pooling Mechanism throw JNDI
JNDI[Java Naming And Directory Interface]: JNDI is a Middleware Service or an abstraction provided by SUN Micreosystems as part of J2EE and which is implemented by all the Application Servers vendors like Weblogic, JBOSS, Glassfish,.....
JNDI is existed inside the application Servers to provide any resource with Global Scope, that is, JNDI will share any resource like "DataSource" to all the applications which are running in the present application server.
In general, almost all the Application Servers are having their own Connection Pooling mechanisms, if we want to use Application Servers provided Connection pooling mechanisms we have to use the following steps.
1)Install Application Server. 2)COnfigure Connection Pooling and Datasource in JNDI provided by Application Servers. 3)Add the required new JARs to Library. 4)Provide JNDI Setups in beans configuration File.
1) Install Application Server[Weblogic Server]
- Download fmw_12.2.1.3.0_wls_quick.jar from internet[oracle.com]
- Open command prompt in Administrator mode.
- Set JAVA8 or JAVA7 in path.
set path=C:\Java\jdk1.8.0_144\bin;
- Goto setup file loaction and execute JAR file with the following command.
F:\softwares\servers\weblogic>java -jar fmw_12.2.1.3.0_wls_quick.jar
- Click on "Next" button.
- Click on "Next" Button
- Specify Home directory "Oracle"
- Click on "Next" button.
- Click on "Next" button.
- Click on "Next" button
- Click on "Install" button.
- Click on "Next" button.
- Click on "Finish" button.
- Provide domain name "durga_domain".
- Click on "Next" button.
- Click on "Next" button.
- Provide user name and password.
user name: weblogic password: weblogic_weblogic confirm password: weblogic_weblogic
- Click on "Next" button.
- Click on "Next" button.
- Select "Adminstration Server"
- CLick on "Next" button.
- Click on "Next" button.
- Click on "Create" button.
- Click on "Next" button.
- Click on "Finish" button.
COnfigure Connection Pooling and Datasource in JNDI provided by Application Servers
- Goto durga_domain location
C:\Oracle\user_projects\domains\durga_domain
- double click on "startWeblogic" batch file.
- Open Browser and provide the following url to open Administration console.
http://localhost:7001/console
- Provide domain user name and password.
user name: weblogic password: weblogic_weblogic
- Click on "Login" button.
- Go for Domain Structer and select "Services".
- Select "DataSource".
- Click on "New" button.
- Select "Generic datasource".
- Provide the following details.
Name: durgads Scope: GLOBAL JNDI Name: durgajndi Database Type: Oracle
- Click on "Next" button.
- Click on "Next" button.
- Click on "Next" button.
- Provide the following details.
Database Name: xe Host Name: localhost Port : 1521 Database User Name: system password: durga confirm password: durga
- Click on "Next" button.
- Click on "Next" button.
- Select "admin server".
- Click on "Finish" button.
Add the required new JARs to Library
To use Weblogic Server provided Connection Pooling mechanism in Spring JDBC Application then we have to use the following jars along with the regular jars. 1)weblogic.jar 2)spring-jdbc-4.0.4.RELEASE.jar 3)spring-tx-4.0.4.RELEASE.jar
Provide JNDI Setups in beans configure — Employee.java
To use Weblogic Server provided Connection Pooling mechanism in Spring JDBC Application then we have to provide the following DataSource configuration in beans configuration file.\
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="durgajndi"/> <property name="jndiEnvironment">
<props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url">t3://localhost:7001</prop>
</props> </property>
</bean>
<bean id="empDao" class="com.durgasoft.dao.EmployeeDaoImpl"> <property name="dataSource" ref="dataSource"/>
</bean>
Example:
Provide JNDI Setups in beans configure — EmployeeDao.java
Provide JNDI Setups in beans configure — EmployeeDaoImpl.java
Provide JNDI Setups in beans configure — applicationContext.xml
Provide JNDI Setups in beans configure — Test.java
- Key ideas of Spring - JDBC / DAO explained simply
- Ready-to-use code examples
- Exam-style questions at the end