美文网首页Mysql
JDBC 1.认识jdbc

JDBC 1.认识jdbc

作者: 第二套广播体操 | 来源:发表于2019-03-28 14:24 被阅读0次

    JDBC Java DataBase Connectivity Java数据库连接
    统一的一套Java代码操作所有关系型数据库
    JDBC:定义了操作所有关系型数据库的规则(提供了接口Driver)

    各数据库 去实现这套接口 提供数据库驱动jar包 我们可以使用这套接口编程
    真正执行的代码是驱动Java包的实现类

    步骤:(注册 连接 执行)
    1.导入Jar包
    mysql-connector-java-5.1.37-bin
    复制Jar包到项目自己创建的(libs)目录下 并add as library 加载驱动
    2.注册驱动
    3.获取数据库连接对象connection
    DriverManager.getConnection
    4.定义sql
    5.获取执行数据库对象 statement
    6.执行sql语句接受返回值 影响的行数或者Resultset结果集
    7.处理结果
    8.释放资源

    DriverManager 驱动管理程序
    Connection 数据库连接对象
    Statement 执行sql的对象
    Resultset 结果集
    PreparedStatement 执行sql的对象 (使用这个)


    mysql 5.0以后可以不注册驱动
    com.sql.jdbc.Driver文件中存在静态代码块 加载文件时,静态代码块会被自动加载
    会自动调用其中的DriverManager.registerDriver();方法注册驱动

    DriverManager 驱动管理程序
    1.功能:注册驱动 告诉程序使用了哪个数据库Jar包
    2.获取数据库连接 DriverManager.getConnection();
    url 指定连接路径:语法 jdbc:mysql://ip地址或域名:端口号/数据库名
    连接本机mysql默认服务器端口3306可以简写
    jdbc:mysql:///数据库名

    Connection 数据库连接对象
    功能:
    1 获取执行sql数据库对象statement 或者PreparedStatement(sql)
    2 管理事务
    开启事务 setAutoCommit 传入参数 false开启手动提交模式
    提交 commit();
    回滚 rollback();

    Statement 执行静态sql executeUpdate 执行增删改 executeQuery 执行查询
    ResultSet 封装结果的对象 通过next()方法移动光标 通过getXXX("列名");
    获取相应数据

    Connection Statement ResultSet 使用后都需要释放资源
    完整的更新数据操作

    public class Jdbc_Update {
        public static void main(String[] args) {
            Connection conn = null;
            Statement statement = null;
    
            try {
    //        加载驱动
                Class.forName("com.mysql.jdbc.Driver");
    //        获取connection对象 连接数据库对象
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");
    //        获取statement对象 执行数据库对象
                statement = conn.createStatement();
    //            sql语句
                String sql = "update galary set galary = 1700 where id=2";
                int count = statement.executeUpdate(sql);
                if (count > 0)
                    System.out.println("修改成功");
                else
                    System.out.println("修改失败");
    
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    }
    

    因为代码的冗长 每次进行相关操作都需要重复的注册驱动创建连接 关闭 很麻烦 所以模仿Arrays创建Jdbc数据库工具类 JdbcUnits

    ClassLoader

                ClassLoader loader = JdbcUnits.class.getClassLoader();
    //            通过classLoader对象动态获取指定在src文件夹下的文件路径
                String path = loader.getResource("jdbc.properties").getPath();
                Properties prop = new Properties();
               prop.load(new FileReader(path));
    
    //            通过classLoader对象动态获取指定在src文件夹下的文件路径
                ClassLoader loader = JdbcUnits.class.getClassLoader();
    //           通过ClassLoader 直接获取相应文件的inputstream
                InputStream inputStream = loader.getResourceAsStream("jdbc.properties");
                Properties prop = new Properties();
                prop.load(inputStream);
    

    功能实现:
    1 随着调用方法类的注入自动注册驱动
    2 将url user password 保存成jdbc. properties 可以进行快速修改
    3 实现静态获取Connection对象的功能
    4 实现静态获取Connection statement ResultSet 关闭功能

    jdbc. properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db1
    user=root
    password=root
    

    JdbcUnits

    //模仿其他工具类模式 创造jdbc工具类
    public class JdbcUnits {
        private static String url;
        private static String user;
        private static String password;
    
        //    每次启动文件时只需要被调用一次 不需要每次调用方法都重新加载
        static {
    
            try {
    //            通过classLoader对象动态获取指定在src文件夹下的文件路径
                ClassLoader loader = JdbcUnits.class.getClassLoader();
    //           通过ClassLoader 直接获取相应文件的inputstream
                InputStream inputStream = loader.getResourceAsStream("jdbc.properties");
    //          通过Properties获取到驱动位置 用户名 密码
                Properties prop = new Properties();
                prop.load(inputStream);
    
                String driver = prop.getProperty("driver");
                url = prop.getProperty("url");
                user = prop.getProperty("user");
                password = prop.getProperty("password");
    //注册驱动
                Class.forName(driver);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        //    创建数据库连接方法
        public static Connection getConnection() {
            Connection connection = null;
            try {
                connection = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    
        //    关闭connection statement
        public static void getClose(Connection conn, Statement stat) {
            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        //    重载 工具类 关闭 executeQuery()获取的resultSet
        public static void getClose(Connection conn, Statement stat, ResultSet res) {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            getClose(conn,stat);
        }
    }
    

    相关文章

      网友评论

        本文标题:JDBC 1.认识jdbc

        本文链接:https://www.haomeiwen.com/subject/theqbqtx.html