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


    }
}
相关推荐
舒克日记5 分钟前
基于springboot的民谣网站的设计与实现
java·spring boot·后端
凤山老林2 小时前
新一代Java应用日志可视化与监控系统开源啦
java·后端·开源
流星白龙2 小时前
【Qt】7.信号和槽_connect函数用法(2)
java·数据库·qt
黎宇幻生6 小时前
Java全栈学习笔记39
java·笔记·学习
Aurorar0rua7 小时前
C Primer Plus Notes 09
java·c语言·算法
nongcunqq8 小时前
abap 操作 excel
java·数据库·excel
史迪奇_xxx9 小时前
10、一个简易 vector:C++ 模板与 STL
java·开发语言·c++
2301_801252229 小时前
Java中的反射
java·开发语言
遇印记9 小时前
大二java学习笔记:二维数组
java·笔记·学习
小杨同学yx10 小时前
有关maven的一些知识点
java·开发语言