2015-11-07 76 views
0

我有一個Java swing應用程序,我想使用Spring框架並使用JDBC連接MySQL數據庫。這是一個朋友的項目,我希望提供幫助,所以沒有任何與工作有關的東西。我對Spring框架和JDBC的瞭解只不過是基本的。 數據庫連接不起作用,我想要爲此提供解決方案。整個工程結構如下,如何使用Java swing應用程序來Spring框架和MySQL?

enter image description here

正如你看到的,該項目主要有3個包(com.dev.frontend.config,com.dev.frontend.panels和com.dev。 frontend.services;面板裏面有2子包:編輯列表

這裏是我的進度。

  1. 我使用pom.xml文件獲取依賴項並更新Maven項目。這些是我目前在圖片中顯示的依賴關係。 enter image description here

  2. 我使用的Apache Tomcat V8.0作爲服務器和context.xml文件裏面,我更新了數據庫中的信息,如下圖所示

**

<Context> 
<Resource name="jdbc/spring" auth="Container" type="javax.sql.DataSource" 
      maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="student" 
      password="student" driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/mySalesApp1" /> 
</Context> 

* * 這爲服務器提供了用戶名,密碼和數據庫名稱。

  • 我在項目中添加一個新的WebContent文件夾,並把web.xml和報價-servlet.xml文件有如下, enter image description here
  • 的web.xml文件文件如下,

    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    
        <display-name>MySpringMVC</display-name> 
    
        <welcome-file-list> 
        <welcome-file>index.html</welcome-file> 
        <welcome-file>index.htm</welcome-file> 
        <welcome-file>index.jsp</welcome-file> 
        <welcome-file>default.html</welcome-file> 
        <welcome-file>default.htm</welcome-file> 
        <welcome-file>default.jsp</welcome-file> 
        </welcome-file-list> 
    
    
        <servlet> 
        <description></description> 
        <display-name>offers</display-name> 
        <servlet-name>offers</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
        </servlet> 
    
        <servlet-mapping> 
        <servlet-name>offers</servlet-name> 
        <url-pattern>/</url-pattern> 
        </servlet-mapping> 
    
    
        <description>Spring Database</description> 
        <resource-ref> 
        <description>DB Connection</description> 
        <res-ref-name>jdbc/spring</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type> 
        <res-auth>Container</res-auth> 
        </resource-ref> 
    
        <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
        </listener> 
    
        <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
         classpath:com/dev/frontend/config/dao-context.xml 
         </param-value> 
        </context-param> 
    
    </web-app> 
    

    該程序知道數據庫連接從<resource-ref>標記。這些offers-servlet.xml文件是從web.xml文件引用的,但我沒有使用它。在這個文件的末尾,有<context-param>標籤,告訴掃描配置文件包內的dao-context.xml文件。

  • 在DAO-context.xml中變爲如下,

    enter image description here

  • 它知道使用<jee:jndi-lookup>標籤並查找服務數據庫連接包。

    1. 服務包內有服務。java的源文件,我試圖做一些查詢,如下圖所示

      @Component("Services") 
          public class Services { 
      
      
      private static NamedParameterJdbcTemplate jdbc; 
      
      @Autowired 
      public void setDataSource(DataSource jdbc) { 
      
          this.jdbc = new NamedParameterJdbcTemplate(jdbc); 
      } 
      
      public static List<Customer> getMyCustomer() { 
      
          return jdbc.query("select * from Customer", new RowMapper<Customer>(){ 
      
           public Customer mapRow(ResultSet rs, int rowNum) 
             throws SQLException { 
      
            Customer customer = new Customer(); 
      
            customer.setCustomerID(rs.getString("CustomerID")); 
            customer.setName(rs.getString("Name")); 
            customer.setAddress(rs.getNString("Address")); 
            customer.setPhone1(rs.getNString("Phone 1")); 
            customer.setPhone2(rs.getNString("Phone 2")); 
      
            customer.setCreditLimit(rs.getDouble("Credit Limit")); 
            customer.setCurrentCredit(rs.getDouble("Current Credit")); 
      
            return customer; 
           } 
          }); 
      } 
      
      } 
      

    getMyCustomer()內的SQL查詢不工作,返回下面的錯誤,

    enter image description here

    如何解決此問題並正確連接數據庫?謝謝。

    +0

    只是一句話:你的Spring版本是舊的_and_有已知的安全問題。更新至當前3.2或4.x – Marged

    +0

    已更新至 4.2.2.RELEASE。感謝您的建議。 –

    回答

    1

    雖然升級到春天4,你可以使用:

    文件:數據庫的context.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" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        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-4.2.xsd 
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 
    
    
        <context:annotation-config/> 
        <context:spring-configured /> 
        <tx:annotation-driven/> 
        <aop:aspectj-autoproxy proxy-target-class = "true"/> 
    
        <bean id="emf" 
         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
         <property name="dataSource" ref="dataSource" /> 
         <property name="packagesToScan" value="your.jpa.generated"/> 
         <property name="jpaVendorAdapter"> 
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
         </property> 
         <property name="jpaProperties"> 
          <props> 
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
          </props> 
         </property> 
        </bean> 
    
        <bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
         <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
         <property name="url" value="jdbc:mysql://yourserver:3306/yourdatabase" /> 
         <property name="username" value="youruser" /> 
         <property name="password" value="yourpassword" /> 
        </bean> 
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
         <property name="entityManagerFactory" ref="emf" /> 
        </bean> 
    
        <bean id="persistenceExceptionTranslationPostProcessor" 
         class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 
    
    </beans> 
    

    然後在您的類中添加:

    @PersistenceContext 
    private EntityManager em; 
    

    而且使用EntityManager的您的查詢。

    +0

    我是否需要在dao-context,xml文件中使用這些代碼?我將數據庫信息放在Tomcat服務器的conext.xml文件中,並認爲我可以在程序中使用該信息。 –

    +0

    這是一個彈簧配置xml文件。你需要在web.xml中引用它 –

    相關問題