java.sql.SQLException:无法加载类:com.mysql.jdbc.Driver
我希望能够将我的程序连接到数据库。但由于我以前从未使用过数据库,因此我将遵循以下教程:。在完成本教程的所有步骤(据我所知)后,我得到以下堆栈跟踪:java.sql.SQLException:无法加载类:com.mysql.jdbc.Driver,java,spring-boot,Java,Spring Boot,我希望能够将我的程序连接到数据库。但由于我以前从未使用过数据库,因此我将遵循以下教程:。在完成本教程的所有步骤(据我所知)后,我得到以下堆栈跟踪: java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader@34a245ab;ClassLoader:sun.misc.Launcher$AppClassLoader@34a24
java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader@34a245ab;ClassLoader:sun.misc.Launcher$AppClassLoader@34a245ab
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:283) ~[tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:716) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:648) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:468) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:143) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) [tomcat-jdbc-8.5.4.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) [tomcat-jdbc-8.5.4.jar:na]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) [spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) [spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46) [spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:176) [spring-boot-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:85) [spring-boot-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:77) [spring-boot-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.postProcessAfterInitialization(DataSourceInitializerPostProcessor.java:62) [spring-boot-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1214) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) [spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) [spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at edu.demo.SpringbootJdbcDemoApplication.main(SpringbootJdbcDemoApplication.java:9) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
User.java
package edu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJdbcDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJdbcDemoApplication.class, args);
}
}
package edu.demo;
public class User{
private Integer id;
private String name;
private String email;
public User(int number, String strName, String strEmail){
id = number;
name = strName;
email = strEmail;
}
public User(){
}
public Integer getId() {
return id;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
SpringbootJdbcDemoApplicationTests.java
package edu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJdbcDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJdbcDemoApplication.class, args);
}
}
package edu.demo;
public class User{
private Integer id;
private String name;
private String email;
public User(int number, String strName, String strEmail){
id = number;
name = strName;
email = strEmail;
}
public User(){
}
public Integer getId() {
return id;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
包edu.demo;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.SpringApplicationConfiguration;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入java.util.List;
导入org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
公共类SpringbootJdbcDemoApplicationTests{
@自动连线
私有用户存储库用户存储库;
@org.junit.Test
公共无效findAllUsers(){
List users=userRepository.findAll();
Assert.assertNotNull(用户);
Assert.assertTrue(!users.isEmpty());
}
@试验
public void findUserById(){
User=userRepository.findUserById(1);
Assert.assertNotNull(用户);
}
@试验
public void createUser(){
用户=新用户(0,“John”john@gmail.com");
User savedUser=userRepository.create(用户);
User newUser=userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals(“John”,newUser.getName());
Assert.assertEquals(“john@gmail.com,newUser.getEmail());
}
}
UserRepository.java
package edu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJdbcDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJdbcDemoApplication.class, args);
}
}
package edu.demo;
public class User{
private Integer id;
private String name;
private String email;
public User(int number, String strName, String strEmail){
id = number;
name = strName;
email = strEmail;
}
public User(){
}
public Integer getId() {
return id;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
package edu.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
public class SpringbootJdbcDemoApplicationTests {
@Autowired
private UserRepository userRepository;
@org.junit.Test
public void findAllUsers() {
List<User> users = userRepository.findAll();
Assert.assertNotNull(users);
Assert.assertTrue(!users.isEmpty());
}
@Test
public void findUserById() {
User user = userRepository.findUserById(1);
Assert.assertNotNull(user);
}
@Test
public void createUser() {
User user = new User(0, "John", "john@gmail.com");
User savedUser = userRepository.create(user);
User newUser = userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals("John", newUser.getName());
Assert.assertEquals("john@gmail.com", newUser.getEmail());
}
}
包edu.demo;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.SpringApplicationConfiguration;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入java.util.List;
导入org.junit.*;
@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringbootJdbcDemoApplication.class)
公共类SpringbootJdbcDemoApplicationTests{
@自动连线
私有用户存储库用户存储库;
@org.junit.Test
公共无效findAllUsers(){
List users=userRepository.findAll();
Assert.assertNotNull(用户);
Assert.assertTrue(!users.isEmpty());
}
@试验
public void findUserById(){
User=userRepository.findUserById(1);
Assert.assertNotNull(用户);
}
@试验
public void createUser(){
用户=新用户(0,“John”john@gmail.com");
User savedUser=userRepository.create(用户);
User newUser=userRepository.findUserById(savedUser.getId());
Assert.assertNotNull(newUser);
Assert.assertEquals(“John”,newUser.getName());
Assert.assertEquals(“john@gmail.com,newUser.getEmail());
}
}
是的,在pom中,您对h2有依赖关系
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
com.h2数据库
. 或者改变它,或者简单地添加mysql(如果您也使用h2),以
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
mysql
mysql连接器java
和我的解决方案:
步骤1:删除“mysql连接器”maven存储库:
rm -r ~/.m2/repository/mysql/mysql-connector-java
步骤2:将以下代码段添加到pom.xml中:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
mysql
mysql连接器java
第三步:保存项目,然后等待“构建工作区”完成,一切顺利。但是我不知道发生了什么…如果您使用netbeans和hibernate framwork手动添加jdbc,请转到persistence.xml并转到design add jdbc connection,它对我来说很好
祝您好运对于手动构建数据源的人员,除了加载mysql连接器依赖项
,还要确保驱动程序类名
设置正确
DataSourceFactory factory = new DataSourceFactory();
factory.setUrl("..");
factory.setUser("..");
factory.setDriverClass("com.mysql.jdbc.Driver"); //Important
...
DBI dbi = new DBI(factory.build(new MetricRegistry(), "mysql"));
否则,DataSourceFactory.driverClass
实例字段将设置为“”
。当应用程序类加载器尝试加载名为“”
”的类时,它将抛出一个ClassNotFoundException
,在我的pom文件中输入该代码会给我一个新错误:“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障成功发送到服务器的最后一个数据包是0毫秒前的。驱动程序尚未从服务器收到任何数据包。“这确实是一个不同的问题,但是现在您有了mysql jdbc驱动程序,您需要将其配置为指向您的数据库。那通常在你的房间里。通过链接,数据源配置由spring.DataSource.*
,url
,用户名
,密码
和驱动程序类名
中的外部配置属性控制。好的,我将查看您链接到@Elliott Frisch的页面。谢谢你的帮助。哦,天哪,那是很多代码!如果你把它的一部分拿出来,你还有同样的问题吗?在使用dropwizard框架时,我也遇到了非常类似的问题,我缺少带有值的配置,因此错误如下:由:java.sql.SQLException:无法从ClassLoader加载类