欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用PreparedStatement接口实现增,删,改操作

程序员文章站 2022-03-06 18:38:23
...
1. PreparedStatement接口引入

PreparedStatement是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语句,但是此SQL语句的具体内容暂时不设置,而是之后再进行设置。

以后开发一般用PreparedStatement,不用Statement


2. 使用PreparedStatement接口实现添加数据操作

3. 使用PreparedStatement接口实现更新数据操作

4. 使用PreparedStatement接口实现删除数据操作

TestInsert.java

package com.andrew.jdbc.chap04;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.andrew.jdbc.model.Book;
import com.andrew.jdbc.util.DbUtil;

public class TestInsert {
    private static DbUtil dbUtil = new DbUtil();
    private static int addBook(Book book) throws Exception {
        Connection connection = dbUtil.getConnection(); // 获取连接
        String sql = "insert into t_book values(null,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, book.getBookName());
        preparedStatement.setFloat(2, book.getPrice());
        preparedStatement.setString(3, book.getAuthor());
        preparedStatement.setInt(4, book.getBookTypeId());
        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement, connection);
        return result;
    }
    public static void main(String[] args) throws Exception {
        Book book = new Book("Java学习2", 1, "学习2", 1);
        int result = addBook(book);
        if (result == 1) {
            System.out.println("添加成功!");
        } else {
            System.out.println("添加失败!");
        }
    }
}

添加成功!

TestUpdate.java

package com.andrew.jdbc.chap04;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.andrew.jdbc.model.Book;
import com.andrew.jdbc.util.DbUtil;

public class TestUpdate {
    private static DbUtil dbUtil = new DbUtil();
    private static int updateBook(Book book) throws Exception {
        Connection connection = dbUtil.getConnection();
        String sql = "update t_book set bookName=?,price=?,author=?,bookTypeId=? where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, book.getBookName());
        preparedStatement.setFloat(2, book.getPrice());
        preparedStatement.setString(3, book.getAuthor());
        preparedStatement.setInt(4, book.getBookTypeId());
        preparedStatement.setInt(5, book.getId());
        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement, connection);
        return result;
    }
    public static void main(String[] args) throws Exception {
        Book book = new Book(4, "K2", 2, "K", 2);
        int result = updateBook(book);
        if (result == 1) {
            System.out.println("更新成功!");
        } else {
            System.out.println("更新失败!");
        }
    }
}

更新成功!

TestDelete.java

package com.andrew.jdbc.chap04;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.andrew.jdbc.util.DbUtil;

public class TestDelete {
    private static DbUtil dbUtil = new DbUtil();
    private static int deleteBook(int id) throws Exception {
        Connection connection = dbUtil.getConnection();
        String sql = "delete from t_book where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(4, id);
        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement, connection);
        return result;
    }
    public static void main(String[] args) throws Exception {
        int result = deleteBook(12);
        if (result == 1) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }
}

删除成功!
相关标签: jdbc