spring 整合jdbc(一)

没有使用spring注入时   我们需要采用的方法就是利用dataSource进行与数据库的连接:

		DriverManagerDataSource dataSource=new DriverManagerDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
		dataSource.setUser("root");
		dataSource.setPassword("chen");
		
		Connection connection=null;
		
		Statement statement=null;
		
		ResultSet rs=null;
		
		try {
			connection=dataSource.getConnection();
			statement=(Statement) connection.createStatement();
			rs=statement.executeQuery("select * from student");


这里是采用了spring注入后  我们建里的bean是dataSource,就不用在实例化 DriverManagerDataSource  ,这是spring反向注入的好处之一:

		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		DataSource dataSource=(DataSource) ctx.getBean("dataSource");
		
		Connection connection=null;
		
		Statement statement=null;
		
		ResultSet rs=null;
		
		try {
			connection=dataSource.getConnection();
			statement=(Statement) connection.createStatement();
			rs=statement.executeQuery("select * from student");
			
			while (rs.next()) {
				System.out.print("编号="+rs.getInt(1));
				System.out.print("用户名="+rs.getString("username"));
				System.out.print("密码="+rs.getString("password"));
				System.out.println("年龄="+rs.getString("age"));
				
			}

 

Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。

Statement对象,用于执行不带参数的简单SQL语句。

Statement 对象用于将 SQL 语句发送到数据库中。实际上有三种 Statement 对象,它们都作为在给定连接上执行 SQL 语句的包容器:Statement、PreparedStatement(它从 Statement 继承而来)和 CallableStatement(它从 PreparedStatement 继承而来)。它们都专用于发送特定类型的 SQL 语句: Statement 对象用于执行不带参数的简单 SQL 语句;PreparedStatement 对象用于执行带或不带 IN 参数的预编译 SQL 语句;CallableStatement 对象用于执行对数据库已存在的存储过程的调用

在spring的配置文件中利用向导添加dataSource的bean时系统会自动给你加入Myeclipes中dataSource的类库<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">报错:java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

上网查过后的原因是:工程中少了org/apache/commons/pool/impl/GenericObjectPool 所在的包: 这个 类在commons-pool.jar包中 请去apache官方下载一个,放到工程中。

所以这里要改为:<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">,它是通过DriverManagerDataSource类中的DriverManager来进行驱动

<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/mysql">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了很好的支持来与JDBC进行整合。以下是Spring整合JDBC的一般步骤: 1. 添加依赖:在项目的构建文件(如`pom.xml`或`build.gradle`)中,添加Spring JDBC相关的依赖项,包括`spring-jdbc`和适当的数据库驱动程序依赖。 2. 配置数据源:在Spring的配置文件(如XML配置文件或Java配置类)中,配置一个数据源(如`DataSource`)。数据源是一个连接池,它提供了对数据库的连接管理。 3. 配置JdbcTemplate:通过配置一个`JdbcTemplate` bean,将其与数据源关联起来。`JdbcTemplate`是Spring提供的一个简化JDBC操作的工具类。 4. 执行SQL操作:通过使用`JdbcTemplate`的方法,可以执行各种SQL操作,如查询、更新、插入等。`JdbcTemplate`会自动处理数据库连接的获取和释放,以及异常的处理和转换。 下面是一个示例配置文件(XML方式): ```xml <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <!-- 配置JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> ``` 在上述配置中,使用了MySQL数据库作为示例。你需要根据实际情况修改驱动类名、数据库URL、用户名和密码。 通过在代码中注入`JdbcTemplate` bean,并调用其方法,你可以轻松执行各种SQL操作。 这只是整合JDBC的一个简单示例,实际应用中可能还需要进行事务管理、异常处理等其他配置。你可以根据具体需求,进一步深入学习和调整配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值