Java 如何在spring中使用spring.xml连接数据库?
我是Spring框架的新手,我试着做一些基本的数据库操作。我使用oracle 10g作为数据库。我正确导入了jar文件。以前,我在JdbcDaoImplement.java中直接使用了相同的数据库配置,它工作正常,但是当我将数据库配置放在spring.xml文件中时,它抛出了错误。请告诉我是什么错误 My spring.xml包括:Java 如何在spring中使用spring.xml连接数据库?,java,spring,spring-jdbc,Java,Spring,Spring Jdbc,我是Spring框架的新手,我试着做一些基本的数据库操作。我使用oracle 10g作为数据库。我正确导入了jar文件。以前,我在JdbcDaoImplement.java中直接使用了相同的数据库配置,它工作正常,但是当我将数据库配置放在spring.xml文件中时,它抛出了错误。请告诉我是什么错误 My spring.xml包括: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springfra
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="org.spring"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byName">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property>
<property name="username" value="system"></property>
<property name="password" value="abcd"></property>
</bean>
</beans>
接下来是JdbcDaoImplement.java
package org.spring.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.spring.model.Rect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class JdbcDaoImplement {
@Autowired
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Rect getRect(int rectId)
{
Connection conn=null;
try
{
//String DriverName="oracle.jdbc.OracleDriver";
//Class.forName(DriverName);
//String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
//conn = DriverManager.getConnection(jdbcUrl, "system", "abcd");
conn = dataSource.getConnection();
PreparedStatement ps=conn.prepareStatement("SELECT * FROM RECTANGLE WHERE id=?");
ps.setInt(1, rectId);
Rect rect=null;
ResultSet rs=ps.executeQuery();
if(rs.next()==true)
{
rect=new Rect(rectId,rs.getString("name"));
}
rs.close();
ps.close();
return rect;
}
catch(Exception e)
{
throw new RuntimeException();
}
finally
{
try {
conn.close();
} catch (SQLException e) {
//e.printStackTrace();
}
}
}
}
和JdbcDemo.java
package org.spring;
import org.spring.dao.JdbcDaoImplement;
import org.spring.model.Rect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcDemo {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
JdbcDaoImplement dao=ctx.getBean("dataSource",JdbcDaoImplement.class);
Rect rect=dao.getRect(1);
System.out.println(rect.getName());
}
}
错误日志为:
INFO: Loaded JDBC driver: oracle.jdbc.OracleDriver
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dataSource' must be of type [org.spring.dao.JdbcDaoImplement], but was actually of type [org.springframework.jdbc.datasource.DriverManagerDataSource]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:376)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979)
at org.spring.JdbcDemo.main(JdbcDemo.java:12)
根据异常类型判断,您缺少spring jdbc模块问题1的答案 更改:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource.class" autowire="byName">
好的,我这样做了,但现在另一个例外是抛出。我也更新了这个问题,但是我检查了一下,发现spring-jdbc-4.0.0.RELEASE.jar已经存在了。我必须定义pom.xml吗?spring.xml还不够吗?是的,pom.xml只用于添加项目的依赖项。添加一些解释会使这成为一个更好的答案
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource.class" autowire="byName">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byName">
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
JdbcDaoImplement dao=ctx.getBean(JdbcDaoImplement.class);
Rect rect=dao.getRect(1);
System.out.println(rect.getName());
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"></property>
<property name="url" value=" jdbc:h2:tcp://localhost/~/test2"></property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!-- hibernate mapping to data definition lang -->
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan"><!-- Entities -->
<list>
<value>com.nt</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>