spring framwor操作数据库示例spring-jdbc
来源:互联网 发布:外链源码 编辑:程序博客网 时间:2024/06/05 10:39
示例代码:http://download.csdn.net/detail/u010476739/9922735
工具:maven、spring framework、postgresql
1.准备数据库,使用postgresql
CREATE TABLE Student( ID SERIAL PRIMARY Key, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL);
2.新建项目,添加依赖
<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.jackletter</groupId> <artifactId>spring-jdbc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>this is name</name> <description>this is desc</description> <properties> <spring.version>4.2.4.RELEASE</spring.version> <postgresql.version>9.1-901.jdbc4</postgresql.version> </properties> <dependencies> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version></dependency><dependency><groupId>postgresql</groupId><artifactId>postgresql</artifactId><version>${postgresql.version}</version></dependency> </dependencies></project>
3.spring配置(applicationContext.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-3.0.xsd "><!-- Initialization for data source --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="org.postgresql.Driver" /><property name="url" value="jdbc:postgresql://localhost:5432/Demo" /><property name="username" value="postgres" /><property name="password" value="huqingjie123" /></bean><!-- Definition for studentJDBCTemplate bean --><bean id="studentJDBCTemplate" class="bean.StudentJDBCTemplate"><property name="dataSource" ref="dataSource" /></bean></beans>
4.代码
Student
package bean;public class Student {private Integer age;private String name;private Integer id;public void setAge(Integer age) {this.age = age;}public Integer getAge() {return age;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setId(Integer id) {this.id = id;}public Integer getId() {return id;}}
StudentDAO
package bean;import java.util.List;import javax.sql.DataSource;public interface StudentDAO {public void setDataSource(DataSource dataSource);public void create(String name, Integer age);public Student getStudent(Integer id);public List<Student> listStudents();public void delete(Integer id);public void update(Integer id, Integer age);}
StudentJDBCTemplate
package bean;import java.util.List;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class StudentJDBCTemplate implements StudentDAO {private DataSource dataSource;private JdbcTemplate jdbcTemplateObject;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;this.jdbcTemplateObject = new JdbcTemplate(dataSource);}public void create(String name, Integer age) {String SQL = "insert into Student (name, age) values (?, ?)";jdbcTemplateObject.update(SQL, name, age);System.out.println("Created Record Name = " + name + " Age = " + age);return;}public Student getStudent(Integer id) {String SQL = "select * from Student where id = ?";Student student = jdbcTemplateObject.queryForObject(SQL, new Object[] { id }, new StudentMapper());return student;}public List<Student> listStudents() {String SQL = "select * from Student";List<Student> students = jdbcTemplateObject.query(SQL, new StudentMapper());return students;}public void delete(Integer id) {String SQL = "delete from Student where id = ?";jdbcTemplateObject.update(SQL, id);System.out.println("Deleted Record with ID = " + id);return;}public void update(Integer id, Integer age) {String SQL = "update Student set age = ? where id = ?";jdbcTemplateObject.update(SQL, age, id);System.out.println("Updated Record with ID = " + id);return;}}
StudentMapper
package bean;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper<Student> {public Student mapRow(ResultSet rs, int rowNum) throws SQLException {Student student = new Student();student.setId(rs.getInt("id"));student.setName(rs.getString("name"));student.setAge(rs.getInt("age"));return student;}}
App
package springjdbc;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import bean.Student;import bean.StudentJDBCTemplate;public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate");System.out.println("------Records Creation--------");studentJDBCTemplate.create("Zara", 11);studentJDBCTemplate.create("Nuha", 2);studentJDBCTemplate.create("Ayan", 15);System.out.println("------Listing Multiple Records--------");List<Student> students = studentJDBCTemplate.listStudents();for (Student record : students) {System.out.print("ID : " + record.getId());System.out.print(", Name : " + record.getName());System.out.println(", Age : " + record.getAge());}System.out.println("----Updating Record with ID = 2 -----");studentJDBCTemplate.update(2, 20);System.out.println("----Listing Record with ID = 2 -----");Student student = studentJDBCTemplate.getStudent(2);System.out.print("ID : " + student.getId());System.out.print(", Name : " + student.getName());System.out.println(", Age : " + student.getAge());}}
阅读全文
0 0
- spring framwor操作数据库示例spring-jdbc
- 《Spring JDBC 操作数据库》
- Spring操作数据库示例
- Spring JDBC数据库操作类
- Spring使用JDBC操作数据库
- Spring-JDBC 操作SQL数据库
- Spring之jdbc数据库操作
- Spring JDBC模版基本操作示例
- Spring JDBC模版基本操作示例
- Spring+jdbc使用示例
- Spring JDBC 示例
- Spring集成JDBC操作数据库实例教程
- 用Spring的JDBC直接操作数据库
- spring jdbc-数据库操作对象化模型
- Spring MVC + JDBC 实现数据库操作
- 使用spring jdbc template简化jdbc数据库操作实例代码
- Spring Jdbc常规操作
- Spring Jdbc常规操作
- MBProgressHUD提示框简单实用的封装
- Java实现邮箱验证
- ccs灰色断点问题的解决
- 656. Coin Path
- Qt+MySQL实现数据库图书管理系统
- spring framwor操作数据库示例spring-jdbc
- (Mac) 命令行生成Dex文件
- 用 GetEnvironmentVariable 获取常用系统环境变量
- 爬虫Scrapy框架的安装配置
- Xcode插件优缺点对比(推荐20款插件)
- IO流——FileReader
- UVA12186AnotherCrisis
- 硬盘分区之MBR讲解
- oracle sql,using