java JDBC接口之Statement和PrepareStatement(3)

一. 接口说明

1.Statement和PrepareStatement都是执行sql 的接口

2.PrepareStatement可以预防sql注入,实现原理是将SQL特殊的符号加反斜杠,使其转换为普通的字符串,而不是sql命令,就是我们常说的预编译过程,而Statement的没有这样的功能的

2.接口说明和使用实列

1.获取对象和执行更新操作sql

/**
 * @author zeng
 */
public class Main {
    private static final String username = "root";
    private static final String password = "root";
    private static final String url = "jdbc:mysql://bar-mysql:3306/bar_baruser?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";

    public static void main(String[] args) throws Exception {
        final Connection connection = DriverManager.getConnection(url, username, password);
        // 获取一个 Statement对象,用于将SQL语句发送到数据库。
        Statement statement = connection.createStatement();
        String sqlStatement="update bar_user set username='zhangsan' where user_id=1";
        // 返回影响的行数
        final int isUpdate = statement.executeUpdate(sqlStatement);
        System.out.println(isUpdate);//1
        // PreparedStatement通过 ? 预留参数 在preparedStatement调用set 设置值,索引从1开始
        String sqlPreparedStatement=
                "update bar_user set username = ? where user_id = ? ";
        // 获取一个 PreparedStatement对象
        final PreparedStatement preparedStatement
                = connection.prepareStatement(sqlPreparedStatement);
        // 设置username 的值
        preparedStatement.setString(1,"lishi");
        // 设置user_id 的值 
        preparedStatement.setInt(2,1);
        final int isPreparedStatementUpdate = preparedStatement.executeUpdate();
        System.out.println(1);

        // 关闭资源
        statement.close();
        connection.close();
        preparedStatement.close();
    }
}

2.Statement事务

1.setAutoCommit(false); 设置手动事务

2.  connection.commit();提交事务

3.connection.rollback(); 回滚事务

示例代码

import java.sql.*;

/**
 * @author zeng
 */
public class Main {
    private static final String username = "root";
    private static final String password = "root";
    private static final String url = "jdbc:mysql://bar-mysql:3306/bar_baruser?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            // 获取一个 Statement对象,用于将SQL语句发送到数据库。
            Statement statement = connection.createStatement();
            // 设置手动提交事务
            connection.setAutoCommit(false);
            String sqlStatement = "update bar_user set username='admin' where user_id=1";
            // 返回影响的行数
            final int isUpdate = statement.executeUpdate(sqlStatement);
            // 假如给一个异常
            int i=1/0;
            connection.commit();
        }catch (Exception e){
            if (connection!=null){
                // 回滚数据
                connection.rollback();
            }
            e.printStackTrace();
        }finally {
            if (connection!=null){
                // 关闭资源
                connection.close();
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中打印插入SQL的PreparedStatement可以按照以下步骤进行: 1. 导入相关的Java类和库:使用Java的标准库,我们需要导入java.sql包中的相关类,包括PreparedStatement类。 2. 建立数据库连接:首先需要建立与数据库的连接,使用DriverManager类的getConnection()方法来获取一个连接。 3. 创建PrepareStatement对象:使用连接对象的prepareStatement()方法创建一个PreparedStatement对象,并将插入SQL作为参数传递进去。 4. 设置插入参数:如果需要在SQL语句中使用参数,可使用PreparedStatement对象的setXXX()方法设置参数的值,其中XXX表示参数的类型,如setString()、setInt()等。 5. 执行插入操作:使用PreparedStatement对象的executeUpdate()方法执行插入操作,该方法返回一个整数值,表示受影响的行数。 6. 关闭连接和PreparedStatement对象:在插入操作完成后,需要关闭连接和PreparedStatement对象,以释放资源,使用close()方法或try-with-resources语句块来实现。 下面是一个示例代码: ``` import java.sql.*; public class Main { public static void main(String[] args) { try { // 建立数据库连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建PreparedStatement对象 PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable (name, age) VALUES (?, ?)"); // 设置插入参数 pstmt.setString(1, "张三"); pstmt.setInt(2, 20); // 执行插入操作 int rows = pstmt.executeUpdate(); System.out.println("插入成功,受影响的行数:" + rows); // 关闭连接和PreparedStatement对象 pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 以上就是使用Java打印插入SQL的PreparedStatement的步骤,通过设置参数和执行插入操作,可以将相关数据插入到数据库中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值