• JDBC


    JDBC

    JDBC本质

    JDBC(java databases connectiviting)
    JDBC定义了一套操作关系型数据库的公共接口,不同数据库厂家实现其接口,真正执行的是驱动jar包中的实现类

    快速入门

    1、导入驱动jar包
    http://dev.mysql.com/downloads/connector/
    教程:https://www.cnblogs.com/it-mh/p/11205866.html
    2、注册驱动
    3、获取数据库连接对象
    4、定义sql语句
    5、获取执行sql的statement对象
    6、执行sql
    7、处理结果
    8、释放资源

    代码实现:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Demo {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            // 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 获取链接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3","root","123");
            // 定义sql
            String sql = "update student set age =100 where name = 'king'";
            // 获取执行sql的statement对象
            Statement statement = connection.createStatement();
            // 执行sql
            int count = statement.executeUpdate(sql);
            // 结果
            System.out.println(count);
            // 释放资源
            statement.close();
            connection.close();
        }
    }
    

    JDBC-curd操作实例

    statement.executeUpdate(sql)返回值是int,表示返回的影响行数一般执行DDL,DML语句。
    stetement.executeQuery(sql)返回值是ResultSet是字段结果返回集合。
    先看executeUpdate.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JDBCDemo {
        public static void main(String[] args) {
            Connection conn = null;
            Statement statement = null;
            try {
                // 注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 建立连接
                conn = DriverManager.getConnection("jdbc:mysql://rm-wz9gfi4876z4y12036o.mysql.rds.aliyuncs.com/test", "root", "123");
                // 创建sql
                String addSql = "insert into student values(6,'a',12)";
                String updateSql = "update student set age = 100 where id = 3";
                String delSql = "delete from student where id = 4";
                // 创建statement对象
                statement = conn.createStatement();
                // 执行sql
                int addCount = statement.executeUpdate(addSql);
                int updateCount = statement.executeUpdate(updateSql);
                int delCount = statement.executeUpdate(delSql);
                System.out.println("新增:" + addCount + "修改:" + updateCount + "删除:" + delCount);
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                if (null != statement) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (null != statement) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    ResultSet的使用:

    import java.sql.*;
    
    public class JDBCDemo {
        public static void main(String[] args) {
            Connection conn = null;
            Statement statement = null;
            try {
                // 注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 建立连接
                conn = DriverManager.getConnection("jdbc:mysql://rm-wz9gfi4876z4y12036o.mysql.rds.aliyuncs.com/test", "root", "123");
                // 创建sql
                statement = conn.createStatement();
                ResultSet resultSet = statement.executeQuery("select * from  student");
                // 获取返回集
                while (resultSet.next()){
                    System.out.println(resultSet.getString("name"));
                }
                resultSet.close();
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                if (null != statement) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (null != statement) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    2019/10/21 动手动脑
    2019/10/21 课堂测试---向数据库添加新用户
    2019/10/14 如何在静态方法中访问类的实例成员
    2019/10/14 构造函数、默认值、初始化块的优先级。
    2019/10/14 动手动脑---
    2019/9/30 极限测试(1)
    读《程序员的修炼之道------从小工到专家》有感 1
    课堂测试,统计小说<飘>前N个最常出现的单词
    Java课堂 动手动脑
    2019/9/23 课堂测试一 回文
  • 原文地址:https://www.cnblogs.com/Kingfan1993/p/13357877.html
Copyright © 2020-2023  润新知