使用JDBC和Spring访问关系数据时嵌套的Servlet异常

时间:2014-04-28 21:00:03

标签: java spring spring-mvc

我关注此文件 -

http://spring.io/guides/gs/relational-data-access/#scratch

设置数据源后,创建一个jdbcTemplate -

SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
        dataSource.setUsername("username");
        dataSource.setUrl("jdbc:mysql:/uel.to.connect.the.db");
        dataSource.setPassword("password");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

当我尝试进行选择查询时 -

List<GameBean> results = jdbcTemplate.query("select * from Games",
                new RowMapper<GameBean>() {
                    @Override
                    public GameBean mapRow(ResultSet rs, int rowNum) throws SQLException {
                        GameBean gb = new GameBean();
                        gb.setGameId(rs.getInt("GameId"));
                        gb.setGameName(rs.getString("GameName"));
                        return gb;
                    }
                });

我收到错误提及&#34;无法获得JDBC连接;&#34;

   org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'social_devuser'@'10.55.200.1' (using password: YES)
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
    com.service.games.dao.GamesDao.getAllGames(GamesDao.java:26)
    com.service.games.SpringServiceController.getGreeting(SpringServiceController.java:24)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

有人可以建议我在这里失踪吗?

1 个答案:

答案 0 :(得分:1)

您必须正确配置对MySql架构的访问:

java.sql.SQLException: Access denied for user 'social_devuser'@'10.55.200.1' (using password: YES)

也许您在配置中指定了错误的用户名/密码,或者您没有从10.55.200.1 ip address授予对该用户的访问权限