当我运行主类时,会弹出此错误。我已经检查了这个问题的大部分链接,但无法解决问题。请检查以下文件以了解问题,提前谢谢。
# 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 :(得分:0)
首先,您的SQL请求无效,应该是select * FROM notices
而不是FORM
。
其次,我没有设法得到与你相同的错误。实际上,我甚至无法使应用程序上下文找到当前配置的bean.xml和jdbc.properties文件。我改变了一些配置如下:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
和
<context:property-placeholder location="jdbc.properties" />
将文件放在main / resources目录中,然后构建并使用
成功运行项目mvn clean compile
mvn mvn exec:java -Dexec.mainClass="com.springproject.test.springtest01.App"