软件1班20240509

文章目录

1.JDBC本质

  • 重写 接口的 方法
  • idea 报错 -- 不动脑 alt + enter
  • 知道没有重写方法 CTRL + o 重写 方法 快捷键
java 复制代码
package com.yanyu;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:42
 * @description:
 */
public interface JDBC {
//
//
    void getConnection();
//    这就是一个方法   在接口中的  方法
//      {   }
}
java 复制代码
package com.yanyu;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:45
 * @description:
 */
public class Mysql implements JDBC{
    @Override
    public void getConnection() {
        System.out.println("连接MySQL 数据库");
    }
}
java 复制代码
package com.yanyu;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:55
 * @description:
 */
public class Oracle implements JDBC{

    @Override
    public void getConnection() {
        System.out.println("连接  Oracle  数据库");
    }
}
java 复制代码
package com.yanyu;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:52
 * @description:
 */
public class MysqlTest {
    public static void main(String[] args) {
        JDBC mysql = new Mysql();
        mysql.getConnection();
        System.out.println("------------------");
//        ctrl   +  单机
//        先  调用 JDBC接口 的方法  在 间接到  实现类  MYsql
        JDBC oracle = new Oracle();
        oracle.getConnection();
    }
}

2.增

java 复制代码
package com.yanyu;

import java.sql.*;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:19
 * @description:
 */
public class JDBCTest01 {
    public static void main(String[] args)  {
//        注册驱动   用  反射
        Connection con = null;
        Statement st = null;//   sql   注入
//        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//  8.  版本的 驱动
            String url = "jdbc:mysql://localhost:3306/yanyu";
            String user = "root";
            String password = "yanyu666";
            con = DriverManager.getConnection(url, user, password);
//            System.out.println(con);
//            com.mysql.cj.jdbc.ConnectionImpl@128d2484  说明连接对象没问题
//   不方便关流,所以 要  放大 作用域
//            关闭事务自动提交
            con.setAutoCommit(false);
//            创建操作对象
            st = con.createStatement();
            String sql = "insert into dept values(50,'媒体部','东莞')";
//            执行SQL
            boolean execute = st.execute(sql);
//           手动提交事务
            con.commit();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
//            事务回滚  保证数据库数据安全
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
//              从小到大  rs   st   con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
//       异常 (√)    未重写方法(不可能)
    }

}

3.改

java 复制代码
package com.yanyu;

import java.sql.*;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:19
 * @description:
 */
public class JDBCTest02 {
    public static void main(String[] args)  {
//        注册驱动   用  反射
        Connection con = null;
        Statement st = null;//   sql   注入
//        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//  8.  版本的 驱动
            String url = "jdbc:mysql://localhost:3306/yanyu";
            String user = "root";
            String password = "yanyu666";
            con = DriverManager.getConnection(url, user, password);
//            System.out.println(con);
//            com.mysql.cj.jdbc.ConnectionImpl@128d2484  说明连接对象没问题
//   不方便关流,所以 要  放大 作用域
//            关闭事务自动提交
            con.setAutoCommit(false);
//            创建操作对象
            st = con.createStatement();
            String sql = "update dept set dname = '运营部' where deptno = 50";
//            执行SQL
            boolean execute = st.execute(sql);
//           手动提交事务
            con.commit();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
//            事务回滚  保证数据库数据安全
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
//              从小到大  rs   st   con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
//       异常 (√)    未重写方法(不可能)
    }

}

4.删

java 复制代码
package com.yanyu;

import java.sql.*;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:19
 * @description:
 */
public class JDBCTest03 {
    public static void main(String[] args)  {
//        注册驱动   用  反射
        Connection con = null;
        Statement st = null;//   sql   注入
//        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//  8.  版本的 驱动
            String url = "jdbc:mysql://localhost:3306/yanyu";
            String user = "root";
            String password = "yanyu666";
            con = DriverManager.getConnection(url, user, password);
//            System.out.println(con);
//            com.mysql.cj.jdbc.ConnectionImpl@128d2484  说明连接对象没问题
//   不方便关流,所以 要  放大 作用域
//            关闭事务自动提交
            con.setAutoCommit(false);
//            创建操作对象
            st = con.createStatement();
            String sql = "delete from dept where deptno = 50";
//            执行SQL
            boolean execute = st.execute(sql);
//           手动提交事务
            con.commit();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
//            事务回滚  保证数据库数据安全
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
//              从小到大  rs   st   con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
//       异常 (√)    未重写方法(不可能)
    }

}

5.查

java 复制代码
package com.yanyu;

import java.sql.*;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/5/9 14:19
 * @description:
 */
public class JDBCTest04 {
    public static void main(String[] args)  {
//        注册驱动   用  反射
        Connection con = null;
        Statement st = null;//   sql   注入
//        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//  8.  版本的 驱动
            String url = "jdbc:mysql://localhost:3306/yanyu";
            String user = "root";
            String password = "yanyu666";
            con = DriverManager.getConnection(url, user, password);
//            System.out.println(con);
//            com.mysql.cj.jdbc.ConnectionImpl@128d2484  说明连接对象没问题
//   不方便关流,所以 要  放大 作用域
//            关闭事务自动提交
            con.setAutoCommit(false);
//            创建操作对象
            st = con.createStatement();
            String sql = "select * from dept";
//            执行SQL
            rs = st.executeQuery(sql);
//           手动提交事务
            while (rs.next()){
//                System.out.println(rs.getString("deptno"));
//                System.out.println(rs.getString("dname"));
//                System.out.println(rs.getString("loc"));
                System.out.println(rs.getString(1));
                System.out.println(rs.getString(2));
                System.out.println(rs.getString(3));
            }

            con.commit();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
//            事务回滚  保证数据库数据安全
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
//              从小到大  rs   st   con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
//       异常 (√)    未重写方法(不可能)
    }

}

6.JDBC标准写法

java 复制代码
import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest02 {


    public static void main(String[] args) {
//      1.  获取属性配置文件
        ResourceBundle db = ResourceBundle.getBundle("db");
        String driver = db.getString("driver");
        String user = db.getString("user");
        String password = db.getString("password");
        String url = db.getString("url");
//        2.  放大 数据库几个对象的  作用域
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            //        3.注册驱动

            Class.forName(driver);
//            4.  获取数据库的   连接   对象
            con = DriverManager.getConnection(url,user,password);
//                    url   user    password
//            5.  关闭事务  自动  提交 机制
            con.setAutoCommit(false);

//            9. 获取   操作  对象
            st = con.createStatement();
//            10.  写  sql
            String sql = "insert into t_user values(111,'yy','12')";
//            11.  执行   sql
            boolean execute = st.execute(sql);



//            6.  手段提交事务
            con.commit();
        } catch (ClassNotFoundException | SQLException e) {
//            7.  回滚事务
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            e.printStackTrace();
        }finally {
//            8.  关闭   连接  小  ---  大
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }
}
相关推荐
香精煎鱼香翅捞饭26 分钟前
java通用自研接口限流组件
java·开发语言
ChinaRainbowSea44 分钟前
Linux: Centos7 Cannot find a valid baseurl for repo: base/7/x86_64 解决方案
java·linux·运维·服务器·docker·架构
囧囧 O_o1 小时前
Java 实现 Oracle 的 MONTHS_BETWEEN 函数
java·oracle
去看日出1 小时前
RabbitMQ消息队列中间件安装部署教程(Windows)-2025最新版详细图文教程(附所需安装包)
java·windows·中间件·消息队列·rabbitmq
计算机-秋大田1 小时前
基于Spring Boot的宠物健康顾问系统的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计
JouJz1 小时前
Java虚拟机之垃圾收集(一)
java·开发语言·jvm
源码姑娘1 小时前
基于DeepSeek的智慧医药系统(源码+部署教程)
java·人工智能·程序人生·毕业设计·springboot·健康医疗·课程设计
morris1311 小时前
【redis】布隆过滤器的Java实现
java·redis·布隆过滤器
五行星辰1 小时前
Java链接redis
java·开发语言·redis
编程毕设1 小时前
【含文档+PPT+源码】基于微信小程序的在线考试与选课教学辅助系统
java·微信小程序·小程序