spring中操作mysql数据库
来源:互联网 发布:精乌胶囊知乎 编辑:程序博客网 时间:2024/05/24 04:31
就是在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 blocke.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 stubApplicationContext ctx=new ClassPathXmlApplicationContext("nx/database/beans.xml");StudentsDaoImpl userImpl=(StudentsDaoImpl)ctx.getBean("userImpl");userImpl.saveStudents();}}
0 0
- spring中操作mysql数据库
- Spring Boot Mysql 数据库操作
- Spring boot操作mysql数据库
- shell中操作mysql数据库
- Qt中MySQL数据库操作
- Qt中MySQL数据库操作
- spring开发_JDBC操作MySQL数据库
- Spring中利用JDBC模板完成SQLServer数据库(MySQL一样)各种操作
- Spring boot结合Spring-Data-JPA操作MySQL数据库
- spring配置mysql数据库及对数据库的操作
- 在spring中使用SqlMapClientTemplate操作数据库
- Spring中使用JdbcTemplate操作数据库
- Spring中配置连接数据库(MySQL)
- spring+mysql架构中数据库乱码问题
- VB.net中操作MySql数据库
- Web Service中操作MySQL数据库
- eclipse中java操作mysql数据库注意事项
- VS2008中MySQL数据库操作实例
- ssh无法连接到远端Ubuntu的解决方法
- SpringMVC学习道路上的点滴---开发流程
- zoj 3829 Known Notation 贪心
- Java Web开发之详解JSP
- Prototype模式
- spring中操作mysql数据库
- Android中Application类用法
- php使用事务 转载
- 使用cocos compile -p android时总是报错误 错误如下。。。但是使用eclipse生成apk包时不收影响。。
- 实验:基于cancel的不完全恢复
- 运维的开始
- Javascript异步编程的4种方法
- DettachCurrentThread使用情况
- jQuery源码分析系列