【问题标题】:How to Spring framework and MySQL with Java swing application?如何使用 Java swing 应用程序使用 Spring 框架和 MySQL?
【发布时间】:2016-02-08 18:54:09
【问题描述】:

我有一个 Java swing 应用程序,我想在其中使用 Spring 框架并使用 JDBC 连接 MySQL 数据库。这是一个朋友的项目,我想帮助,所以,与工作无关。我对 Spring 框架和 JDBC 的了解只是基本知识。 数据库连接不工作,我希望有一个解决方案。整个项目结构如下,

如您所见,该项目有 3 个主要包(com.dev.frontend.config、com.dev.frontend.panels 和 com.dev.frontend.services;在面板内有2 个子包:editlist )

这是我的进步。

  1. 我使用 pom.xml 文件获取依赖项并更新 Maven 项目。这些是我目前在图片中显示的依赖项。

  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>

** 这为服务器提供了用户名、密码和数据库名称。

  1. 我在项目中添加了一个新的 WebContent 文件夹,并将 web.xml 和 offer-servlet.xml 文件放在那里,如下所示,

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>

程序从&lt;resource-ref&gt;标签知道数据库连接。 offer-servlet.xml 文件是从 web.xml 文件中引用的,但我没有使用它。在这个文件的最后,有一个&lt;context-param&gt; 标签,告诉我们扫描config 包里面的dao-context.xml 文件。

  1. dao-context.xml如下,

它使用&lt;jee:jndi-lookup&gt; 标签知道数据库连接并查找服务包。

  1. 在服务包中,有 Services.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 查询不起作用并返回以下错误,

如何解决这个问题并正确连接数据库?谢谢。

【问题讨论】:

  • 请注意:您的 Spring 版本较旧并且存在已知的安全问题。更新到当前的 3.2 或 4.x
  • 更新到 4.2.2.RELEASE。感谢您的建议。

标签: java mysql spring swing jdbc


【解决方案1】:

升级到 Spring 4 时,您可以使用:

文件:database-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 进行查询。

【讨论】:

  • 我需要在 dao-context,xml 文件中使用这些代码吗?我将数据库信息放在 Tomcat 服务器的 conext.xml 文件中,并认为我可以在程序中使用该信息。
  • 这是一个spring配置xml文件。您需要在 web.xml 中引用它
猜你喜欢
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多