Spring mvc Adding DAO Code

Now we gonna see how to add DAO code to our existing spring mvc application. web.xml OffersController.java offers-servlet.xml home.jsp Upto now we have seen the working of spring mvc application.But, what if we have a DAO(data access object) code to be added to our web application. Which the DAO will do some CRUD operations. Lets have a look of adding the DAO code to Our spring mvc application. we have the Offer pojo. Offer.java we have the OffersDAO, in this class we have configured the DataSource and JdbcTemplate. To know how to configure Datasource and JdbcTemplate http://sourceforopensource.com/spring-querying-using-jdbc-template/. OffersDAO.java dao-context.xml Conclusion: In

Read more

Spring Setting Up a JNDI Datasource

Now we are going to configure our project on a server to use the pooled datasource connection that provided by the server. In previous posts http://sourceforopensource.com/spring-querying-using-jdbc-template/ we used apache dbcp database connection pooling. To create a pooled set of connections we could used to connect to our database. When we run a java web app the java application server for example Tomcat will implement some kind of connection pooling for you and how you configure it and how you configure your application to connect to it depends on how you deploy your application and depends on what server you are using what

Read more

Spring mvc UsingModels

Now we are going to send data to views from controller using models, The model is not available in the session it exists in the request scope. If we use session object the data is available in session object. If we use ModelandView the data is available in request object. web.xml OffersController.java offers-servlet.xml home.jsp output: Insert title hereHi there! Request: Tiffany If you found an error, highlight it and press Shift + Enter or click here to inform us.

Read more

Spring mvc introduction

The way spring mvc works is it routes all requests to the dispatcher servlet. So, someone goes to the browser and tries to go our web application spring will send the request first to this dispatcher servlet and the dispatcher servlet will go and look for the controller that will tell basically what to do with that request  what view to display using what data. We gonna go through all those bit by bit. Lets create a offers web application using spring mvc. pom.xml web.xml When the request is routed to OffersController where the Request is mapped to “/”  by DispatcherServlet

Read more

Spring Create and Getting Placeholders from Bean Properties

Now we are going to see How to give values as properties to sql in spring, using BeanPropertySqlParameterSource and update method. Lets see an example of that. Offers.java jdbc.properties beans.xml OffersDao.java App.java output: Offer [id=3, name=Sue, email=sue@nowhereatall.com, text=PHP coding] Offer [id=4, name=krishna, email=balakrishna.pendyala12@gmail.com, text=this is my new offer] Note: if we run the program again the id will be automatically incremented.   If you found an error, highlight it and press Shift + Enter or click here to inform us.

Read more

Spring Executing Update Statements

This post is about executing update statements in sql in Spring, this is pretty straight forward the jdbcTemplate have a method called update we can use those to run sql level update of our database we already have a sample data in Offers table in database jdbc.properties Offers.java beans.xml OffersDao.java App.java output: Offer [id=1, name=Bob, email=bob@nowhereatall.com, text=I will write Java for you] Offer [id=3, name=Sue, email=sue@nowhereatall.com, text=PHP coding] If you found an error, highlight it and press Shift + Enter or click here to inform us.

Read more

Spring Using Named Parameters

  In this post we are going to see how to query with dynamic input for specific rows, rather than query the entire database. Offer.java jdbc.properties beans.xml OffersDao.java App.java output: Offer [id=1, name=Bob, email=bob@nowhereatall.com, text=I will write Java for you] Offer [id=2, name=Mike, email=mike@nowhereatall.com, text=Web design, very cheap] Offer [id=3, name=Sue, email=sue@nowhereatall.com, text=PHP coding] //this is the output of named parameter Should be Mike: Offer [id=2, name=Mike, email=mike@nowhereatall.com, text=Web design, very cheap] If you found an error, highlight it and press Shift + Enter or click here to inform us.

Read more

Spring Database Exceptions

Exceptions are common in any java based application, exceptions will follow a hierarchy before breaking the compiler. In spring there is a parent class to handle all types of exceptions which is called DataAccessException. We can handle all types of exception using this class. If you want to handle specific exception with some child class there are many child exception classes for every breaking operation. Lets have an example. lets take code from previous example. What happen in the below code is we defined a parent exception “DataAccessException” which handles every exception type. we also catched “CannotGetJdbcConnectionException” which only catches

Read more

Spring-Querying-Using-Jdbc-Template

In this post we gonna see Querying database in Spring using jdbcTemplate. Before starting every thing just add these dependencies to your maven project. pom.xml Now we have a create Offers table in backend which is stored in mysql database and we inserted some sample data. Offers.sql Below we have an Offers.java which takes values and we will return toString() when anyone called object. Offer.java So, Now we are going to Configure Connection pooling with Apache DBCP. The concept of connection pooling is simple, what we do is we just configure the connections only once for application level, because Creating a

Read more
1 2 3 4