JDBC2
1.JDBC连接池
- public class JdbcTemplateDemo2 {
- //Junit单元测试,可以让方法独立执行
- //1. 获取JDBCTemplate对象
- private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
- /**
- * 1. 修改1号数据的 salary 为 10000
- */
- @Test
- public void test1(){
- //2. 定义sql
- String sql = "update emp set salary = 10000 where id = 1001";
- //3. 执行sql
- int count = template.update(sql);
- System.out.println(count);
- }
- /**
- * 2. 添加一条记录
- */
- @Test
- public void test2(){
- String sql = "insert into emp(id,ename,dept_id) values(?,?,?)";
- int count = template.update(sql, 1015, "郭靖", 10);
- System.out.println(count);
- }
- /**
- * 3.删除刚才添加的记录
- */
- @Test
- public void test3(){
- String sql = "delete from emp where id = ?";
- int count = template.update(sql, 1015);
- System.out.println(count);
- }
- /**
- * 4.查询id为1001的记录,将其封装为Map集合
- * 注意:这个方法查询的结果集长度只能是1
- */
- @Test
- public void test4(){
- String sql = "select * from emp where id = ? or id = ?";
- Map<String, Object> map = template.queryForMap(sql, 1001,1002);
- System.out.println(map);
- //{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}
- }
- /**
- * 5. 查询所有记录,将其封装为List
- */
- @Test
- public void test5(){
- String sql = "select * from emp";
- List<Map<String, Object>> list = template.queryForList(sql);
- for (Map<String, Object> stringObjectMap : list) {
- System.out.println(stringObjectMap);
- }
- }
- /**
- * 6. 查询所有记录,将其封装为Emp对象的List集合
- */
- @Test
- public void test6(){
- String sql = "select * from emp";
- List<Emp> list = template.query(sql, new RowMapper<Emp>() {
- @Override
- public Emp mapRow(ResultSet rs, int i) throws SQLException {
- Emp emp = new Emp();
- int id = rs.getInt("id");
- String ename = rs.getString("ename");
- int job_id = rs.getInt("job_id");
- int mgr = rs.getInt("mgr");
- Date joindate = rs.getDate("joindate");
- double salary = rs.getDouble("salary");
- double bonus = rs.getDouble("bonus");
- int dept_id = rs.getInt("dept_id");
- emp.setId(id);
- emp.setEname(ename);
- emp.setJob_id(job_id);
- emp.setMgr(mgr);
- emp.setJoindate(joindate);
- emp.setSalary(salary);
- emp.setBonus(bonus);
- emp.setDept_id(dept_id);
- return emp;
- }
- });
- for (Emp emp : list) {
- System.out.println(emp);
- }
- }
- /**
- * 6. 查询所有记录,将其封装为Emp对象的List集合
- */
- @Test
- public void test6_2(){
- String sql = "select * from emp";
- List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
- for (Emp emp : list) {
- System.out.println(emp);
- }
- }
- /**
- * 7. 查询总记录数
- */
- @Test
- public void test7(){
- String sql = "select count(id) from emp";
- Long total = template.queryForObject(sql, Long.class);
- System.out.println(total);
- }
- }
- /**
- * Druid连接池的工具类
- */
- public class JDBCUtils {
- //1.定义成员变量 DataSource
- private static DataSource ds ;
- static{
- try {
- //1.加载配置文件
- Properties pro = new Properties();
- pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
- //2.获取DataSource
- ds = DruidDataSourceFactory.createDataSource(pro);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 获取连接
- */
- public static Connection getConnection() throws SQLException {
- return ds.getConnection();
- }
- /**
- * 释放资源
- */
- public static void close(Statement stmt,Connection conn){
- /* if(stmt != null){
- try {
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(conn != null){
- try {
- conn.close();//归还连接
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }*/
- close(null,stmt,conn);
- }
- 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;
- }
- }
- driverClassName=com.mysql.jdbc.Driver
- url=jdbc:mysql:///db3
- username=root
- password=root
- # 初始化连接数量
- initialSize=5
- # 最大连接数
- maxActive=10
- # 最大等待时间
- maxWait=3000
2.JDBC druid连接池
- /**
- * 使用新的工具类
- */
- public class DruidDemo2 {
- public static void main(String[] args) {
- /*
- * 完成添加操作:给account表添加一条记录
- */
- Connection conn = null;
- PreparedStatement pstmt = null;
- try {
- //1.获取连接
- conn = JDBCUtils.getConnection();
- //2.定义sql
- String sql = "insert into account values(null,?,?)";
- //3.获取pstmt对象
- pstmt = conn.prepareStatement(sql);
- //4.给?赋值
- pstmt.setString(1,"王五");
- pstmt.setDouble(2,3000);
- //5.执行sql
- int count = pstmt.executeUpdate();
- System.out.println(count);
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- //6. 释放资源
- JDBCUtils.close(pstmt,conn);
- }
- }
- }
- /**
- * Druid演示
- */
- public class DruidDemo {
- public static void main(String[] args) throws Exception {
- //1.导入jar包
- //2.定义配置文件
- //3.加载配置文件
- Properties pro = new Properties();
- InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
- pro.load(is);
- //4.获取连接池对象
- DataSource ds = DruidDataSourceFactory.createDataSource(pro);
- //5.获取连接
- Connection conn = ds.getConnection();
- System.out.println(conn);
- }
- }
3.JDBC c3po连接池
- /**
- * c3p0的演示
- */
- public class C3P0Demo1 {
- public static void main(String[] args) throws SQLException {
- //1.创建数据库连接池对象
- DataSource ds = new ComboPooledDataSource();
- //2. 获取连接对象
- Connection conn = ds.getConnection();
- //3. 打印
- System.out.println(conn);
- }
- }
- /**
- * c3p0的演示
- */
- public class C3P0Demo1 {
- public static void main(String[] args) throws SQLException {
- //1.创建数据库连接池对象
- DataSource ds = new ComboPooledDataSource();
- //2. 获取连接对象
- Connection conn = ds.getConnection();
- //3. 打印
- System.out.println(conn);
- }
- }
- <c3p0-config>
- <!-- 使用默认的配置读取连接池对象 -->
- <default-config>
- <!-- 连接参数 -->
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</property>
- <property name="user">root</property>
- <property name="password">root</property>
- <!-- 连接池参数 -->
- <!--初始化申请的连接数量-->
- <property name="initialPoolSize">5</property>
- <!--最大的连接数量-->
- <property name="maxPoolSize">10</property>
- <!--超时时间-->
- <property name="checkoutTimeout">3000</property>
- </default-config>
- <named-config name="otherc3p0">
- <!-- 连接参数 -->
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/db3</property>
- <property name="user">root</property>
- <property name="password">root</property>
- <!-- 连接池参数 -->
- <property name="initialPoolSize">5</property>
- <property name="maxPoolSize">8</property>
- <property name="checkoutTimeout">1000</property>
- </named-config>
- </c3p0-config>
JDBC2的更多相关文章
- [JDBC-2] JDBC CURD
package com.amuos.jdbc.curd; import java.sql.Connection; import java.sql.ResultSet; import java.sql. ...
- JDBC2.0操作:结果集,更新,插入,删除,批处理语句
JDBC对ResultSet的支持 JDBC最重要的概念是批处理,可以一次完成多个语句的执行. 可滚动的结果集. 如果想创建可滚动的结果集,则在创建PrepareStatement时候必须指定创建的类 ...
- JDBC2 --- 获取数据库连接的方式二 --- 技术搬运工(尚硅谷)
/** * 方式二,对方式一的迭代 * 在如下的程序中,不出现第三方的api,使得程序具有更好的可移植性. * @throws Exception */ @Test public void testC ...
- 吴裕雄--天生自然JAVA数据库编程:JDBC2.0操作
import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...
- JDBC-2(CRUD)
3.PreparedStatement实现CRUD 3.1 操作和访问数据库 数据库连接被用于向数据库服务器发送命令和SQL语句,接受数据库服务器返回的结果.(一个数据库连接就是也给Socket连接) ...
- 浅谈Slick(2)- Slick101:第一个动手尝试的项目
看完Slick官方网站上关于Slick3.1.1技术文档后决定开始动手建一个项目来尝试一下Slick功能的具体使用方法.我把这个过程中的一些了解和想法记录下来和大家一起分享.首先我用IntelliJ- ...
- spring 多数据源一致性事务方案
spring 多数据源配置 spring 多数据源配置一般有两种方案: 1.在spring项目启动的时候直接配置两个不同的数据源,不同的sessionFactory.在dao 层根据不同业务自行选择使 ...
- JDBC 详解(转载)
原文链接:http://blog.csdn.net/cai_xingyun/article/details/41482835 什么是JDBC? Java语言访问数据库的一种规范,是一套API JDBC ...
- 浅谈Slick(4)- Slick301:我的Slick开发项目设置
前面几篇介绍里尝试了一些Slick的功能和使用方式,看来基本可以满足用scala语言进行数据库操作编程的要求,而且有些代码可以通过函数式编程模式来实现.我想,如果把Slick当作数据库操作编程主要方式 ...
随机推荐
- html上传图片后,在页面显示上传的图片
html上传图片后,在页面显示上传的图片 1.html <form class="container" enctype="multipart/form-data&q ...
- leetcode-hard-array-11 Container With Most Water -NO
mycode time limited class Solution(object): def maxArea(self, height): """ :type hei ...
- js携带参数跳转controller返回页面
upauth:function(){ var record = myForm.getRecord(); var companywyId = record.get("companyId&quo ...
- ConstraintLayout的简单介绍和使用
ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能.我们都知道,在传统的Android开发当中,界面基本 ...
- 搭建Git服务器及本机克隆提交
前文 Git是什么? Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首 ...
- 仿flash运动框架
github地址: [https://github.com/linxd5/pictureShow] PS: 新建一个github项目很简单,只要new一个repo,后面按照提示做就可以了~ 项目思路: ...
- java:LeakFilling (Linux)
1.Nosql 列数据库,没有update,非关系型数据库: 为了解决高并发.高可扩展.高可用.大数据存储问题而产生的数据库解决方案,就是NoSql数据库. NoSQL,泛指非关系型的数据库,NoS ...
- lxml的XPath解析
BeautifulSoup 可以将lxml作为默认的解析器使用,同样lxml可以单独使用.下面比较这两者之间优缺点: BeautifulSoup和lxml原理不一样,BeautifulSoup是基于D ...
- SolidWorks学习笔记4特征
绘制斜的拉伸效果 一般拉伸方向垂直于草图基准面, 可以实现绘制一条线,作为其拉伸方向 效果如下 简单孔 在菜单中选择“插入”--“特征”---“简单直孔” 选择一个平面放置 设置好孔的直径和深度后,确 ...
- web端 微软 RDLC 报表插件 宽大于高 横向打印失效 解决方案
起因于系统报表工具使用的RDLC,本地测试一直使用的纵向打印,未测试过横向打印