博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring的JDBC示例
阅读量:6441 次
发布时间:2019-06-23

本文共 6994 字,大约阅读时间需要 23 分钟。

以下内容引用自:

实现前提:

1、在MySql上新建数据库并导入相关数据,脚本如下:

---- 数据库: `test`---- ------------------------------------------------------------ 表的结构 `student`--CREATE TABLE IF NOT EXISTS `student` (  `ID` int(11) NOT NULL AUTO_INCREMENT,  `NAME` varchar(20) NOT NULL,  `AGE` int(11) NOT NULL,  PRIMARY KEY (`ID`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;---- 转存表中的数据 `student`--INSERT INTO `student` (`ID`, `NAME`, `AGE`) VALUES(1, 'Zara', 11),(2, 'Nuha', 2),(3, 'Ayan', 15);

2、由于是操作MySql,所以还需要mysql-connector-java.jar包:

mysql
mysql-connector-java
6.0.6

实践例子:

pom.xml:

4.0.0
com.jsoft.testspring
testjdbc
0.0.1-SNAPSHOT
jar
testjdbc
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-context
4.1.4.RELEASE
org.springframework
spring-jdbc
4.1.4.RELEASE
org.springframework
spring-tx
4.1.4.RELEASE
mysql
mysql-connector-java
6.0.6

Student.java:

package com.jsoft.testspring.testjdbc;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.java:

package com.jsoft.testspring.testjdbc;import java.util.List;import javax.sql.DataSource;public interface StudentDAO {    public void setDataSource(DataSource ds);    public void create(String name, Integer age);    public Student getStudent(Integer id);    public List
listStudents(); public void delete(Integer id); public void update(Integer id, Integer age);}

StudentMapper.java:

package com.jsoft.testspring.testjdbc;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper
{ @Override public Student mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); return student; }}

提示:此类的作用是通过一行数据返回一个Student的对象。

StudentJDBCTemplate.java:

package com.jsoft.testspring.testjdbc;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;    @Override    public void setDataSource(DataSource dataSource) {        this.dataSource = dataSource;        this.jdbcTemplateObject = new JdbcTemplate(dataSource);    }    @Override    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;    }    @Override    public Student getStudent(Integer id) {        String SQL = "select * from Student where id = ?";        Student student = jdbcTemplateObject.queryForObject(SQL, new Object[] { id }, new StudentMapper());        return student;    }    @Override    public List
listStudents() { String SQL = "select * from Student"; List
students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } @Override 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; } @Override 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; }}

提示:通过继承DAO接口实现相关方法。

beans.xml:

App.java:

package com.jsoft.testspring.testjdbc;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.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
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()); }}

测试结果:

 

测试工程:

==>如有问题,请联系我:easonjim#163.com,或者下方发表评论。<==

转载地址:http://evcwo.baihongyu.com/

你可能感兴趣的文章
mount什么意思
查看>>
c++-链表的回文结构
查看>>
XML模块
查看>>
编写自动化测试用例的原则
查看>>
poj2955(区间dp)
查看>>
突然多了个机会…纠结了一个星期。
查看>>
SAP QUERY
查看>>
MIGO收货 BAPI :BAPI_GOODSMVT_CREATE BADI增强
查看>>
【windows中常用的服务概览和总结】
查看>>
3.插入排序--直接插入排序
查看>>
UVA1584 UVALive3225 Circular Sequence
查看>>
StringBuffer
查看>>
Per-Pixel Lighting
查看>>
外部中断
查看>>
html的dtd声明
查看>>
ivy resolve标签
查看>>
.NET Web后台动态加载Css、JS 文件,换肤方案
查看>>
使用开源软件vlc media player 录制桌面视频
查看>>
web前端(2)—— 前端技术介绍
查看>>
ife2018 零基础学院 day 3
查看>>