一、Statement的介绍
作用:用于执行静态的SQL语句并返回它所生成结果的对象(可查JavaAPI文档Statement)
使用Connection对象下的createStatement()方法创建Statement对象。
int executeUpdate(String sql) 执行给定SQL语句,该语句可能为INSERT、UPDATE或DELETE语句,或者不返回任何内容的SQL语句(如 SQL DDL语句)。
void close() 立即释放此Statement对象的数据库 和JDBC资源,而不是等待该对象自动关闭时发生此操作。
void close() 立即释放此Statement对象的数据库 和JDBC资源,而不是等待该对象自动关闭时发生此操作。
二、INSERT、UPDATE和DELETE操作数据
1、创建book类
public class Book {
//基本属性
private int id;
private String bookName;
private String author;
private float price;
//构造函数重载
Book(){
super();
}
public Book(String bookName, String author, float price) {
super();
this.bookName = bookName;
this.author = author;
this.price = price;
}
public Book(int id, String bookName, String author, float price) {
super();
this.id = id;
this.bookName = bookName;
this.author = author;
this.price = price;
}
//属性获取、设置方法
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
2、创建连接数据库工具类import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbUtil {
private String url="jdbc:mysql://localhost:3306/book";//数据库连接地址
private String user="root";//数据库用户名
private String password="000000";//密码
private String jdbcName="com.mysql.jdbc.Driver";//jdbc驱动
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getcon() throws Exception{
Class.forName(jdbcName);//把jdbc驱动家在进虚拟机
Connection con=DriverManager.getConnection(url, user, password);//获取数据库连接
return con;
}
/**
* 关闭数据库连接
* @param sta
* @param con
* @throws SQLException
*/
public void close(Statement sta,Connection con) throws SQLException{
if(sta != null){
sta.close();//关闭Statement连接
if(con != null){
con.close();//关闭数据库连接
}
}
}
public void close(Connection con) throws SQLException{
if(con != null){
con.close();//关闭数据库连接
}
}
}
3、数据库操作,insert、update、delete语句练习import java.sql.Connection;
import java.sql.Statement;
import model.Book;
import util.DbUtil;
public class demo1 {
/**
* 插入图书
* @param book
* @throws Exception
*/
private static void addBook(Book book) throws Exception{
DbUtil dbUtil = new DbUtil();
String sql="insert into t_book values(null,'"+book.getBookName()
+"','"+book.getAuthor()+"',"+
book.getPrice()+")";//插入数据
Connection con = dbUtil.getcon();//获取数据库连接
Statement sta = con.createStatement();//实例化一个Statement对象
int result = sta.executeUpdate(sql);
if(result == 1){
System.out.println("插入数据成功");
}else{
System.out.println("插入数据失败");
}
dbUtil.close(sta, con);//关闭连接
}
/**
* 更新图书
* @param book
* @throws Exception
*/
private static void updateBook(Book book) throws Exception{
DbUtil dbUtil = new DbUtil();
Connection con = dbUtil.getcon();
String sql="update t_book set bookName='"+book.getBookName()
+"',author='"+book.getAuthor()+"',price="
+book.getPrice()+" where id="+book.getId();
Statement sta = con.createStatement();
int result = sta.executeUpdate(sql);
if(result == 1){
System.out.println("更新数据成功");
}else{
System.out.println("更新数据失败");
}
dbUtil.close(sta, con);//关闭连接
}
/**
* 删除图书
* @param index
* @throws Exception
*/
private static void deleteBook(int index) throws Exception{
DbUtil dbUtil = new DbUtil();
Connection con = dbUtil.getcon();
String sql="delete from t_book where id="+index;
Statement sta = con.createStatement();
int result = sta.executeUpdate(sql);
if(result == 1){
System.out.println("删除数据成功");
}else{
System.out.println("删除数据失败");
}
dbUtil.close(sta, con);//关闭连接
}
public static void main(String[] args) throws Exception {
/*Book book = new Book("java","你好",48);
addBook(book);//插入数据
*/
/*Book book = new Book(1,"java1","你不好",34);
updateBook(book);//更新数据库*/
//deleteBook(1);
}
}