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

标签 java mysql spring jdbc

我正在使用db4free's我的 spring-webmvc 项目的 mysql 服务器。但问题是,我无法连接到服务器,异常是

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 类:

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

最佳答案

该网址看起来很奇怪:

    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 很奇怪。

关于java - 如何在 Spring jdbc 中从远程服务器获取 mysql 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196292/

相关文章:

JBoss 7 中的 Spring 模块

java - 如何拆分ArrayList的元素?

java - 为什么这个作业不起作用?

java - 使用 REST 模板 Java Spring MVC 从服务器下载大文件

mysql - 删除级联更新级联的外键约束

mysql本地导入数据基础知识

java - 如何编写没有 for 和 if 语句的代码

php - 锂框架 MySQL 原始查询

java - 在 Spring 中处理嵌套 REST 资源调用的更优雅的方式?

spring - 使用 Spring Sleuth Zipkin 配置基本身份验证