0

I am trying to create a Spring project to have database connection using tomcat server and editing the context.xml file in server folder of tomcat

This is my context.xml file

<?xml version="1.0" encoding="UTF-8"?>
<Context>


    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <Resource name="jdbc/SpringPractise4" auth="Prasad"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
        username="root" password="root@123" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/database1" />



</Context>

this is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringPractise4</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.obj</url-pattern>
  </servlet-mapping>


  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/SpringPractise4</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
  </resource-ref>

</web-app>

This is my dispatcher-servlet.xml file

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean name="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
</bean>

    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="com.prasad"></context:component-scan>
</beans>

This is my JSP page where I am calling the database table

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    Welcome ${username}

    <sql:query var="rs" dataSource="jdbc/database1">
select name, email, text from offers
</sql:query>


    DB Table values

    <c:forEach var="row" items="${rs.rows}">
    Name ${row.name}<br />
    Email ${row.email}<br />
    Text ${row.text}<br />
    </c:forEach>


</body>
</html>

I am able to fetch data from the same DB using JDBC in a normal main program but , when I am trying to do this with spring I am getting error.

I am using mysql connector jar 5.1.6 and it is in build path also still I am getting No suitable driver found for jdbc/database1 error

Prasad Madge
  • 71
  • 1
  • 14
  • could you show your application structure tree? i want to see where your context.xml is located. – AchillesVan Jul 25 '16 at 15:41
  • To me it looks like it can't find the mysql connector jar for some reason. Is it in the tomcat lib folder? – Jaydee Jul 25 '16 at 15:56
  • Comments by Jaydee and Drew are correct - you have configured the data source in Tomcat, not in your application, so the mysql connector jar must be in Tomcat's classpath, rather than in your application's classpath. – GreyBeardedGeek Jul 25 '16 at 16:31
  • I was making mistake in my jsp page in this line here i should write jdbc/SpringPractise4 – Prasad Madge Jul 25 '16 at 17:18

0 Answers0