我正在使用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/