java spring jdbc_Spring与JDBC支持
JDBC是一种标准Java编程接口(JAVA API),可以将Java编程语言与广泛数据库进行连接。
JDBC API库包含下面提到的每个任务,都是与数据库相关的常用用法。数据库的连接
创建sql语句
执行或提交sql语句
查看或修改查询到的记录
从根本上说,JDBC是一种规范,它提供了一套完整的接口,可以便携式访问底层数据库。
JDBC架构
JDBC API支持两层或三层的处理模式连接数据库,但一般只使用两层处理模式。JDBC API:提供了应用程序对JDBC管理器的连接。
JDBC DRIVER API:提供了JDBC管理器对驱动程序的连接。
JDBC API使用驱动程序管理器和数据库特定的驱动程序来提供异构(heterogeneous)数据库的透明连接。
JDBC驱动程序管理器可确保正确的驱动程序访问每个数据源,驱动程序管理器能够支持连接多个异构数据库的多个并发驱动程序。
JDBC API 提供了以下接口和类
DriverManager:这个类管理一系列数据库驱动程序。使用通信子协议从Java应用程序中请求合适的数据库驱动程序,建立数据库连接。
Driver:这个接口处理与数据库服务器的通信。我们将很少直接与驱动陈旭互动。
Connection:此接口具有操作数据库的所有方法。改连接对象表示通信上下文,即所有与数据库的通信仅通过这个对象连接。
Statement:这个接口的对象将sql语句提交到数据库。
ResultSet:这个接口保存sql语句查询结果。
SqlException:这个类处理发生在数据库应用程序的任何错误。
代码示例
Customer类
public class Customer {
int custId;
String name;
int age;
//getter and setter methods}
数据库访问DAO
public interface CustomerDAO{
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
/*** @author :fengzhiwei* @date :Created in 2019/6/10 11:16* @description:${description}*/
public class JdbcCustomerDAO implements CustomerDAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void insert(Customer customer) {
StringBuilder sb = new StringBuilder();
sb.append("insert into customer ")
.append("(cust_id, name, age) ")
.append("value ")
.append("(?,?,?)");
Connection connection = null;
try {
connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sb.toString());
preparedStatement.setInt(1, customer.getCustId());
preparedStatement.setString(2, customer.getName());
preparedStatement.setInt(3, customer.getAge());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public Customer findByCustomerId(int custId) {
StringBuilder sb = new StringBuilder("select * from cutomer where cust_id = ?");
Connection connection = null;
Customer customer = null;
try {
connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sb.toString());
preparedStatement.setInt(1, custId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
customer = new Customer();
customer.setCustId(resultSet.getInt("cust_id"));
customer.setAge(resultSet.getInt("age"));
customer.setName(resultSet.getString("name"));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return customer;
}
}
Spring 配置文件
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
java spring jdbc_Spring与JDBC支持相关推荐
- Spring学习五(JDBC支持)
Spring的jdbc支持 1配置db.properties,将有关JDBC的信息载入 2bean文件配置数据源,这里用e3p0作为数据源载入db.properties 3配置template的bea ...
- spring jdbc_Spring JDBC示例
spring jdbc Spring JDBC is the topic of this tutorial. Databases are integral part of most of the En ...
- Java缓存学习之五:spring 对缓存的支持
(注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...
- 伯远帖 - Java Spring 4.0 集成 MyBatis 3.1 支持
1. pom.xml 添加MySQL组件 <dependency><groupId>mysql</groupId><artifactId>mysql-c ...
- 69道Java Spring 面试笔试题
目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...
- Spring对JNDI的支持方法
Spring对JNDI的支持 Spring中对于JNDI的访问,提供了便捷的方法,在Spring的org.springframework.jndi包中包含了所有的类.其中提供了一下核心类: 1)Jnd ...
- Java中的事务——JDBC事务和JTA事务
转载自 Java中的事务--JDBC事务和JTA事务 我的博客中曾经关于事务有过很多讨论,之前的事务介绍基本都是数据库层面的事务,本文来介绍一下J2EE中和事务相关的内容,在阅读本文之前,希望读者对分 ...
- datastore_使用Spring Session和JDBC DataStore进行会话管理
datastore 在Web应用程序中,用户会话管理对于管理用户状态至关重要. 在本文中,我们将学习在集群环境中管理用户会话所采用的方法,以及如何使用Spring Session以更简单和可扩展的方式 ...
- 使用Spring Session和JDBC DataStore进行会话管理
在Web应用程序中,用户会话管理对于管理用户状态至关重要. 在本文中,我们将学习在集群环境中管理用户会话所遵循的方法,以及如何使用Spring Session以更加简单和可扩展的方式实现它. 通常在生 ...
最新文章
- php 按汉字首字母查询[转载]
- 【Python】*args 和 **kwargs的用法
- Nginx-Nginx配置文件详细说明
- 使用ubuntu的新立德下载和安装的Eclipse无法在其help菜单中连接并安装ADT
- python postmessage 鼠标_SilkTest高级进阶系列7-用PostMessage模拟鼠标
- 最流行的轻量级php框架,GitHub - meolu/zan: zan 轻量级PHP微框架
- sqlserver之创建视图
- 2019年VQA论文整理
- 假设某台式计算机的内存容量为256,计算机二级试题与答案
- flask爱家租房项目开发(一)
- Linux主机Windows容器,了解用于Linux和 Windows容器的Docker“容器主机”与“容器操作系统”...
- “System.NullReferenceException”类型的异常在 App_Web_j2s3gau3.dll 中发生,但未在用户代码中进行处理的Bug解决方案
- 阿里系纯滑块验证码破解思路
- 第十二届noc网络机器人赛项成绩_第十二届NOC活动全国决赛圆满落下帷幕
- 机器视觉CCD和CMOS图像传感器的区别主要有哪些
- 第5章 - 二阶多智能体系统的协同控制 --> 连续时间系统编队控制【程序代码】
- docker容器的detached模式下查看logs
- java 修改图片dpi_如何在图像中设置DPI信息?
- 按占比划分文件,并将文件名写入txt文件(train、valid、trainvalid、test)
- EXCEL使用技巧-如何将单个单元格内容隐藏