0

I have a spring mvc project that is supposed to register some one when they enter their desired username and password, but when ever the user enters their username and password i get the error "Loading XML bean definitions from class path resource [beans.xml]

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server."

here is the java file that handles the register for and has a register controller

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<form action="register">

<div class="imgcontainer">

<img src="img_avatar2.png" alt="Avatar" class="avatar">

</div>





<div class="container">

<label for="uname"><b>Username</b></label>

<input type="text" placeholder="Enter Username" name="userName" required>



<label for="psw"><b>Password</b></label>

<input type="password" placeholder="Enter Password" name="password" required>



<button type="submit">Register</button>

<label>

<input type="checkbox" checked="checked" name="remember"> Remember me

</label>

</div>



<div class="container" style="background-color:#f1f1f1">

<button type="button" class="cancelbtn">Cancel</button>

<span class="psw">Forgot <a href="#">password?</a></span>

</div>

</form>

</html>

here is the java file that handles the register for and has a register controller

package com.springmvctim.controller;



import java.sql.SQLException;



import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;



import com.springmvctim.dao.LoginService;

import com.springmvctim.model.UserAuto;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



//import com.springmvctim.dao.LoginService;

//import com.springmvctim.model.UserAuto;











@Controller

public class LoginController {



private LoginService login_service;

@RequestMapping(value="/register", method=RequestMethod.GET)

public String register(HttpServletRequest request, HttpServletResponse response

) {

ApplicationContext ap = new ClassPathXmlApplicationContext("beans.xml");

login_service = (LoginService)ap.getBean("ldao");

String user_name = request.getParameter("userName");

String password = request.getParameter("password");

UserAuto user = new UserAuto();

user.setUserName(user_name);

user.setPassword(password);

//LoginService login_service = new LoginService();

try {

login_service.InsertUser(user,true);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "display.jsp";

}



}

here is the dao class that handles that is supposed to actually enter the user into the db where the InsertUser method is created

package com.springmvctim.dao;



import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Base64;



import javax.sql.DataSource;



import org.springframework.jdbc.core.JdbcTemplate;

//import org.eclipse.jdt.internal.compiler.ast.ThisReference;

//import org.springframework.jdbc.core.JdbcTemplate;

//import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;



import com.mysql.jdbc.ResultSetMetaData;

import com.springmvctim.dbservice.DBConnection;

import com.springmvctim.model.UserAuto;





@Repository

public class LoginService {

  private DataSource dataSource;

   private JdbcTemplate jdbcTemplate;



   



   public void setjdbcTemplate(DataSource dataSource) {

        this.jdbcTemplate = new JdbcTemplate(dataSource);

       

       

    }   

public void InsertUser(UserAuto user, boolean isEncrypted) throws SQLException {

    Connection con = null;

   

    try {

    //con= DBConnection.getConnection();

           Base64.Encoder encoder = Base64.getUrlEncoder();

           

           

           String password = (isEncrypted) ? encoder.encodeToString(user.getPassword().getBytes()) : user.getPassword();



   

    String insert = "INSERT INTO `user`(user_name,password) VALUE('"+user.getUserName()+"','"+password+"') " ;

    //jdbcTemplateObject = new JdbcTemplate();

    // con.prepareStatement(insert).executeQuery();

   

   

   

    jdbcTemplate.update(insert);

    }catch(Exception e) {

    System.out.print(e);

    }

   



    }

}

Here is the bean.xml that I put in the resources folder

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

<beans xmlns = "http://www.springframework.org/schema/beans"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation = "http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">



<!-- Initialization for data source -->

<bean id="jdbcTemplate"

class = "org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>

<property name = "url" value = "jdbc:mysql://localhost:3306/Automation"/>

<property name = "username" value = "root"/>

<property name = "password" value = "Password"/>

</bean>



<!-- Definition for studentJDBCTemplate bean -->

<bean id = "ldao"

class = "com.springmvctim.dao.LoginService">

<property name = "jdbcTemplate" ref = "jdbcTemplate" />

</bean>



</beans>

Here is the web.xml that connects to the dispathcher servlet

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>



<display-name>Archetype Created Web Application</display-name>





<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>



<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/springtest-servlet.xml</param-value>



</init-param>

<load-on-startup>1</load-on-startup>

</servlet>



<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>





</web-app>

and here is the dispatcher that is being called by the web.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:ctx="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd ">







<ctx:component-scan base-package="com.springmvctim.controller"></ctx:component-scan>

<ctx:annotation-config></ctx:annotation-config>

</beans>

I also made sure that I have mysql connector in pom.xml

enter image description here

I am able to maven clean and install. I have tried cleaning the project and the tomcat server. I have tried Deployment assembly for all the maven dependencies but this did not work.

Here are the logs

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Server version name:   Apache Tomcat/9.0.63

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Server built:          May 11 2022 07:52:11 UTC

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Server version number: 9.0.63.0

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: OS Name:               Mac OS X

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: OS Version:            10.15.7

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Architecture:          x86_64

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: JVM Version:           17.0.1+12-LTS-39

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: JVM Vendor:            Oracle Corporation

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: CATALINA_BASE:         /Users/tismith/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: CATALINA_HOME:         /Users/tismith/Downloads/apache-tomcat-9.0.63

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: -Dcatalina.base=/Users/tismith/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: -Dcatalina.home=/Users/tismith/Downloads/apache-tomcat-9.0.63

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: -Dwtp.deploy=/Users/tismith/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: -Dfile.encoding=UTF-8

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.VersionLoggerListener log

INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages

Jun 29, 2022 9:25:01 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent

INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [/Users/tismith/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]

Jun 29, 2022 9:25:01 PM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler ["http-nio-8080"]

Jun 29, 2022 9:25:01 PM org.apache.catalina.startup.Catalina load

INFO: Server initialization in [582] milliseconds

Jun 29, 2022 9:25:01 PM org.apache.catalina.core.StandardService startInternal

INFO: Starting service [Catalina]

Jun 29, 2022 9:25:01 PM org.apache.catalina.core.StandardEngine startInternal

INFO: Starting Servlet engine: [Apache Tomcat/9.0.63]

Jun 29, 2022 9:25:02 PM org.apache.jasper.servlet.TldScanner scanJars

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

Jun 29, 2022 9:25:02 PM org.apache.catalina.core.ApplicationContext log

INFO: No Spring WebApplicationInitializer types detected on classpath

Jun 29, 2022 9:25:02 PM org.apache.catalina.core.ApplicationContext log

INFO: Initializing Spring FrameworkServlet 'dispatcher'

Jun 29, 2022 9:25:02 PM org.springframework.web.servlet.DispatcherServlet initServletBean

INFO: FrameworkServlet 'dispatcher': initialization started

Jun 29, 2022 9:25:02 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh

INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Jun 29 21:25:02 EDT 2022]; root of context hierarchy

Jun 29, 2022 9:25:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springtest-servlet.xml]

Jun 29, 2022 9:25:03 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler

INFO: Mapped URL path [/register] onto handler 'loginController'

Jun 29, 2022 9:25:03 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler

INFO: Mapped URL path [/register.*] onto handler 'loginController'

Jun 29, 2022 9:25:03 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler

INFO: Mapped URL path [/register/] onto handler 'loginController'

Jun 29, 2022 9:25:03 PM org.springframework.web.servlet.DispatcherServlet initServletBean

INFO: FrameworkServlet 'dispatcher': initialization completed in 539 ms

Jun 29, 2022 9:25:03 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["http-nio-8080"]

Jun 29, 2022 9:25:03 PM org.apache.catalina.startup.Catalina start

INFO: Server startup in [1895] milliseconds

Jun 29, 2022 9:25:04 PM org.springframework.web.servlet.PageNotFound noHandlerFound

WARNING: No mapping found for HTTP request with URI [/SpringMVC/img_avatar2.png] in DispatcherServlet with name 'dispatcher'

Jun 29, 2022 9:25:17 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4efcd47c: startup date [Wed Jun 29 21:25:17 EDT 2022]; root of context hierarchy

Jun 29, 2022 9:25:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [beans.xml]

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • Did you search for `MySQLNonTransientConnectionException: Could not create connection to database server.` ? https://stackoverflow.com/questions/50177907/com-mysql-jdbc-exceptions-jdbc4-mysqlnontransientconnectionexception-could-not – PeterMmm Jul 05 '22 at 11:30
  • Please do some formatting on the code with all that whitespace in there it is totally unreadable. Also include the **full stacktrace** not just a snippet. One issue I already see with your code is that you are mixing Spring 4.1 and 5.3 jars. Never mix modules from different versions of a framework. – M. Deinum Jul 05 '22 at 18:20
  • @PeterMmm that link helped me with my problem – Timothy Smith Jul 05 '22 at 18:40

0 Answers0