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

How to get a mysql connection from remote server in spring jdbc?

我正在为我的 spring-webmvc 使用 db4free's mysql 服务器 project.But 问题是,我无法连接到服务器和异常是

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

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

@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;
    }

这是我要执行的示例程序

    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);

url 看起来很奇怪:

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

应该是这样的

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

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

OTOH jdbc url 很奇怪。