简体   繁体   English

如何将Spring框架和MySQL与Java swing应用程序结合?

[英]How to Spring framework and MySQL with Java swing application?

I have a Java swing app where I would like to use Spring framework and connect with MySQL database with JDBC. 我有一个Java swing应用程序,我想在其中使用Spring框架并通过JDBC与MySQL数据库连接。 It is a project from a friend that I would like to help, so, nothing related to job. 这是一个我想帮助的朋友的项目,因此,与工作无关。 My knowledge of Spring framework and JDBC is little more than basic. 我对Spring框架和JDBC的了解只不过是基础知识。 THE DATABASE CONNECTION IS NOT WORKING AND I WOULD LIKE TO HAVE A SOLUTION FOR THAT. 数据库连接无法正常工作,我很想为此找到解决方案。 The whole project structure is as following, 整个项目结构如下:

在此处输入图片说明

As you see that the project has 3 main packages ( com.dev.frontend.config, com.dev.frontend.panels and com.dev.frontend.services ; inside the panels there are 2 sub-packages: edit and list ) 如您所见,该项目有3个主要包( com.dev.frontend.config,com.dev.frontend.panels和com.dev.frontend.services ;面板内有2个子包: editlist

Here is how I'm progressing. 这就是我的进度。

  1. I get the dependencies using the pom.xml file and update the Maven project. 我使用pom.xml文件获取了依赖项,并更新了Maven项目。 These are the dependencies that I have currently showing in the picture. 这些是我当前在图片中显示的依赖项。 在此处输入图片说明

  2. I'm using Apache tomcat v8.0 as server and inside the context.xml file, I updated the database information as following, 我将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>

** This provides server the user name, password and the database name. **这为服务器提供了用户名,密码和数据库名。

  1. I add a new WebContent folder in the project and put web.xml and offers-servlet.xml file there as following, 我在项目中添加了一个新的WebContent文件夹,并将web.xml和offers-servlet.xml文件放入其中,如下所示: 在此处输入图片说明

The web.xml file goes as following, 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>

the program knows the database connect from <resource-ref> tag. 该程序从<resource-ref>标签知道数据库连接。 The offers-servlet.xml file is referenced from the web.xml file but I don't make any use of that. 从web.xml文件引用了offers-servlet.xml文件,但我没有对此进行任何使用。 In the end of this file, there is <context-param> tag that tells to scan inside the config package to look for dao-context.xml file. 在该文件的末尾,有一个<context-param>标记,该标记指示扫描config软件包内部以查找dao-context.xml文件。

  1. The dao-context.xml goes as following, dao-context.xml如下所示,

    在此处输入图片说明

It knows the database connection using the <jee:jndi-lookup> tag and look for the services package. 它使用<jee:jndi-lookup>标记知道数据库连接,并查找服务包。

  1. Inside the services package, there is Services.java source file and I'm trying to do some query as following, 在服务包中,有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; } }); } } 

The SQL query inside the getMyCustomer() is not working and returning the following errors, getMyCustomer()内部的SQL查询无法正常工作,并返回以下错误,

在此处输入图片说明

How to solve this problem and connect properly with the database ? 如何解决此问题并正确连接数据库? Thanks. 谢谢。

While upgrading to Spring 4, you could use: 升级到Spring 4时,您可以使用:

File: database-context.xml 文件: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>

Then in your class add: 然后在您的课程中添加:

@PersistenceContext
private EntityManager em;

And use the EntityManager for your queries. 并使用EntityManager进行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM