Hints
DriverManagerDataSource is a spring framework utility class. It always returns new connection for each access not working as pool connections.
|
Start a Web based Spring application
-
Select New menu -> Dynamic Web Project
-
Enter Project Name as "SpringWithJDBCDriverBasedDataSourceX"
-
Click Next, Selecting Target runtime as Apache Tomcat 7.0
-
Click Next, Check Generate web.xml deployment descriptor then click on "Finish"
-
Copy and paste Spring's 21 Framework Jars, mysql-connector-java-5.0.8-bin.jar and commons-logging-1.1.jar into /WEB-INF/lib
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringWithJDBCDriverBasedDataSourceX</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-servlet.xml</param-value>
</context-param>
</web-app>
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="mysql" />
</bean>
</beans>
|
package com.springexample;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;
@WebServlet("/ShowBookDetails")
public class ShowBookDetails extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getServletContext());
DataSource dataSource = (DataSource) context.getBean("myDataSource");
JdbcTemplate template = new JdbcTemplate(dataSource);
List<Map<String,Object>> books = template.queryForList("SELECT * FROM books");
for (Map<String,Object> book:books){
resp.getWriter().write(book.get("id")+":"+book.get("book_name")+"\n");
}
}
}
|
Download as Zip
Link to download
SpringWithJDBCDriverBasedDataSourceX
Hints.
Click on File menu. then click " Download"
|