我尝试使用JDBC制作Spring Embedded Database HSQL Exmple,但是我收到错误,我的代码,consol输出和gist链接都在下面。如何修复这个项目,谢谢你的帮助。
package com.fatih.model;
public class Calisan {
private String ad;
private String soyad;
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
public Calisan() {
// TODO Auto-generated constructor stub
}
public Calisan(String ad, String soyad) {
super();
this.ad = ad;
this.soyad = soyad;
}
@Override
public String toString() {
return "Calisan [ad=" + ad + ", soyad=" + soyad + "]";
}
}
-
package com.fatih.dao;
import java.util.List;
import com.fatih.model.Calisan;
public interface CalisanDAO {
public void calisanEkle(Calisan calisan);
public List<Calisan> caliasanListele();
}
-
package com.fatih.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;
import com.fatih.dao.CalisanDAO;
import com.fatih.model.Calisan;
@Named
public class CalisanDAOImpl implements CalisanDAO {
@Inject
private DataSource dataSource;
public CalisanDAOImpl() {
// TODO Auto-generated constructor stub
}
public void calisanEkle(Calisan calisan) {
PreparedStatement ps=null;
Connection conn=null;
try {
conn=dataSource.getConnection();
ps=conn.prepareStatement("insert into Calisan(ad,soyad)values(?,?)");
ps.setString(1, calisan.getAd());
ps.setString(2, calisan.getSoyad());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(ps!=null){
ps.close();
}
}catch(SQLException e){
throw new RuntimeException(e);
}
}
}
public List<Calisan> caliasanListele() {
PreparedStatement ps=null;
Connection conn=null;
List<Calisan> calisanList=new ArrayList<Calisan>();
try{
conn=dataSource.getConnection();
ps=conn.prepareStatement("select*from calisan");
ResultSet rs=ps.executeQuery();
while(rs.next()){
//int id=rs.getInt("calisanId");
String ad=rs.getString("ad");
String soyad=rs.getString("soyad");
Calisan calisan=new Calisan(ad, soyad);
calisanList.add(calisan);
}
System.out.println("Calisan listesi");
for (Calisan calisan : calisanList) {
System.out.println(calisan);
}
}catch(SQLException e){
e.printStackTrace();
}
return calisanList;
}
}
-
package com.fatih.service;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import com.fatih.dao.impl.CalisanDAOImpl;
import com.fatih.model.Calisan;
@Named
public class CalisanService{
private CalisanDAOImpl calisanDAOImpl;
public CalisanService() {
// TODO Auto-generated constructor stub
}
@Inject
public CalisanService(CalisanDAOImpl calisanDAOImpl) {
this.calisanDAOImpl = calisanDAOImpl;
}
public void calisanEkle(Calisan calisan){
calisanDAOImpl.calisanEkle(calisan);
}
public List<Calisan> calisanListele(){
return calisanDAOImpl.caliasanListele();
}
}
-
package com.fatih.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fatih.model.Calisan;
import com.fatih.service.CalisanService;
public class CalisanTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("app- config.xml");
CalisanService calisanService=context.getBean(CalisanService.class);
Calisan calisan=new Calisan("ali" , "kaya");
calisanService.calisanEkle(calisan);
}
}
-
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean">
<property name="databasePopulator" ref="populator" />
<property name="databaseType" value="HSQL" />
</bean>
<bean id="populator"
class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="scripts">
<list>
<value>schema.sql</value>
<value>data.sql</value>
</list>
</property>
</bean>
<context:annotation-config />
<context:component-scan
base-package="com.fatih" />
data.sql
insert into Calisan (ad, soyad) values ('Ayhan', 'Isik');
insert into Calisan ( ad, soyad) values ('Zeki', 'Müren');
insert into Calisan (ad, soyad) values ( 'Kemal','Sunal');
schema.sql文件
drop table Calisan if exists;
create table Calisan (ad varchar(50), soyad varchar(50));
的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.fatih</groupId>
<artifactId>SpringDBJDBCEmbeddedDatabase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>4.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>
Consol输出
Eki 20,2016 9:26:26 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO:刷新 org.springframework.context.support.ClassPathXmlApplicationContext@31221be2: 启动日期[Thu Oct 20 21:26:26 EEST 2016];背景的根源 层次Eki 20,2016 9:26:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:从类路径加载XML bean定义 资源[app-config.xml] Eki 20,2016 9:26:27 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 信息:JSR-330&#39; javax.inject.Inject&#39;找到的注释和 支持自动装配Eki 20,2016 9:26:27 PM org.springframework.context.support.ClassPathXmlApplicationContext 刷新警告:上下文初始化期间遇到异常 - 取消刷新尝试 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; dataSource&#39;的bean在类路径资源中定义 [app-config.xml]:bean的初始化失败;嵌套异常是 java.lang.NoClassDefFoundError: org / springframework / dao / DataAccessException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83) 在com.fatih.test.CalisanTest.main(CalisanTest.java:12)引起: java.lang.NoClassDefFoundError: org / springframework / dao / DataAccessException at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(未知来源)at java.security.SecureClassLoader.defineClass(未知来源)at java.net.URLClassLoader.defineClass(未知来源)at java.net.URLClassLoader.access $ 100(未知来源)at java.net.URLClassLoader $ 1.run(未知来源)at java.net.URLClassLoader $ 1.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at java.lang.Class.getDeclaredMethods0(Native Method)at java.lang.Class.privateGetDeclaredMethods(Unknown Source)at java.lang.Class.getDeclaredMethods(未知来源)at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:606) 在 org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:518) 在 org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:504) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ... 11更多引起:java.lang.ClassNotFoundException: org.springframework.dao.DataAccessException at java.net.URLClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)... 43更多
线程中的异常&#34; main&#34; org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; dataSource&#39;的bean在类路径资源中定义 [app-config.xml]:bean的初始化失败;嵌套异常是 java.lang.NoClassDefFoundError: org / springframework / dao / DataAccessException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83) 在com.fatih.test.CalisanTest.main(CalisanTest.java:12)引起: java.lang.NoClassDefFoundError: org / springframework / dao / DataAccessException at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(未知来源)at java.security.SecureClassLoader.defineClass(未知来源)at java.net.URLClassLoader.defineClass(未知来源)at java.net.URLClassLoader.access $ 100(未知来源)at java.net.URLClassLoader $ 1.run(未知来源)at java.net.URLClassLoader $ 1.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at java.lang.Class.getDeclaredMethods0(Native Method)at java.lang.Class.privateGetDeclaredMethods(Unknown Source)at java.lang.Class.getDeclaredMethods(未知来源)at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:606) 在 org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:518) 在 org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:504) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ... 11更多引起:java.lang.ClassNotFoundException: org.springframework.dao.DataAccessException at java.net.URLClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)... 43更多
答案 0 :(得分:0)
Looks like you are missing the spring-dao.jar.
nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
Make sure you have the jar file in your classpath.
答案 1 :(得分:0)
Pay attention that in your appconfig.xml (application context) you do not have defined any DataSource object. You defined a DataSourceFactory. That its not the same.
In your classes you are injecting a DataSource object.
So in your appconfig.xml (application context) you should replace this
<bean id="dataSource"
class="org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean">
<property name="databasePopulator" ref="populator" />
<property name="databaseType" value="HSQL" />
</bean>
<bean id="populator"
class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="scripts">
<list>
<value>schema.sql</value>
<value>data.sql</value>
</list>
</property>
</bean>
with this
<!-- jdbc:hsqldb:mem:dataSource -->
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:data.sql" />
<!-- assuming you have your sql scripts in src/main/resources -->
</jdbc:embedded-database>