如何在spring jdbc中从远程服务器获取mysql连接?

时间:2021-02-23 07:27:24

I am using db4free's mysql server for my spring-webmvc project.But the problem is , I can't get a connection to the server and the Exception is

我在我的spring-webmvc项目中使用db4free的mysql服务器。但问题是,我无法连接到服务器而且Exception是

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: 
Must specify port after ':' in connection string

But I have specified port correctly just after ':' , Here is my configuration java class:

但是我在':'之后正确指定了端口,这是我的配置java类:

@Configuration
@ComponentScan(basePackages="org.ratajo.amaderbari")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new      DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://http://www.db4free.net:3306/myDB");
        dataSource.setUsername("user");
        dataSource.setPassword("pass");

        return dataSource;
    }

Here is the sample program I am trying to execute

这是我试图执行的示例程序

    MvcConfiguration mv = new MvcConfiguration();
    JdbcTemplate jdbcTemplate = new JdbcTemplate(mv.getDataSource());
    String sql="CREATE TABLE 'contact' ('contact_id' int(11) NOT NULL AUTO_INCREMENT,) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8";
    jdbcTemplate.execute(sql);

1 个解决方案

#1


The url looks weird :

网址看起来很奇怪:

    dataSource.setUrl("jdbc:mysql://http://www.db4free.net:3306/myDB");

should be something like

应该是这样的

    dataSource.setUrl("jdbc:mysql://www.db4free.net:3306/myDB");

otherwise it is trying to use http as hostname and //www.db4free.net as port. (which explains the error). But I would also double check the hostname as it looks weird to go to a host 'www.something'.

否则它会尝试使用http作为主机名和//www.db4free.net作为端口。 (这解释了错误)。但我还要仔细查看主机名,因为看起来很奇怪,去主持人'www.something'。

OTOH jdbc url's are weird.

OTOH jdbc网址很奇怪。

#1


The url looks weird :

网址看起来很奇怪:

    dataSource.setUrl("jdbc:mysql://http://www.db4free.net:3306/myDB");

should be something like

应该是这样的

    dataSource.setUrl("jdbc:mysql://www.db4free.net:3306/myDB");

otherwise it is trying to use http as hostname and //www.db4free.net as port. (which explains the error). But I would also double check the hostname as it looks weird to go to a host 'www.something'.

否则它会尝试使用http作为主机名和//www.db4free.net作为端口。 (这解释了错误)。但我还要仔细查看主机名,因为看起来很奇怪,去主持人'www.something'。

OTOH jdbc url's are weird.

OTOH jdbc网址很奇怪。