软件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();
                }
            }
        }


    }
}
相关推荐
customer0814 分钟前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu1 小时前
Spring MVC消息转换器
java·spring
乔冠宇1 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ2 小时前
简单的SQL语句的快速复习
java·数据库·sql
komo莫莫da2 小时前
寒假刷题Day19
java·开发语言
S-X-S3 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq83 小时前
设计模式学习(二)
java·学习·设计模式
桦说编程4 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦4 小时前
Redis 基础命令
java·数据库·redis
字节全栈_rJF5 小时前
性能测试 —— Tomcat监控与调优:status页监控_tomcat 自带监控
java·tomcat