Druid连接池和springJDbc框架-Java(新手)
Druid连接池:
Druid 由阿里提供
安装步骤:
导包 durid1.0.9 jar包
定义配置文件 properties文件
名字任意位置也任意 加载文件
获得数据库连接池对象 通过DuridDataSourceFactory获得
获取链接
SpringJDBC :jdbcTemplate:
SpringJDBC :jdbcTemplate
定义:
Spring框架对jdbc进行了封装 提供的一个JDBCTemplated对象简化jdbc开发
如何用:
导包 libs-->add
创建JDBCTemplate对象,依赖于DataSource
调用JDBCTemplate方法 CRUD
3.1 insert增 delete删 update改 DML语句
3.2 queryForMap() 查询结果封装为map 集合 将列名key value
3.3 queryForList() 查询结果封装List 集合
3.4 query() 查询结果 JavaBean对象
3.5 queryForObject() 将结果封装成对象
实例:
package cn.Wuchuang.JDBCTemplate; import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate; import java.util.List;
import java.util.Map; public class Demo1JDBCTemplate {
//创建JdbcTemplate对象。
JdbcTemplate tem= new JdbcTemplate(JdbcUtils.JDBCUtils.getDataSource()); //指定行添加。
@Test
public void fun1(){ String sql = "insert into tablename1 values (3,?,?)";
int i = tem.update(sql,"吴十一",);
System.out.println(i);
} //添加数据。
@Test
public void fun2(){
String sql = "insert into tablename1(money,uname) values(?,?)";
int i = tem.update(sql,,"吴墨酬");
System.out.println(i);
} //查询所有
@Test
public void fun3(){
String sql = "select * from tablename1";
List<Map<String,Object>>maps = tem.queryForList(sql);
for (Map<String,Object>Som:maps){
System.out.println(Som);
}
} //查询所有记录,将其封装为Emp对象的List集合。
@Test
public void fun4(){
String sql = "select * from emp";
List<Demo1Emps> list = tem.query(sql,new BeanPropertyRowMapper<Demo1Emps>(Demo1Emps.class));
for (Demo1Emps demp : list){
System.out.println(demp);
}
}
}
Emp表的实体化对象:
package cn.Wuchuang.JDBCTemplate;
public class Demo1Emps {
private int uid;
private String uname;
private int Job_id;
private int mgr;
private String state;
private Double salary;
private Double bonus;
private int Dect_id;
public Demo1Emps() {
}
public Demo1Emps(int uid, String uname, int job_id, int mgr, String state, Double salary, Double bonus, int dect_id) {
this.uid = uid;
this.uname = uname;
Job_id = job_id;
this.mgr = mgr;
this.state = state;
this.salary = salary;
this.bonus = bonus;
Dect_id = dect_id;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getJob_id() {
return Job_id;
}
public void setJob_id(int job_id) {
Job_id = job_id;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Double getBonus() {
return bonus;
}
public void setBonus(Double bonus) {
this.bonus = bonus;
}
public int getDect_id() {
return Dect_id;
}
public void setDect_id(int dect_id) {
Dect_id = dect_id;
}
@Override
public String toString() {
return "Demo1Emps{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", Job_id=" + Job_id +
", mgr=" + mgr +
", state='" + state + '\'' +
", salary=" + salary +
", bonus=" + bonus +
", Dect_id=" + Dect_id +
'}';
}
}
JDBC工具类:
package JdbcUtils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; /**
*Druid连接池工具类
* */
public class JDBCUtils {
//1 定义成员变量 DataSource
private static DataSource ds; static{
//2 加载配置文件 获得连接池
try {
Properties pro= new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获得链接
public static Connection getConnection() throws Exception {
return ds.getConnection();
}
// 释放资源
public static void close(Statement stmt,Connection conn){
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement stmt, Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//获得连接池
public static DataSource getDataSource(){
return ds;
}
}
Druid连接池和springJDbc框架-Java(新手)的更多相关文章
- Java开发笔记(一百五十一)Druid连接池的用法
C3P0连接池自诞生以来在Java Web领域反响甚好,业已成为hibenate框架推荐的连接池.谁知人红是非多,C3P0在大型应用场合中暴露了越来越多的局限性,包括但不限于下列几点:1.C3P0管理 ...
- Java学习笔记42(数据库连接池 druid连接池)
druid连接池: 是阿里的连接池,druid的稳定性及效率都很高,目前用的比较广,所以建议开发过程中尽量用druid连接池(支持国产最重要) druid连接池也需要配置文件,配置文件必须是prope ...
- Druid连接池(无框架)
关于连接池有不少技术可以用,例如c3p0,druid等等,因为druid有监控平台,性能在同类产品中算top0的.所以我采用的事druid连接池. 首先熟悉一个技术,我们要搞明白,为什么要用他, 他能 ...
- 【Java】java数据库连接中C3P、DBCP、Druid连接池的使用
使用JDBC的步骤:1.加载数据库驱动2.通过DriverManager获得数据库连接3.通过Connection获得Statement对象4.使用Statement执行SQL语句.5.操作结果集合6 ...
- java基础之JDBC八:Druid连接池的使用
基本使用代码: /** * Druid连接池及简单工具类的使用 */ public class Test{ public static void main(String[] args) { Conne ...
- spring+mybatis+c3p0数据库连接池或druid连接池使用配置整理
在系统性能优化的时候,或者说在进行代码开发的时候,多数人应该都知道一个很基本的原则,那就是保证功能正常良好的情况下,要尽量减少对数据库的操作. 据我所知,原因大概有这样两个: 一个是,一般情况下系统服 ...
- spring 5.x 系列第6篇 —— 整合 mybatis + druid 连接池 (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...
- spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...
- Spring Boot (四): Druid 连接池密码加密与监控
在上一篇文章<Spring Boot (三): ORM 框架 JPA 与连接池 Hikari> 我们介绍了 JPA 与连接池 Hikari 的整合使用,在国内使用比较多的连接池还有一个是阿 ...
随机推荐
- Python实现线程交替打印字符串
import threading con = threading.Condition() word = u"12345上山打老虎" def work(): global word ...
- java.lang.ClassNotFoundException :xxxxxxx
码的!java.lang.ClassNotFoundException: org.springframework.jdbc.core.RowMapper这个错误搞了半天 因为写的项目是手动建的WEB- ...
- The difference between applicationContext.xml in Spring and xxx-servlet.xml in SpringMVC
一直搞不明白两者的区别.如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 因为 ...
- 那些让程序员目瞪口呆的Bug
程序员一生与bug奋战,可谓是杀敌无数,见怪不怪了!在某知识社交平台中,一个"有哪些让程序员目瞪口呆的bug"的话题引来了6700多万的阅读,可见程序员们对一个话题的敏感度有多高. ...
- Oracle Compute云快速搭建MySQL Keepalived高可用架构
最近有个客户在测试Oracle Compute云,他们的应用需要使用MySQL数据库,由于是企业级应用一定要考虑高可用架构,因此有需求要在Oracle Compute云上搭建MySQL高可用集群.客户 ...
- 《数据结构与算法》—— O(3N)=O(N) ?
上帝的磨盘转动很慢,但是却磨得很细. --毛姆 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles ...
- [翻译]python3中新的字符串格式化方法-----f-string
从python3.6开始,引入了新的字符串格式化方式,f-字符串. 这使得格式化字符串变得可读性更高,更简洁,更不容易出现错误而且速度也更快. 在本文后面,会详细介绍f-字符串的用法. 在此之前,让我 ...
- Roma - Facebook工具链大一统
什么是roma roma,中文名罗马,是Facebook的rn团队的产出,是一个试验性质的javascript工具链,集编译,linter,格式化,打包,测试等等于一体.目标是成为一个处理javasc ...
- Eureka 注册中心看这一篇就够了
服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者 url 串.路由信息等.服务注册中心是微服务架构中最基础的设施之一. 在微服务架构流行之前,注册中心 ...
- Flex实现九宫格
写一个靠谱的flex布局 <!DOCTYPE html> <html> <style> .block { padding-top: 30%; margin-top: ...