Statement 接口引入
作用:用于执行静态 SQL 语句并返回它所生成结果的对象。
- int executeUpdate(String sql) 执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或
DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。 - void close() 立即释放此 Statement 对象的数据库和 JDBC 资源,而不是等待该对象自动关闭时发生此操作。
使用 Statement 接口实现添加数据操作
添加数据时,若添加后的数据为???则在连接数据库时加上?useUnicode=true&characterEncoding=UTF-8
这一句话。如下所示:
private static String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8";
数据Book.java
package com.cn.zj.JDBC.model;
/**
* 图书模型
* @author Administrator
*
*/
public class Book {
private int id;
private String bookName;
private float price;
private String author;
private int bookTypeId;
//构造方法 Source+fields
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;
}
}
连接数据库通用方法DbUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* 通用方法
* @author Administrator
*
*/
public class DbUtil {
//驱动名称
private static String jdbcName="com.mysql.jdbc.Driver";
//mysql数据库地址
private static String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8";
//用户名
private static String dbUserName="root";
//密码
private static String dbPassword="root";
//获取数据库连接的方法
public Connection getCon() throws Exception{
Class.forName(jdbcName); //加载驱动
Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
return con;
}
//关闭数据库连接
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
插入数据demo02.java
import java.sql.Connection;
import java.sql.Statement;
import com.cn.zj.JDBC.model.Book;
import com.cn.zj.JDBCUtil.DbUtil;
public class demo02 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加图书2
* @param book
* @return
* @throws Exception
*/
private static int addBook2(Book book) throws Exception{
Connection con=dbUtil.getCon(); //获取连接
//SQL语句
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); //关闭连接
return result;
}
/**
* 添加图书
* @param bookName
* @param price
* @param author
* @param bookTypeId
* @return
* @throws Exception
*/
public 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);
dbUtil.close(stmt, con); //关闭连接
return result;
}
public static void main(String[] args) throws Exception {
/* int result=addBook("我是你妈妈",99,"我",1);
if(result==1){
System.out.println("数据添加成功");
}else{
System.out.println("添加失败!");
}*/
//多行注释 ctrl+shift+/
Book book=new Book("我是你后妈",99,"我儿",1);
int result=addBook2(book);
if(result==1){
System.out.println("数据添加成功");
}else{
System.out.println("添加失败!");
}
}
}
使用 Statement 接口实现更新数据操作
数据Book.java
因为根据ID更改数据,所以重载构造方法
/**
* 图书模型
* @author Administrator
*
*/
public class Book {
private int id;
private String bookName;
private float price;
private String author;
private int bookTypeId;
//构造方法 Source+fields
public Book(String bookName, float price, String author, int bookTypeId) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
}
//更新数据要包括ID,重载构造方法
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 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;
}
}
更改数据方法
import java.sql.Connection;
import java.sql.Statement;
import com.cn.zj.JDBC.model.Book;
import com.cn.zj.JDBCUtil.DbUtil;
/**
* 更新数据
* @author Administrator
*
*/
public class demo {
private static DbUtil dbUtil=new DbUtil();
private static int updateBook(Book book)throws Exception{
Connection con=dbUtil.getCon(); //获取连接
//根据ID来对数据进行更新
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); //关闭连接
return result;
}
public static void main(String[] args) throws Exception {
Book book=new Book(1,"我是你爹爹",9,"爹爹",2);
int result=updateBook(book);
if(result==1){
System.out.println("数据更新成功");
}else{
System.out.println("更新失败!");
}
}
}
使用 Statement 接口实现删除数据操作
删除数据方法
import java.sql.Connection;
import java.sql.Statement;
import com.cn.zj.JDBCUtil.DbUtil;
/*
* 删除数据
*/
public class demo {
private static DbUtil dbUtil=new DbUtil();
private static int deleteBook(int id)throws Exception{
Connection con=dbUtil.getCon(); //获取连接
//根据ID来对数据进行更新
String sql ="delete from t_book where id="+id;
Statement stmt=con.createStatement(); //创建Statement
int result=stmt.executeUpdate(sql);
dbUtil.close(stmt, con); //关闭连接
return result;
}
public static void main(String[] args) throws Exception {
int result=deleteBook(10017);
if(result==1){
System.out.println("数据更新成功");
}else{
System.out.println("更新失败!");
}
}
}