java.sql.SQLException: url 不能为 null [Multi modules App]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41960563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: The url cannot be null [Multi modules App]
提问by The Guide
I want to access to my web app by using an oauth2 server.
我想使用 oauth2 服务器访问我的网络应用程序。
The authentification pass well and I am redirected to my web app.
身份验证通过得很好,我被重定向到我的网络应用程序。
But the problem is that when I try to load datas from my data base, I get this
但问题是当我尝试从我的数据库加载数据时,我得到了这个
exception :
例外 :
janv. 31, 2017 1:16:07 PM org.apache.tomcat.jdbc.pool.PooledConnection connectUsingDriver
AVERTISSEMENT: Not loading a JDBC driver as driverClassName property is null.
janv. 31, 2017 1:16:07 PM org.apache.tomcat.jdbc.pool.ConnectionPool init
GRAVE: Unable to create initial connections of pool.
java.sql.SQLException: The url cannot be null
Can someone help me please.
有人能帮助我吗。
EDIT:
编辑:
I have a separate config module where I setup my DB connection. And I add this module like a dependency in my web app.
我有一个单独的配置模块,用于设置我的数据库连接。我在我的网络应用程序中添加了这个模块,就像一个依赖项。
My application.properties file
我的 application.properties 文件
# DataSource configuration
datasource.host=localhost
datasource.name=my-db
datasource.port=5432
datasource.username=my-user
datasource.password=my-password
datasource.url=jdbc:postgresql://${datasource.host}:${datasource.port}/${datasource.name}
datasource.driver.class.name=org.postgresql.Driver
My config class (I am using myBatis like ORM)
我的配置类(我使用 myBatis 之类的 ORM)
@Configuration
@MapperScan(basePackages = {"com.package.mapper"})
@ComponentScan(basePackages = {"com.package.repository"})
public class MyBatisConfig {
@Value("${datasource.driver.class.name}")
private String dbDriverClassName;
@Value("${datasource.url}")
private String dbUrl;
@Value("${datasource.username}")
private String dbUsername;
@Value("${datasource.password}")
private String dbPassword;
@Bean
public DataSource getDataSource() {
return DataSourceBuilder
.create()
.url(dbUrl)
.driverClassName(dbDriverClassName)
.username(dbUsername)
.password(dbPassword)
.build();
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setConfigLocation(new ClassPathResource("META-INF/spring/mybatis-config.xml"));
sessionFactory.setDataSource(getDataSource());
return sessionFactory.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(getDataSource());
}
}
回答by Mike
i think that you have to setup DB connection in your configuration files. Because, from the exception you post i can see that you don't load a Driver for JDBC DriverManager and an URL to access your database.
我认为您必须在配置文件中设置数据库连接。因为,从您发布的异常中,我可以看到您没有加载 JDBC DriverManager 的驱动程序和访问数据库的 URL。
"driverClassName" value="oracle.jdbc.OracleDriver"
"url" value="jdbc:oracle:thin:@localhost:1521:xe"
"username" value="user"
"password" value="password"
this is a simple example of what i'm saying. If i'm wrong please edit your post with your configuration for DB Connection.
这是我所说的一个简单的例子。如果我错了,请使用您的数据库连接配置编辑您的帖子。