spring框架 使用 JdbcTemplate 对数据进行增删改查
user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=12
maxPoolSize=20
minPoolSize=5
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:dataSourceConfig.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property> </bean> <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
package com.zr.entity; public class User {
private int id;
private String username;
private String password;
private String email;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User() {
super();
}
public User(int id, String username, String password, String email,
String phone) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", email=" + email + ", phone=" + phone + "]";
} }
package com.zr.utils; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import com.zr.entity.User; public class TemplateOperation { private ApplicationContext ctc = null;
private JdbcTemplate template = null;
{
ctc = new ClassPathXmlApplicationContext("applicationContext.xml");
template = (JdbcTemplate) ctc.getBean("template");
} /**
* @return the row number from table
*/
public int queryRowNumber(){
int result = 0;
String sql = "SELECT COUNT(*) FROM USERTEST";
result = template.queryForObject(sql, Integer.class);
return result;
} /**
* update table data by sql command and parameters
* @param sql sql-command
* @param args
* @return the number how many rows be operated
*/
public int updateUser(String sql,Object...args){
return template.update(sql,args);
} /**
* query user infomation by username from table
* @param sql sql-command
* @param args
* @return user infomation
*/
public User queryUserByUsername(String sql,Object...args){
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
User user = template.queryForObject(sql, rowMapper, args);
return user;
} /**
* @return the set storing all data from table
*/
public List<User> queryAllUser(){
String sql = "SELECT * FROM USERTEST";
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
List<User> list = template.query(sql, rowMapper);
return list;
} }
package com.zr.utils; import java.util.List; import com.zr.entity.User; public class TestOperation { public static void main(String[] args) {
TemplateOperation operation = new TemplateOperation(); /*String sql = "INSERT INTO USERTEST VALUES(?,?,?,?,?)";
System.out.println(operation.updateUser(sql,3,"lhh","123","123","123"));*/ String sql = "SELECT * FROM USERTEST WHERE USERNAME=?";
User user1 = operation.queryUserByUsername(sql, "lf");
System.out.println("user1:"+user1); List<User> list = operation.queryAllUser();
for(User user : list){
System.out.println(user);
} System.out.println(operation.queryRowNumber());
}
}
spring框架 使用 JdbcTemplate 对数据进行增删改查的更多相关文章
- Mybatis框架基于注解的方式,实对数据现增删改查
编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...
- Django框架之第二篇--app注册、静态文件配置、form表单提交、pycharm连接数据库、django使用mysql数据库、表字段的增删改查、表数据的增删改查
本节知识点大致为:静态文件配置.form表单提交数据后端如何获取.request方法.pycharm连接数据库,django使用mysql数据库.表字段的增删改查.表数据的增删改查 一.创建app,创 ...
- django学习-12.访问不同url/接口地址实现对指定数据的增删改查功能
1.前言 通过前面博客[django学习-10.django连接mysql数据库和创建数据表]里的操作,我们已经成功在数据库[hongjingsheng_project]里创建了一张数据表[hello ...
- dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)
jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...
- MVC模式:实现数据库中数据的增删改查功能
*.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...
- Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查
5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...
- Mybatis学习总结(二)—使用接口实现数据的增删改查
在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...
- 数据的增删改查(三层)<!--待补充-->
进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)” 既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充) 注: ...
- vue实现对表格数据的增删改查
在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...
随机推荐
- 【英语】Bingo口语笔记(81) - wear系列
- LeetCode 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- 转载Verilog乘法器
1. 串行乘法器 两个N位二进制数x.y的乘积用简单的方法计算就是利用移位操作来实现. module multi_CX(clk, x, y, result); input clk; input [7: ...
- 野村综合社,惠普2面,索尼,CDK面试经理
今天疯了一口气面试了四个企业,自我介绍都说了七八遍,晚上回家到头就睡.中间接了oracle的电话,也不知道如何. 11:00野村面试 野村综合社北京流通部 面试3个人,一个英语部门负责人,一个日语负责 ...
- bzoj 3501 PA2008 Cliquers Strike Back——贝尔数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3501 用贝尔三角形 p^2 地预处理 p 以内的贝尔数.可以模(mod-1)(它是每个分解下 ...
- 【openCV学习笔记】【1】如何载入一张图片
直接看代码好了 #include <iostream> #include <opencv/highgui.h>//这里主要用到窗口显示 int main(int argc, c ...
- You-Get 一键下载全网视频资源
下载视频 无论是单纯的下载视频收藏,还是以便离线收看,都离不开“下载”,好的工具让你把注意力更好的放在视频的本身,而不用考虑要如何下载视频.下载视频从来不乏方法,之前也介绍了下载 Youtube ...
- java代码求输入数的平均值~~~~
总结:1.谢谢程老师,一个很好的老师,人很普通,但是浑浊的世界里,那一份真实感动到底~~~~很感谢他 2.这里注意两个方面,也是我最大的弱点:循环和数组的length属性.前者运用不灵活,后者自己总是 ...
- 分布式缓存系统 Memcached 半同步/半异步模式
在前面工作线程初始化的分析中讲到Memcached采用典型的Master_Worker模式,也即半同步/半异步的高效网络并发模式.其中主线程(异步线程)负责接收客户端连接,然后分发给工作线程,具体由工 ...
- SQL2008R转SQL2005
1.SQL2008R生成 任务--生成脚本 “为服务器版本编写脚本”:SQL Server 2005 “要编写脚本的数据类型”:架构和数据 2.SQL2005还原超大sql语句文件 运行-cmd: o ...