我有一个Java swing应用程序,我想使用Spring框架并使用JDBC连接MySQL数据库。这是一个朋友的项目,我希望提供帮助,所以没有任何与工作有关的东西。我对Spring框架和JDBC的了解只不过是基本的。 数据库连接不起作用,我想要为此提供解决方案。整个工程结构如下,如何使用Java swing应用程序来Spring框架和MySQL?
正如你看到的,该项目主要有3个包(com.dev.frontend.config,com.dev.frontend.panels和com.dev。 frontend.services;面板里面有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文件有如下,
在DAO-context.xml中变为如下,
服务包内有服务。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; } }); } }
的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文件。
它知道使用<jee:jndi-lookup>
标签并查找服务数据库连接包。
的getMyCustomer()内的SQL查询不工作,返回下面的错误,
如何解决此问题并正确连接数据库?谢谢。
只是一句话:你的Spring版本是旧的_and_有已知的安全问题。更新至当前3.2或4.x – Marged
已更新至 4.2.2.RELEASE 。感谢您的建议。 –