Jdbc -Statement

Java提供了 Statement、PreparedStatement 和 CallableStatement三种方式来执行查询语句;

PreparedStatement是用于执行参数化查询

预编译statement,提高查询速度,防止sql注入。通过调用connection.preparedStatement(sql)方法可以获得PreparedStatment对象。数据库系统会对sql语句进行预编译处理(如果JDBC驱动支持的话),预处理语句将被预先编译好,这条预编译的sql查询语句能在将来的查询中重用,这样一来,它比Statement对象生成的查询速度更快。预编译:比如java会编译为class文件,然后在编译为计算机能识别的语言,sql语句也一样。

使用范围:当执行相似sql语句的次数比较多(例如用户登陆,对表频繁操作..)语句一样,只是具体的值不一样,被称为动态SQL

优点:语句只编译一次,减少编译次数。提高了安全性(阻止了SQL注入)

缺点: 执行非相似SQL语句时,速度较慢。

原理:相似SQL只编译一次,减少编译次数

Statement 用于通用查询。

使用范围:当执行相似SQL(结构相同,具体值不同)语句的次数比较少

优点:语法简单

缺点:采用硬编码效率低,安全性较差。

原理:硬编码,每次执行时相似SQL都会进行编译

CallableStatement则是用于存储过程。

1 statement 代码实例

public static void main(String[] args) {

        // 数据库连接
        Connection connection = null;
        //Statement,使用预编译的Statement提高数据库性能
        Statement statement = null;
        // 结果 集
        ResultSet resultSet = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 通过驱动管理类获取数据库链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02?characterEncoding=utf-8",
                    "root", "");
            // 定义sql语句 ?表示占位符
            String sql = "select * from user where username = '王五'";
            // 获取预处理statement
            statement = connection.createStatement();
            //新增
            statement.executeUpdate("INSERT INTO USER (username ,birthday,sex)VALUES('fyp','2014-01-02',1)",
                    Statement.RETURN_GENERATED_KEYS);
            connection.setAutoCommit(false);           
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }

PreparedStatement

    public static void main(String[] args) {

        // 数据库连接
        Connection connection = null;
        // 预编译的Statement,使用预编译的Statement提高数据库性能
        PreparedStatement preparedStatement = null;
        // 结果 集
        ResultSet resultSet = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 通过驱动管理类获取数据库链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02?characterEncoding=utf-8",
                    "root", "");
            // 定义sql语句 ?表示占位符
            String sql = "select * from user where username = ?";
            // 获取预处理statement
            connection.createStatement();
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
            preparedStatement.setString(1, "王五");
            // 向数据库发出sql执行查询,查询出结果集
            resultSet = preparedStatement.executeQuery();
            // 遍历查询结果集
            while (resultSet.next()) {
                System.out.println(resultSet.getString("id") + "  " + resultSet.getString("username"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
原文地址:https://www.cnblogs.com/fanBlog/p/9511058.html