【问题标题】:Spring Boot gives an error of URL must start with 'jdbc'Spring Boot 给出 URL 必须以 'jdbc' 开头的错误
【发布时间】:2020-10-31 18:55:42
【问题描述】:

我正在尝试使用注释配置在 Spring Boot 中配置 Postgres 数据库。我在一个名为 database.properties 的文件中拥有所有数据库凭据,配置文件名为 DBconfig.java

database.url= jdbc:postgresql://localhost/mydb 
database.driverClassName= com.postgresql.jdbc.Driver
database.username= postgres
database.password= password

dbConfig 文件 -

@Configuration
@PropertySource("classpath:databaseAccess/database.properties")

public class DBconfig {

  @Value("${username}")
  private String username;

  @Value("${password}")
  private String password;

  @Value("${url}")
  private String url;

  @Bean
  @Qualifier("postgresDB")
  public DataSource dataSource() {
    DataSourceBuilder dataSource = DataSourceBuilder.create();
    dataSource.url(url);
    dataSource.password(password);
    //dataSource.driverClassName(driverClassName);
    dataSource.username(username);
    return dataSource.build();
  }

  @Bean
  @Qualifier("jdbcTemplate")
  public JdbcTemplate jdbcTemplate() {
      return new JdbcTemplate(dataSource());
  }
}

这是我的主文件

     @SpringBootApplication
     public class GetNotificationsApplication {

        public static void main(String[] args) {

           ApplicationContext ctx = new AnnotationConfigApplicationContext(DBconfig.class);
           JdbcTemplate template= ctx.getBean("jdbcTemplate", JdbcTemplate.class);
           template.execute("CREATE TABLE TEST( test VARCHAR(20))");

        }

     }

我不断收到错误 工厂方法“dataSource”抛出异常;嵌套异常是 java.lang.IllegalArgumentException: URL must start with 'jdbc'

【问题讨论】:

  • url 中的值是否与属性文件中的值一样?
  • 是的,我在我的计算机上的 8080 端口启动了一个 Postgres 服务器。
  • Postgres 服务器在 8080,那么你的 spring 应用程序正在运行哪个端口?
  • 哎呀对不起我的意思是 5432 标准端口

标签: postgresql spring-boot spring-jdbc


【解决方案1】:

尝试通过为 postgres 定义端口号来更改 url 参数的值。假设 postgres 在默认端口 5432 上运行。

改变

jdbc:postgresql://localhost/mydb

到

jdbc:postgresql://localhost:5432/mydb

端口号检查Find the host name and port using PSQL commands

更新:

还将@Value("${username}") 更改为@Value("${database.username}") 和其他属性也可以通过前缀database。

【讨论】:

  • 我确实改变了它,但它仍然给出同样的错误。
  • 有问题...现在检查答案。
猜你喜欢
  • 2018-09-22
  • 2021-03-09
  • 2019-10-22
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 2023-02-05
相关资源
最近更新 更多