Guest User

Untitled

a guest
Apr 11th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. driverClassName = com.mysql.jdbc.Driver
  2. url = jdbc:mysql://localhost:3306/musicdb?useSSL=true
  3. username = prospring5
  4. password = prospring5
  5.  
  6. package pl.boot.music;
  7.  
  8. import java.sql.Driver;
  9.  
  10. import javax.sql.DataSource;
  11.  
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.context.annotation.Bean;
  16. import org.springframework.context.annotation.Configuration;
  17. import org.springframework.context.annotation.Lazy;
  18. import org.springframework.context.annotation.PropertySource;
  19. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  20. import org.springframework.jdbc.datasource.SimpleDriverDataSource;
  21.  
  22. @Configuration
  23. @PropertySource("classpath:mysql.properties")
  24. public class MysqlConfig {
  25.  
  26. private static Logger logger = LoggerFactory.getLogger(MysqlConfig.class);
  27.  
  28. @Value("${driverClassName}")
  29. private String driverClassName;
  30. @Value("${url}")
  31. private String url;
  32. @Value("${username}")
  33. private String username;
  34. @Value("${password}")
  35. private String password;
  36.  
  37. @Lazy
  38. @Bean
  39. public DataSource dataSource() {
  40. try {
  41. SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
  42. @SuppressWarnings("unchecked")
  43. Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
  44. dataSource.setDriverClass(driver);
  45.  
  46. logger.debug("url = {" + url + "}");
  47. dataSource.setUrl(url);
  48. logger.debug("username = {" + username + "}");
  49. dataSource.setUsername(username);
  50. logger.debug("password = {" + password + "}");
  51. dataSource.setPassword(password);
  52.  
  53. return dataSource;
  54. } catch (Exception e) {
  55. return null;
  56. }
  57. }
  58. }
  59.  
  60. package pl.boot.music;
  61.  
  62. import static org.junit.Assert.assertTrue;
  63.  
  64. import java.sql.Connection;
  65. import java.sql.PreparedStatement;
  66. import java.sql.ResultSet;
  67. import java.sql.SQLException;
  68.  
  69. import javax.sql.DataSource;
  70.  
  71. import org.junit.Test;
  72. import org.junit.runner.RunWith;
  73. import org.slf4j.Logger;
  74. import org.slf4j.LoggerFactory;
  75. import org.springframework.boot.test.context.SpringBootTest;
  76. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  77. import org.springframework.context.support.GenericApplicationContext;
  78. import org.springframework.test.context.junit4.SpringRunner;
  79.  
  80. @SpringBootTest
  81. public class MusicBootApplicationTests {
  82.  
  83. private static Logger logger = LoggerFactory.getLogger(MusicBootApplicationTests.class);
  84.  
  85. @Test
  86. public void testDataSource() throws SQLException {
  87. GenericApplicationContext ctx = new AnnotationConfigApplicationContext(MysqlConfig.class);
  88. DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
  89. Connection connection = null;
  90. try {
  91. connection = dataSource.getConnection();
  92. PreparedStatement statement = connection.prepareStatement("SELECT 1");
  93. ResultSet resultSet = statement.executeQuery();
  94. while (resultSet.next()) {
  95. int mockVal = resultSet.getInt("1");
  96. assertTrue(mockVal == 1);
  97. }
  98. statement.close();
  99. } catch (SQLException e) {
  100. logger.debug("Something unexpected happened.", e);
  101. throw e;
  102. } finally {
  103. if (connection != null) {
  104. try {
  105. connection.close();
  106. } catch (Exception e) {
  107. }
  108. }
  109. ctx.close();
  110. }
  111. }
  112. }
  113.  
  114. 11:02:43.202 [main] DEBUG p.s.b.m.MysqlConfig - url = {jdbc:mysql://localhost:3306/musicdb?useSSL=true}
  115. 11:02:43.212 [main] DEBUG p.s.b.m.MysqlConfig - username = {Przemysław Lastname}
  116. 11:02:43.212 [main] DEBUG p.s.b.m.MysqlConfig - password = {prospring5}
  117. 11:02:43.454 [main] DEBUG p.s.b.m.MusicBootApplicationTests - Something unexpected happened.
  118. java.sql.SQLException: Access denied for user 'Przemysław Lastname'@'localhost' (using password: YES)
  119. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
  120.  
  121. <?xml version="1.0" encoding="UTF-8"?>
  122. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  124. <modelVersion>4.0.0</modelVersion>
  125.  
  126. <groupId>pl.boot.music</groupId>
  127. <artifactId>preliminary</artifactId>
  128. <version>0.0.1-SNAPSHOT</version>
  129. <packaging>war</packaging>
  130.  
  131. <name>MusicBoot</name>
  132. <description>Music DB Spring Boot</description>
  133.  
  134. <parent>
  135. <groupId>org.springframework.boot</groupId>
  136. <artifactId>spring-boot-starter-parent</artifactId>
  137. <version>2.0.1.RELEASE</version>
  138. <relativePath/> <!-- lookup parent from repository -->
  139. </parent>
  140.  
  141. <properties>
  142. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  143. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  144. <java.version>1.8</java.version>
  145. </properties>
  146.  
  147. <dependencies>
  148. <dependency>
  149. <groupId>org.springframework.boot</groupId>
  150. <artifactId>spring-boot-starter-jdbc</artifactId>
  151. </dependency>
  152. <dependency>
  153. <groupId>org.springframework.boot</groupId>
  154. <artifactId>spring-boot-starter-web</artifactId>
  155. </dependency>
  156.  
  157. <dependency>
  158. <groupId>mysql</groupId>
  159. <artifactId>mysql-connector-java</artifactId>
  160. <scope>runtime</scope>
  161. </dependency>
  162. <dependency>
  163. <groupId>org.springframework.boot</groupId>
  164. <artifactId>spring-boot-starter-tomcat</artifactId>
  165. <scope>provided</scope>
  166. </dependency>
  167. <dependency>
  168. <groupId>org.springframework.boot</groupId>
  169. <artifactId>spring-boot-starter-test</artifactId>
  170. <scope>test</scope>
  171. </dependency>
  172. </dependencies>
  173.  
  174. <build>
  175. <plugins>
  176. <plugin>
  177. <groupId>org.springframework.boot</groupId>
  178. <artifactId>spring-boot-maven-plugin</artifactId>
  179. </plugin>
  180. </plugins>
  181. </build>
  182.  
  183. </project>
Add Comment
Please, Sign In to add comment