JDBC-使用Statement接口实现增、删、改操作

本文介绍了如何使用JDBC中的Statement接口来执行数据库的增加、更新和删除操作。通过DbUtil工具类管理数据库连接,利用Book模型类进行数据操作,分别展示了test方法(增加)、test1方法(更新)和test2方法(删除)的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Statement接口
在这里插入图片描述
2 使用Statement接口实现增加、更新,删除数据操作
在这里插入图片描述
代码结构如上所示,DbUtil表示获取和关闭连接的工具类,Book表示图书的模型类,test表示增加操作,test1表示更新操作,test2表示删除操作,详细代码如下

package com.chb.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DbUtil {

	// 数据库地址
	private static String dbUrl="jdbc:mysql://localhost:3306/db_book";
	// 用户名
	private static String dbUserName="root";
	// 密码
	private static String dbPassword="123456";
	// 驱动名称
	private static String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon()throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @throws Exception
	 */
	public void close(Statement stmt,Connection con)throws Exception{
		if(stmt!=null){
			stmt.close();
			if(con!=null){
				con.close();
			}
		}
		
	}
}
package com.chb.model;

/**
 * 图书模型
 * @author 
 *
 */
public class Book {

	private int id;
	private String bookName;
	private float price;
	private String author;
	private int bookTypeId;
	
	public Book(int id, String bookName, float price, String author,
			int bookTypeId) {
		super();
		this.id = id;
		this.bookName = bookName;
		this.price = price;
		this.author = author;
		this.bookTypeId = bookTypeId;
	}
	public Book(String bookName, float price, String author, int bookTypeId) {
		super();
		this.bookName = bookName;
		this.price = price;
		this.author = author;
		this.bookTypeId = bookTypeId;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public int getBookTypeId() {
		return bookTypeId;
	}
	public void setBookTypeId(int bookTypeId) {
		this.bookTypeId = bookTypeId;
	}	
}
package com.chb.jdbc;

import java.sql.Connection;
import java.sql.Statement;

import com.chb.model.Book;
import com.chb.util.DbUtil;

public class test {

	private static DbUtil dbUtil=new DbUtil();
	
	/**
	 * 添加图书2
	 * @param book
	 * @return
	 * @throws Exception
	 */
	private static int addBook2(Book book)throws Exception{
		Connection con=dbUtil.getCon();  // 获取连接
		String sql="insert into t_book values(null,'"+book.getBookName()+"',"+book.getPrice()+",'"+book.getAuthor()+"',"+book.getBookTypeId()+")";
		Statement stmt=con.createStatement(); // 创建Statement
		int result=stmt.executeUpdate(sql);
		dbUtil.close(stmt, con);  // 关闭Statement和连接
		return result;
	}
	
	/**
	 * 添加图书
	 * @param bookName
	 * @param price
	 * @param author
	 * @param bookTypeId
	 * @return
	 * @throws Exception
	 */
	private static int addBook(String bookName,float price,String author,int bookTypeId)throws Exception{
		Connection con=dbUtil.getCon();  // 获取连接
		String sql="insert into t_book values(null,'"+bookName+"',"+price+",'"+author+"',"+bookTypeId+")";
		Statement stmt=con.createStatement(); // 创建Statement
		int result=stmt.executeUpdate(sql);//result表示执行语句的次数
		dbUtil.close(stmt, con);  // 关闭Statement和连接
		return result;
	}
	
	public static void main(String[] args) throws Exception{
		/*int result=addBook("Java牛牛", 121, "牛哥", 1);
		if(result==1){
			System.out.println("添加成功!");
		}else{
			System.out.println("添加失败!");
		}*/   
		// 多行注释  ctrl+shift+/
		Book book=new Book("Java牛牛2", 1212, "牛哥2", 2);
		int result=addBook2(book);
		if(result==1){
			System.out.println("添加成功!");
		}else{
			System.out.println("添加失败!");
		}
	}
}

package com.chb.jdbc;

import java.sql.Connection;
import java.sql.Statement;

import com.chb.model.Book;
import com.chb.util.DbUtil;

public class test1 {

	private static DbUtil dbUtil = new DbUtil();

	/**
	 * 更新图书
	 * @param book
	 * @return
	 * @throws Exception
	 */
	private static int updateBook(Book book) throws Exception {
		Connection con = dbUtil.getCon(); // 获取连接
		String sql = "update t_book set bookName='" + book.getBookName()
				+ "',price=" + book.getPrice() + ",author='" + book.getAuthor()
				+ "',bookTypeId=" + book.getBookTypeId() + " where id="
				+ book.getId();  // ctrl+a 全选  ctrl+shift+F 格式化代码
		Statement stmt = con.createStatement(); // 创建Statement
		int result = stmt.executeUpdate(sql);
		dbUtil.close(stmt, con); // 关闭Statement和连接
		return result;
	}

	public static void main(String[] args) throws Exception{
		Book book=new Book(3,"Java牛牛2222", 121, "牛哥222", 1);
		int result=updateBook(book);
		if(result==1){
			System.out.println("更新成功!");
		}else{
			System.out.println("更新败!");
		}
		
	}
}

package com.chb.jdbc;

import java.sql.Connection;
import java.sql.Statement;

import com.chb.util.DbUtil;

public class test2 {

	private static DbUtil dbUtil=new DbUtil();
	
	/**
	 * 删除图书
	 * @param id
	 * @return
	 * @throws Exception
	 */
	private static int deleteBook(int id)throws Exception{
		Connection con = dbUtil.getCon(); // 获取连接
		String sql ="delete from t_book where id="+id;
		Statement stmt = con.createStatement(); // 创建Statement
		int result = stmt.executeUpdate(sql);
		dbUtil.close(stmt, con); // 关闭Statement和连接
		return result;
	}
	
	public static void main(String[] args) throws Exception{
		int result=deleteBook(3);
		if(result==1){
			System.out.println("删除成功!");
		}else{
			System.out.println("删除失败!");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值