I have uploaded a public project on Github:
https://github.com/sotish/SpringMVC11.git

My model class is:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.data.annotation.Id;
@EntityScan
@Entity
@Table(name = "Student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable= false, unique=true)
private String firstName;
@Column(nullable= false, unique=true)
private String lastName;
@Column(nullable= false, unique=true)
private int age;
@Column(nullable= false)
private String email;
//getter and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
I made a Student interface :-
package com.sat.Dao;
import java.util.List;
public interface Student {
public void insert(Student student);
public Boolean update();
public Student findById(int id);
public Student findByFirstname(String firstname);
List<Student> select(int id, String firstname, String lastname, String email, int age);
List<Student> selectAll();
}
studentDao is
import java.util.List;
public class StudentDao implements Student{
@Override
public void insert(Student student) {
// TODO Auto-generated method stub
}
@Override
public Boolean update() {
// TODO Auto-generated method stub
return null;
}
@Override
public Student findById(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Student findByFirstname(String firstname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Student> selectAll() {
// TODO Auto-generated method stub
return null;
}
}
I have a database on mySql

I am trying to connect to the Student database using SpringDriverManager or BasicdataSource.
How to go about it?
My main application class is:
@SpringBootApplication
@ComponentScan ({"com.sat", "com.sat.controller"} )
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
public class SpringMvc10Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringMvc10Application.class);
application.setShowBanner(false);;
application.run(args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
}
}
my Application properties file is
# Spring MVC
#spring.view.prefix:classpath:/templates/
#spring.view.suffix:.html
#spring.view.view-names:jsp/*, html/*
#spring.thymeleaf.view-names:thymeleaf/*
name=Phil, david
# Server
server.port=8088
#override the spring parameter 'create-drop', 'create' creates the schema deleting the previous data
spring.jpa.hibernate.ddl-auto=create
# no sql in the log
spring.jpa.show-sql=false
# mySQL
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/student
database.username=root
database.password=root
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.excluded-view-names= # comma-separated list of view names that should be excluded from resolution
#spring.thymeleaf.view-names= well, # comma-separated list of view names that can be resolved
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache= true
# set to false for hot refresh
My simple index.html is where user insert inputs that get stored on the database :
<!DOCTYPE html>
<html> <!--xmlns:th="http://www.thymeleaf.org">-->
<head>
<title>Thymeleaf tutorial: exercise 2</title>
<!--<link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" />-->
<meta charset="utf-8" />
</head>
<body>
<h1>Thymeleaf tutorial - Student Info</h1>
<h2>Student information</h2>
<input type="text" name="fname" value="" id="firstName"/>
<input type="text" name="lname" value="" id="lastName"/>
<label for="submit">submit</label><input type="submit" name="submit" value="submit" id="submit"/>
<span th:text="${name}"> </span>
</body>
</html>
Now I am confused as I am following lots of tutorials but I don't see much on springBoot. I don't know how to go further.
Please suggest me to the right direction as I am learning Spring. I hope someone would help as I've been stuck on this for days now.
I want to make a connection pool using spring Driver Manager:
@Bean (name = "dataSource")
public DataSource dm() {
DriverManagerDataSource dbs = new DriverManagerDataSource();
dbs.setDriverClassName("jdbc.driverClassName");
dbs.setUrl("jdbc:mysql://localhost:3306/student");
dbs.setUsername("root");
dbs.setPassword("root");
// dbs.max-active=100;
return dm();
}
How to set maxActive Connections, in this?
Now I want to inject this onto my StudentDaoImp class, something like this:
@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) throws Exception {
java.sql.Connection con = ds.getConnection();
// List
con.close();
return null;
}
I get this when I run the project as Spring Boot App:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.sat.SpringMvc10Application: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dm' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [jdbc.driverClassName]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMetho
Could someone help me correct the mistakes.
Thanks
You forgot to set the correct driver class name:
Change the statement
dbs.setDriverClassName("jdbc.driverClassName");to
dbs.setDriverClassName("com.mysql.jdbc.Driver");and you should be good to go. Well, at least this exception should be gone. And don't forget to add the driver to your class path.
Edit:
dm()callsdm()instead of simply returningdbs.@Idannotation in yourStudententity (should be the JPA one).EntityManagerto do that. And in this very case, when using Spring Data JPA (part of your class path), you needn't even implement a DAO using anEntityManager, you can simply define a JPA repository Java interface with the methods you desire.