0


【Java 进阶篇】JDBC Statement:执行 SQL 语句的重要接口

在这里插入图片描述

在Java应用程序中,与数据库进行交互是一项常见的任务。为了执行数据库操作,我们需要使用JDBC(Java Database Connectivity)来建立与数据库的连接并执行SQL语句。

Statement

接口是JDBC中的一个重要接口,它用于执行SQL语句并与数据库进行交互。本文将详细介绍

Statement

接口的使用,包括如何创建

Statement

对象、执行SQL语句、处理结果等内容。

什么是 JDBC Statement?

Statement

接口是JDBC的一部分,它允许我们向数据库发送SQL查询和更新语句,并从数据库中获取结果。

Statement

接口有多个子接口和实现类,常用的有以下几种:

  • Statement:用于执行普通的SQL语句,不带有参数。
  • PreparedStatement:用于执行预编译的SQL语句,可以带有参数,防止SQL注入攻击。
  • CallableStatement:用于执行数据库存储过程。

在本文中,我们将主要关注

Statement

接口及其用法。

创建 JDBC Statement 对象

在执行SQL语句之前,首先需要创建

Statement

对象。

Statement

对象通常与

Connection

对象关联,通过

Connection

对象的

createStatement()

方法创建。下面是创建

Statement

对象的示例代码:

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;publicclassStatementExample{publicstaticvoidmain(String[] args){// JDBC连接参数String url ="jdbc:mysql://localhost:3306/mydatabase";String username ="root";String password ="password";// 创建Connection对象try(Connection connection =DriverManager.getConnection(url, username, password)){// 创建Statement对象Statement statement = connection.createStatement();// 在此处执行SQL语句// 关闭Statement
            statement.close();}catch(SQLException e){
            e.printStackTrace();}}}

在上述代码中,我们首先创建了一个

Connection

对象,然后使用

createStatement()

方法创建了一个

Statement

对象。需要注意的是,我们在

try-with-resources

块中创建了

Connection

Statement

对象,这样在退出块时会自动关闭这些资源,无需手动关闭。

执行 SQL 查询语句

一旦创建了

Statement

对象,我们可以使用它来执行SQL查询语句。以下是一个简单的示例,演示如何执行

SELECT

查询并处理查询结果:

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassSelectExample{publicstaticvoidmain(String[] args){// JDBC连接参数String url ="jdbc:mysql://localhost:3306/mydatabase";String username ="root";String password ="password";try(Connection connection =DriverManager.getConnection(url, username, password)){// 创建Statement对象Statement statement = connection.createStatement();// 执行SQL查询语句String sql ="SELECT * FROM employees";ResultSet resultSet = statement.executeQuery(sql);// 处理查询结果while(resultSet.next()){int id = resultSet.getInt("id");String name = resultSet.getString("name");double salary = resultSet.getDouble("salary");System.out.println("ID: "+ id +", Name: "+ name +", Salary: "+ salary);}// 关闭ResultSet和Statement
            resultSet.close();
            statement.close();}catch(SQLException e){
            e.printStackTrace();}}}

在上述代码中,我们使用

executeQuery()

方法执行

SELECT

查询语句,然后使用

ResultSet

对象遍历查询结果。最后,我们关闭了

ResultSet

Statement

对象,释放资源。

执行 SQL 更新语句

除了查询,

Statement

对象还可以用于执行SQL更新语句,如

INSERT

UPDATE

DELETE

。以下是一个示例,演示如何执行

INSERT

语句来插入新数据:

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;publicclassInsertExample{publicstaticvoidmain(String[] args){// JDBC连接参数String url ="jdbc:mysql://localhost:3306/mydatabase";String username ="root";String password ="password";try(Connection connection =DriverManager.getConnection(url, username, password)){// 创建Statement对象Statement statement = connection.createStatement();// 执行SQL更新语句String sql ="INSERT INTO employees (name, salary) VALUES ('John', 50000)";int rowsAffected = statement.executeUpdate(sql);if(rowsAffected >0){System.out.println("Insertion successful. Rows affected: "+ rowsAffected);}else{System.out.println("Insertion failed.");}// 关闭Statement
            statement.close();}catch(SQLException e){
            e.printStackTrace();}}}

在上述代码中,我们使用

executeUpdate()

方法执行

INSERT

语句,并检查受影响的行数来确定是否插入成功。

防止 SQL 注入攻击

在使用

Statement

执行SQL语句时,要注意防止SQL注入攻击。SQL注入攻击是一种常见的网络安全威胁,它可以通过恶意构造的输入来破坏数据库操作。为了防止SQL注入攻击,应该使用

PreparedStatement

而不是

Statement

来执行带有参数的SQL语句。

PreparedStatement

允许将参数绑定到SQL语句中,从而避免直接拼接用户输入,如下所示:

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.SQLException;publicclassPreparedStatementExample{publicstaticvoidmain(String[] args){// JDBC连接参数String url ="jdbc:mysql://localhost:3306/mydatabase";String username ="root";String password ="password";try(Connection connection =DriverManager.getConnection(url, username, password)){// 创建PreparedStatement对象String sql ="INSERT INTO employees (name, salary) VALUES (?, ?)";PreparedStatement preparedStatement = connection.prepareStatement(sql);// 设置参数
            preparedStatement.setString(1,"Alice");
            preparedStatement.setDouble(2,60000);// 执行SQL更新语句int rowsAffected = preparedStatement.executeUpdate();if(rowsAffected >0){System.out.println("Insertion successful. Rows affected: "+ rowsAffected);}else{System.out.println("Insertion failed.");}// 关闭PreparedStatement
            preparedStatement.close();}catch(SQLException e){
            e.printStackTrace();}}}

在上述代码中,我们使用

PreparedStatement

来执行

INSERT

语句,并使用

setString()

setDouble()

方法设置参数值。这样可以确保用户输入不会被误解为SQL代码,提高了安全性。

总结

Statement

接口是JDBC中执行SQL语句的关键接口之一。通过创建

Statement

对象,我们可以执行查询和更新等各种数据库操作。然而,为了提高安全性,建议在执行SQL语句时使用

PreparedStatement

,尤其是涉及用户输入的情况下。希望本文能够帮助您更好地理解JDBC中的

Statement

接口以及如何使用它来与数据库进行交互。
作者信息

作者 : 繁依Fanyi
CSDN: https://techfanyi.blog.csdn.net
掘金:https://juejin.cn/user/4154386571867191

标签: java sql 开发语言

本文转载自: https://blog.csdn.net/qq_21484461/article/details/133467613
版权归原作者 繁依Fanyi 所有, 如有侵权,请联系我们删除。

“【Java 进阶篇】JDBC Statement:执行 SQL 语句的重要接口”的评论:

还没有评论