40.0/jdbc/Java数据连接/jar包运用增删改

目录

40.1. 回顾

40.2. 正文

40.1 为什么需要jdbc

40.2 如何连接mysql数据库

40 .3 jdbc容易出现的错误

40.4 完成删除

40.5 完成修改


40.1. 回顾

1. 自联查询: 自己连接自己的表。注意:一定要为表起别名。
2. 嵌套查询: 把一个查询的结果作为另一个查询的条件值。
3. 组合查询: union[去除重复的内容只保留一条]   union all
4. 把查询的结果当作一张临时表和其他表进行联表查询。

-- 1、检索读者“杨凡”所在单位--系
   -- 查询哪些字段 从哪些表查询  查询时需要的条件
  

   select dept from readers where name='杨凡'


-- 2、检索所有读者的全部信息

    select * from readers 


-- 3、检索图书馆中所有藏书的书名和出版单位
   

 select book_name,publisher from books 


-- 4、检索“人民大学出版社”所有的书名和单价,结果按照单价降序排列
   

 select book_name,price from books order by price desc; 


-- 5、检索价格在10元至15元之间的图书的名称、作者、单价和分类号,结果按分类号和单价升序排列

select book_name,author,price,type_id from books where price BETWEEN 10 and 15 order by type_id, price ; 


-- 6、检索“人民大学出版社”和“清华大学出版社”的所有图书的名称和作者

  select book_name,author from books where publisher in('人民大学出版社','清华大学出版社')


-- 7、检索书名以“数据库”开头的所有图书的书名和作者

select book_name,author from books where book_name like '数据库%'


-- 8、检索同时借了总编号为209116和209124两本图书的借书证号
   -- 查询借阅了209116这本书的读者编号

    select reader_id from borrow_info where  book_id=209116 


        
     -- 查询借阅了209124这本书的读者编号且读者编号要和上面的读者编号一致 
  

   select reader_id from borrow_info where book_id=209124 and reader_id in(select reader_id from borrow_info where  book_id=209116)
     select reader_id from borrow_info where book_id in(209116,209124) group by reader_id having count(distinct book_id)>=2;


    
-- 9、检索所有借阅了图书的读者姓名和所在单位
    -- 在借书表中-查询所有的读者id
     

   select name,dept from readers where reader_id in
        (select distinct reader_id from borrow_info)


-- 10、检索“扬凡”所借的所有图书的书名和借阅日期

    select book_name,borrow_time from books b join borrow_info bi on b.book_id=bi.book_id 
    join readers r on r.reader_id=bi.reader_id where r.name='杨凡'    


   
-- 11、检索价格在20元以上且已经借出的图书,结果按单价降序排列

   select distinct b.* from books b join borrow_info bi on b.book_id=bi.book_id where b.price>20 order by price desc


-- 12、检索借阅了“C语言程序设计”一书的读者姓名和所在单位
 

   select name,dept from books b join borrow_info bi on b.book_id=bi.book_id 
    join readers r on r.reader_id=bi.reader_id where b.book_name='C语言程序设计'


-- 13、检索与“杨凡”在同一天借阅了图书的读者的姓名和所在单位
   -- 求出杨凡借书时间
    

 select distinct borrow_time from borrow_info bi join readers r on bi.reader_id=r.reader_id where r.name='杨凡'


     -- 查询与杨凡在同一天借书的读者编号
  

  select distinct reader_id from borrow_info where borrow_time in(select distinct borrow_time from borrow_info bi join readers r on bi.reader_id=r.reader_id where r.name='杨凡')
     
     select name,dept from readers where reader_id in(
                select distinct reader_id from borrow_info where borrow_time in
                (select distinct borrow_time from borrow_info bi     join readers r on bi.reader_id=r.reader_id where r.name='杨凡')
     ) and name!='杨凡'


     
-- 14、检索藏书中比“高等教育出版社”的所有图书的单价更高的图书
    -- 查询高等教育出版社出版社最高的书的价格
        

select * from books where price>
        (select max(price) from books where publisher= '高等教育出版社')


-- 15、检索藏书中所有与“数据库导论”或“数据库原理”在同一出版社出版的图书
   -- 查询“数据库导论”或“数据库原理”的出版社

     select * from books where publisher in(
     select publisher from books where book_name in('数据库导论','数据库原理')
   ) and book_name not in('数据库导论','数据库原理')


-- 16、求该图书馆藏书的总册数

   select count(book_id) from books


-- 17、求“高等教育出版社”的图书中最高的价格、最低的价格以及平均价格

select max(price),min(price),avg(price) from books where publisher= '高等教育出版社'


-- 18、求“计算机学院”当前借阅了图书的读者人数

select count(distinct bi.reader_id) from readers r join borrow_info bi on r.reader_id=bi.reader_id where dept='计算机学院'


-- 19、求各个出版社的最高价格、最低价格、平均价格

select publisher, max(price),min(price),avg(price) from books group by publisher


-- 20、分别求出各个单位当前借阅图书的读者人数

select dept, count(distinct bi.reader_id) from readers r left join borrow_info bi on r.reader_id=bi.reader_id
group by dept;


-- 21、求各个出版单位的册书、价格总额,并按总价降序排列,如有总价相同者按出版社名称降序排列

select publisher, count(book_id),sum(price) from books group by publisher order by sum(price) desc,publisher desc;


-- 22、检索当前至少借阅了5本图书的读者姓名和所在单位

   -- 查询每个读者借书的个数
  

   select name,dept from readers where reader_id in(
     select reader_id from borrow_info group by reader_id having count(distinct book_id)>=5)


-- 23、分别找出借书人数超过10个人的单位和人数
 

   select dept, count(distinct bi.reader_id) from readers r left join borrow_info bi on r.reader_id=bi.reader_id
group by dept having count(distinct bi.reader_id) >10 ;


-- 24、检索没有借阅任何图书的读者姓名和所在单位

select name,dept from readers where reader_id not in(select reader_id from borrow_info)

40.2. 正文

1. jdbc 【java database connection】java数据连接

40.1 为什么需要jdbc

我们前面讲解的都是在mysql的客户端对msql表中的数据进行相关的操作。真实我们需要通过java程序对mysql表中的数据进行相关的操作。

40.2 如何连接mysql数据库

(面试题)

javaoop 面向对象语言

mysql→结构化语言

思考: java要和mysql进行通信,由于语言不同,无法直接通信。 需要翻译。--翻译它也是一个程序,这个程序别人已经写好并且把这个翻译程序打包为一个jar包,以供java连接mysql使用。翻译由不同的数据库厂商提供。jar从网络下载,也可以jar仓库。

(1)创建一个java工程。

(2)在工程中创建一个目录lib【库】 并把mysql的驱动jar包放入该目录

 (3) 把上面的jar--解压--[加载到该工程]

(4)编写代码

package com.demo01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class Test01 {
    public static void main(String[] args) throws Exception{
        //1. 加载驱动==指定驱动名
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2. 获取连接数据库的对象 注意:导入java.sql包下Connection
        /**
         * String url:连接数据库的路径.
         *    jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai
         *    协议:数据库类型://数据库的ip:数据库的端口号/数据库名?时区
         *    时区:在mysql8.0以后才有的
         * String user,数据库的名称root
         * String password 数据库的密码
         */
        String url="jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="root";
        Connection conn = DriverManager.getConnection(url,user,password);
        //3. 获取执行sql语句的对象
        Statement statement = conn.createStatement();
        //4. 执行sql语句
        String sql="insert into tbl_emp values(5,'刘德华','男',25,8888)";
        statement.executeUpdate(sql);

        //5. 关闭资源--如果没有关闭会操作内存的泄露
        statement.close();
        conn.close();
    }
}

连接数据库的步骤:

//1. 加载驱动 Class.forName("com.mysql.cj.jdbc.Driver");

//2. 获取连接对象.

Connection conn=DriverManager.getConnection(url,user,password);

String url="jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai";

//3. 获取执行sql语句的对象

Statement statement=conn.createStatement();

//4. 执行sql语句--增删改

statement.executeUpdate(sql);

//5. 关闭资源 close()

通过java代码往一张表中添加记录。

40 .3 jdbc容易出现的错误

ClassNotFoundException: com.mysql.cj.jdbc.Driver

原因: 【1】可能类名写错了

【2】没有引入mysql驱动jar包

【3】没有解压mysql的驱动jar包

 (2)

数据库名错误。  

(3)  

服务器地址错误或者服务器没有开。  

 (4)

密码错误  

 (5)

sql语句有问题。

40.4 完成删除

public static void delete(int id) throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://192.168.1.70:3306/mydb?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="root";
        Connection conn=DriverManager.getConnection(url,user,password);

        Statement statement=conn.createStatement();
        String sql="delete from tbl_emp where id="+id;
        System.out.println(sql);//JAVA打印
        statement.executeUpdate(sql);
        System.out.println("删除成功");//JAVA打印
        statement.close();
        conn.close();
    }

40.5 完成修改

public static void update() throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://192.168.1.70:3306/mydb?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="root";
        Connection conn=DriverManager.getConnection(url,user,password);

        Statement statement=conn.createStatement();
        String sql="update tbl_emp set name='闫克起',sex='女',age=19 where id=4";
        statement.executeUpdate(sql);

        statement.close();
        conn.close();
    }

改为活数据

 public static void main(String[] args) throws Exception{
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入姓名:");
        String name=sc.next();
        System.out.println("请输入年龄:");
        int age=sc.nextInt();
        System.out.println("请输入性别:");
        String sex=sc.next();
        System.out.println("请输入编号:");
        int id=sc.nextInt();
        update(id,name,sex,age);
    }

    public static void update(int id,String name,String sex,int age) throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://127.0.0.1:3306/mydb?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="root";
        Connection conn=DriverManager.getConnection(url,user,password);

        Statement statement=conn.createStatement();
        String sql="update tbl_emp set name='"+name+"',sex='"+sex+"',age="+age+" where id="+id;


        statement.executeUpdate(sql);

        statement.close();
        conn.close();
    }

  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光明是人的信仰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值