2017-03-08 86 views
0

メインクラスを実行すると、このエラーがポップアップします。私はこの問題のリンクのほとんどをチェックしましたが、問題を解決できませんでした。問題について事前にお知りになりたい場合は、以下のファイルを確認してください。まずSpringフレームワークのMySQL接続エラー(org.springframework.jdbc.CannotGetJdbcConnectionException:JDBC接続を取得できませんでした)

# bean.xml # 
     <?xml version="1.0" encoding="UTF-8"?> 
      <beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 


       <context:property-placeholder 
        location="com/springproject/test/springtest01/props jdbc.properties" /> 

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
        <property name="driverClassName" value="${jdbc.driver}"></property> 
        <property name="url" value="${jdbc.url}"></property> 
        <property name="username" value="${jdbc.username}"></property> 
        <property name="password" value="${jdbc.password}"></property> 
       </bean> 
       <context:component-scan 
        base-package="com.springproject.test.springtest01"> 
       </context:component-scan> 
      </beans> 


      #pom.xml# 
    <project xmlns="http://maven.apache.org/POM/4.0.0"                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
     <modelVersion>4.0.0</modelVersion> 

     <groupId>com.springproject.test</groupId> 
     <artifactId>springtest01</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
     <packaging>jar</packaging> 

     <name>springtest01</name> 
     <url>http://maven.apache.org</url> 

     <properties> 
     <project.build.sourceEncoding>UTF-</project.build.sourceEncoding> 
     </properties> 

     <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>3.2.18.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>3.2.18.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>3.2.18.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>6.0.5</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>3.2.18.RELEASE</version> 
     </dependency> 
     </dependencies> 
    </project> 


    #NoticesDao# 
    package com.springproject.test.springtest01; 

    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.util.List; 

    import javax.sql.DataSource; 

    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.jdbc.core.JdbcTemplate; 
    import org.springframework.jdbc.core.RowMapper; 
    import org.springframework.stereotype.Component; 

    @Component("noticeDao") 
    public class NoticesDAO { 

     private JdbcTemplate jdbc; 

     @Autowired 
     public void setDataSource(DataSource jdbc) { 
      this.jdbc = new JdbcTemplate(jdbc); 
     } 

     public List<Notice> getNotices() { 

      return jdbc.query("select * form notices", new RowMapper<Notice>() {              

       public Notice mapRow(ResultSet rs, int rowNum) throws SQLException { 
        Notice notice = new Notice(); 

        notice.setId(rs.getInt("id")); 
        notice.setName(rs.getString("name")); 
        notice.setEmail(rs.getString("email")); 
        notice.setText(rs.getString("text")); 
        return notice; 
       } 

      }); 
     } 
    } 

    #jdbc.properties# 
    jdbc.username = root 
    jdbc.password = 123456 
    jdbc.driver = com.mysql.jdbc.Driver 
    jdbc.url = jdbc:mysql://localhost:3306/springtutorial 

    #Main# 

    public class App 
    { 
     public static void main(String[] args) 
     { 
      try{ 
       ApplicationContext context = new ClassPathXmlApplicationContext("com/springproject/test/springtest01/beans/beans.xml"); 

       NoticesDAO noticesDAO = (NoticesDAO) context.getBean("noticeDao"); 

       List<Notice> notices = noticesDAO.getNotices(); 
       for(Notice notice: notices){ 
        System.out.println(notice); 
       } 

       ((ClassPathXmlApplicationContext)context).close(); 

      }catch(Exception e){ 
       System.out.println("==========="+e); 
      } 
     } 
    } 

答えて

0

、あなたのSQL要求が無効である、それはselect * FROM noticesないFORMでなければなりません。

第2に、私はあなたと同じエラーを得ることはできませんでした。実際には、現在の設定でBean.xmlとjdbc.propertiesファイルを見つけるためのアプリケーションコンテキストを作成することさえできませんでした。次のように私はいくつかの設定を変更:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

<context:property-placeholder location="jdbc.properties" /> 

が内蔵され、その後、メイン/ resourcesディレクトリ内のファイルを入れて、成功し

mvn clean compile 
mvn mvn exec:java -Dexec.mainClass="com.springproject.test.springtest01.App" 
でプロジェクトを実行しました
関連する問題