软件1班20240509

文章目录

1.JDBC本质

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

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

/**
 * @Author [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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();
                }
            }
        }


    }
}
相关推荐
风铃儿~15 分钟前
Spring AI 入门:Java 开发者的生成式 AI 实践之路
java·人工智能·spring
斯普信专业组20 分钟前
Tomcat全方位监控实施方案指南
java·tomcat
忆雾屿30 分钟前
云原生时代 Kafka 深度实践:06原理剖析与源码解读
java·后端·云原生·kafka
武昌库里写JAVA43 分钟前
iview Switch Tabs TabPane 使用提示Maximum call stack size exceeded堆栈溢出
java·开发语言·spring boot·学习·课程设计
gaoliheng0061 小时前
Redis看门狗机制
java·数据库·redis
我是唐青枫1 小时前
.NET AOT 详解
java·服务器·.net
Su米苏1 小时前
Axios请求超时重发机制
java
本郡主是喵3 小时前
并发编程 - go版
java·服务器·开发语言
南风lof3 小时前
源码赏析:Java线程池中的那些细节
java·源码阅读