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


    }
}
相关推荐
.生产的驴31 分钟前
SpringBoot 接口国际化i18n 多语言返回 中英文切换 全球化 语言切换
java·开发语言·spring boot·后端·前端框架
Howard_Stark35 分钟前
Spring的BeanFactory和FactoryBean的区别
java·后端·spring
饮长安千年月36 分钟前
学生管理系统审计
java·网络安全·代码审计
-曾牛44 分钟前
Spring Boot中@RequestParam、@RequestBody、@PathVariable的区别与使用
java·spring boot·后端·intellij-idea·注解·spring boot 注解·混淆用法
新时代苦力工1 小时前
处理对象集合,输出Map<String, Map<String, List<MyObject>>>格式数据,无序组合键处理方法
java·数据结构·list
niesiyuan0001 小时前
MAC如何安装多版本jdk(以8,11,17为例)
java
zcyf08091 小时前
kafka理论学习汇总
java·分布式·学习·kafka
再拼一次吧2 小时前
Spring进阶篇
java·后端·spring
爱编程的小庄2 小时前
Maven 4.0.0 模式-pom.xml配置详解
xml·java·maven
黄雪超2 小时前
JVM——引入
java·jvm