spring中操作mysql数据库

本文介绍了一个简单的Spring整合MySQL数据库的示例项目。通过Maven管理依赖,使用Spring JDBC模块进行数据库操作。示例展示了如何在Spring环境中配置数据源并实现对学生信息的基本增删改查功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是在spring中,对mysql数据库进行增删改查的例子,非常简单。

文件结构



maven的pom.xml文件,里面用到的几个非常重要的jar包都有

<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>MySpring</groupId>
  <artifactId>MySpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MySpring</name>
  <description>MySpring</description>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.1.0.RELEASE</version>
</dependency>
<dependency> <groupId> mysql</groupId>
     <artifactId> mysql-connector-java</artifactId> 
     <version> 5.0.5</version>
      </dependency>

</dependencies>

  
</project>


操作类

package nx.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

public class StudentsDaoImpl implements StudentsDao{

	private DataSource dataSource;
	public DataSource getDataSource() {
		return dataSource;
	}
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public void saveStudents() {

		Connection conn=null;
		PreparedStatement stmt=null;
		try {
			conn=dataSource.getConnection();
			stmt=conn.prepareStatement("INSERT INTO `data`.`student` (`id`, `name`) VALUES (NULL, 'jiang');");
			stmt.execute();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("保存学生信息成功!");
	}

}


beans.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"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    
<property name="url" value="jdbc:mysql://www.itbuluoge.com:3306/wordpress"></property>   
<property name="username" value="root"></property>    
<property name="password" value="1234451"></property>       
</bean>
<bean id="userImpl" class="nx.database.StudentsDaoImpl">
<property name="dataSource" ref="dataSource"></property>          
</bean>
</beans>




测试类

package nx.database;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ctx=new ClassPathXmlApplicationContext("nx/database/beans.xml");
		StudentsDaoImpl userImpl=(StudentsDaoImpl)ctx.getBean("userImpl");
		userImpl.saveStudents();
	}

}



在完成后,查看数据库,发现已经有了数据



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值